mysql多实例管理(一台服务器可以装多个mysql服务器,启动多个端口号)


1、 mysql 多实例

[root@docker01 ~]# mkdir -p /data/330{7,8,9}/data 创建多个数据目录

[root@docker01 ~]# vim /data/330

3307/ 3308/ 3309/ 

[root@docker01 ~]# vim /data/3307/my.cnf

[root@docker01 ~]# vim /data/3308/my.cnf

[root@docker01 ~]# vim /data/3309/my.cnf

[root@docker01 ~]# cat /data/3307/my.cnf 

[mysqld]

basedir=/usr/local/mysql

datadir=/data/3307/data

socket=/data/3307/mysql.sock

log_error=/data/3307/mysql.log

port=3307

server_id=7

log_bin=/data/3307/mysql-bin


[root@docker01 ~]# cat /data/3308/my.cnf 

[mysqld]

basedir=/usr/local/mysql

datadir=/data/3308/data

socket=/data/3308/mysql.sock

log_error=/data/3308/mysql.log

port=3308

server_id=8

log_bin=/data/3308/mysql-bin


[root@docker01 ~]# cat /data/3309/my.cnf 

[mysqld]

basedir=/usr/local/mysql

datadir=/data/3309/data

socket=/data/3309/mysql.sock

log_error=/data/3309/mysql.log

port=3309

server_id=9

log_bin=/data/3309/mysql-bin


2、 初始化三套数据

mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3307/data

mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3308/data

mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql --datadir=/data/3309/data

启动时有这个错误

[root@docker01 3309]# mysqld --initialize-insecure --user=mysql --basedir=/usr/local/mysql/ --datadir=/data/3309/data/

2020-10-30T10:29:36.954687Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).

2020-10-30T10:29:36.959026Z 0 [ERROR] --initialize specified but the data directory has files in it. Aborting.

2020-10-30T10:29:36.959094Z 0 [ERROR] Aborting

说是有data的目录了,但是端口能启动

3、 授权

[root@docker01 ~]# chown -R mysql.mysql /data/*

4、 systemd管理多实例

[root@docker01 ~]# cd /etc/systemd/system

[root@docker01 system]#cp mysqld.service mysqld3307.service 

[root@docker01 system]#cp mysqld.service mysqld3308.service 

[root@docker01 system]#cp mysqld.service mysqld3309.service 

5、修改后的配置文件

[root@docker01 system]#grep "ExecStart" mysqld3307.service 

ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3307/my.cnf

[root@docker01 system]#grep "ExecStart" mysqld3308.service 

ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3308/my.cnf

[root@docker01 system]#grep "ExecStart" mysqld3309.service 

ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/data/3309/my.cnf

6、 启动数据库

systemctl start mysqld3307

systemctl start mysqld3308

systemctl start mysqld3309

7、查看端口

[root@docker01 3309]#netstat -nltp

Proto Recv-Q Send-Q Local Address   Foreign Address   State   PID/Program name    

tcp        0      0 0.0.0.0:22    0.0.0.0:*    LISTEN   982/sshd            

tcp6       0      0 :::3306        :::*             LISTEN   9710/mysqld         

tcp6       0      0 :::3307        :::*              LISTEN   10112/mysqld        

tcp6       0      0 :::3308         :::*              LISTEN   10149/mysqld        

tcp6       0      0 :::3309          :::*              LISTEN   10183/mysqld        

tcp6       0      0 :::22               :::*              LISTEN   982/sshd