在一台Linux服务器上安装多个MySQL实例(二)--使用单独的MySQL配置文件

前端之家收集整理的这篇文章主要介绍了在一台Linux服务器上安装多个MySQL实例(二)--使用单独的MySQL配置文件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

上一篇文章介绍了两种MysqL部署多实例的方法,分别是:

  • 使用官方自带MysqLd_multi来配置管理,特点是使用同一份MysqL配置文件,这种方式属于集中式管理,管理起来较为方便;
  • 使用单独的MysqL配置文件来单独配置实例,这种方式逻辑简单,数据库之间没有关联。

两种方法最大的区别在于:使用MysqLd_multi方式,所有的实例共使用一个参数文件,使用第二种方法,则各自实例使用自己的参数文件

第一种方方法已经在第一篇文档里面演示过了,这里学习一下第二种方法

 

(一)实验环境

操作系统   :CentOS Linux release 7.4.1708 (Core)

数据库版本:5.7.24-log

预计划安装3个MysqL实例,规划信息为:

实例1 实例2 实例3

basedir=/usr/local/MysqL

datadir=/MysqL/3306/data

port=3306

socket=/tmp/MysqL_3306.sock

参数文件:/MysqL/3306/my.cnf

basedir=/usr/local/MysqL

datadir=/MysqL/3307/data

port=3307

socket=/tmp/MysqL_3307.sock

参数文件:/MysqL/3307/my.cnf

basedir=/usr/local/MysqL

datadir=/MysqL/3308/data

port=3308

socket=/tmp/MysqL_3308.sock

参数文件:/MysqL/3308/my.cnf

 

(二)实验过程

(2.1)在安装MysqL之前,需要卸载服务器自带MysqL包和MysqL数据库分支mariadb的包

[root@masterdb ~]# rpm -qa|grep MysqL
[root@masterdb ~]# rpm -qa | mariadb
mariadb-libs-5.5.56-2.el7.x86_64

[root@masterdb ~]# rpm -e mariadb-libs-2.el7.x86_64 --nodeps

 

(2.2)依赖包安装

MysqL对libaio 库有依赖性。如果未在本地安装该库,则数据目录初始化和随后的服务器启动步骤将失败

# install library

[root@MysqL MysqL]# yum install libaio

对于MysqL 5.7.19和更高版本:通用Linux版本中增加了对非统一内存访问(NUMA)的支持,该版本现在对libnuma库具有依赖性 。

[root@MysqL MysqL]# install libnuma

(2.3)创建用户用户

[root@masterdb ~]# groupadd MysqL
[root@masterdb ~]# useradd -r -g MysqL -s /bin/false MysqL

 

(2.4)解压安装包

[root@masterdb ~]# cd /usr/local/
[root@masterdb local]# tar xzvf /root/MysqL-5.7.24-linux-glibc2.12-x86_64.tar.gz

# 修改解压文件名,与前面定义的basedir相同
[root@masterdb local]# mv MysqL-12-x86_64/ MysqL

最终解压结果如下:

[root@masterdb MysqL]# ls -l
 total 36
 drwxr-xr-x  2 root root   4096 Mar 28 13:48 bin
 -rw-r--r--  1 7161 31415 17987 Oct  4  2018 COPYING
 drwxr-xr-x  2 root root     55 Mar  docs
 drwxr-xr-x  3 root root    include
 drwxr-xr-x  5 root root    230 Mar  lib
 drwxr-xr-x  4 root root     30 Mar 48 man
 -rw-r--r--  31415  2478 Oct   README
 drwxr-xr-x 28 root root    share
 drwxr-xr-x  90 Mar 48 support-files

(2.5)创建数据文件存放路径

mkdir -p /MysqL/{3306,3307,1)">3308,1)">3309}/data [root@masterdb MysqL]# chown -R MysqL:MysqL /MysqL [root@masterdb MysqL]# cd /MysqL [root@masterdb MysqL]# tree . ├── 3306 │ └── data ├── 3307 │ └── data └── 3308 └── data

 

(2.6)创建MysqL参数配置文件

分别为3306、3307、3308各自创建一个my.cnf配置文件配置文件位置为:

[root@masterdb MysqL]# tree /MysqL
/MysqL
├── 
│   ├── data
│   └── my.cnf
├── 
│   ├── data
│   └── my.cnf
└── 
    ├── data
    └── my.cnf

 

其中,各个实例的配置文件信息如下:

实例1(3306) 实例2(3307) 实例3(3308)
[MysqLd]
port = 
basedir=/usr/local/MysqL/
datadir=/MysqL/3306/data
lower_case_table_names=1
innodb_buffer_pool_size=128M
socket=/tmp/MysqL_3306.sock
3307/128M socket=/tmp/MysqL_3307.sock
3308/128M socket=/tmp/MysqL_3308.sock

 

(2.7)初始化数据库

需要注意,初始化结束的最后一行记录了root的密码

# 初始化3306数据库 [root@masterdb MysqL]# /usr/local/MysqL/bin/MysqLd --defaults-file=/MysqL/3306/my.cnf --initialize --basedir=/usr/local/MysqL/ --datadir=/MysqL/data 2020-03-28T08:19:11.202256Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details). 11.352108Z 0 [Warning] InnoDB: New log files created,LSN=45790 11.383671Z 0 [Warning] InnoDB: Creating foreign key constraint system tables. 11.441325Z 0 [Warning] No existing UUID has been found,so we assume that this is the first time that this server has been started. Generating a new UUID: cdab4062-70cc-11ea-aedf-000c29ea7752. 11.442190Z 0 [Warning] Gtid table is not ready to be used. Table 'MysqL.gtid_executed' cannot be opened. 11.442995Z 1 [Note] A temporary password is generated for root@localhost: XE4kl>x4fo?
v

# 初始化3307数据库
[root@masterdb MysqL]# /usr/local/MysqL/bin/MysqLd --defaults-3307/my.cnf --initialize --basedir=/usr/local/MysqL/ --datadir=/MysqL/21.704008Z 21.846567Z 21.877435Z 21.934698Z time that this server has been started. Generating a new UUID: d3ec69cd-70cc-11ea-b101-21.935421Z 21.937056Z 3Oir)9uX2?su


# 初始化3308数据库
[root@masterdb MysqL]# /usr/local/MysqL/bin/MysqLd --defaults-3308/my.cnf --initialize --basedir=/usr/local/MysqL/ --datadir=/MysqL/20:04.342747Z 04.488694Z 04.519974Z 04.579649Z time that this server has been started. Generating a new UUID: ed57843b-70cc-11ea-b206-04.580544Z 04.581480Z NhwFZv6i!UfZ

 

(2.8)设置环境变量

[root@masterdb MysqL]# vim /etc/profile
# 在文件末尾添加下面信息
export PATH=/usr/local/MysqL/bin:$PATH

#使环境变量生效
[root@masterdb MysqL]# source /etc/profile

 

(2.9)启动数据库

# 经过测试,MysqL在初始化的时候新生成的部分文件权限为root,所以最好在启动之前重新将datadir路径授权给MysqL [root@masterdb MysqL]# MysqL # 启动MysqL数据库实例 [root@masterdb ~]# nohup /usr/local/MysqL/bin/MysqLd --defaults-3306/my.cnf --user=MysqL & [root@masterdb ~]# nohup /usr/local/MysqL/bin/MysqLd --defaults-3307/my.cnf --user=MysqL &3308/my.cnf --user=MysqL &

 

(2.10)确认MysqL数据库状况

# 确认MysqL进程 [root@masterdb 3307]# ps -ef| MysqL MysqL 11092 1371 0 16:36 pts/0 00:00 /usr/local/MysqL/bin/MysqLd --defaults-3307/my.cnf --user=MysqL MysqL 11146 3308/my.cnf --user=11182 7 37 pts/3306/my.cnf --user=MysqL root 11211 00 grep --color=auto MysqL # 确认端口使用情况 [root@masterdb 3307]# netstat -ntl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0.0.0.0:22 0.0:* LISTEN tcp 127.0.0.1:25 LISTEN tcp6 0 :::3307 :::* LISTEN tcp6 3308 :::*22 :::*0 ::1:25 :::*3306 :::* LISTEN

 

【完】

 

相关文档集合:

1.在一台Linux服务器上安装多个MySQL实例(一)--使用mysqld_multi方式

2.在一台Linux服务器上安装多个MysqL实例(二)--使用单独的MysqL配置文件

猜你在找的MySQL相关文章