1. 部署环境
系统版本:Centos7.9
服务器ip:192.168.5.152(mysql主),192.168.5.144(mysql从)
mysql版本:5.7
2. 部署mysql
2.1 部署master
2.1.1 创建mysql目录
mkdir –p /data/mysqlcd /data/mysqlmkdir confmkdir datamkdir log
2.1.2 创建mysql配置文件
vim conf/my.cnf[mysqld]# [必须]服务器唯一ID, 主库的server-id值必须比从库的小
server-id=104# [必须]启用二进制日志
log-bin=mysql-bin# 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)
binlog-ignore-db=mysql# 确保binlog日志写入后与硬盘同步
sync_binlog = 1# 跳过所有的错误,继续执行复制操作
slave-skip-errors = all# default
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0#log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
2.1.3 编写docker-compose文件
cd /data/mysqlvim mysql.yaml
version: '3'
services:mysql:container_name: mysql-masterimage: mysql:5.7restart: alwaysvolumes:- ./log:/var/log/mysql- ./conf/my.cnf:/etc/mysql/my.cnf- ./data:/var/lib/mysqlports:- '3306:3306'command: ['--explicit_defaults_for_timestamp=true','--character-set-server=utf8mb4','--collation-server=utf8mb4_general_ci','--max_connections=3000']environment:MYSQL_ROOT_PASSWORD: "shield"
2.1.4 启动mysql容器
cd /data/mysql
docker-compose -f mysql.yaml up –d
2.1.5 进入容器,创建同步账号,并开启权限
docker exec -it mysql-master bashmysql -uroot -pshield# 查看配置的服务ID,显示的应该是配置的IDmysql> show variables like '%server_id%';# 看master信息 File 和 Position 从服务上要用mysql> show master status;# 创建同步账户并开启权限,只是同步账号
mysql> create user 'sync'@'%' identified by '123456';
mysql> grant replication slave,replication client on *.* to 'sync'@'%';# 刷新信息生效
mysql> flush privileges;
2.2 部署slave
2.2.1 创建mysql目录
mkdir –p /data/mysqlcd /data/mysqlmkdir confmkdir datamkdir log
2.2.2 创建mysql配置文件
vim conf/my.cnf
[mysqld]
# [必须]服务器唯一ID
server-id=106# [必须]启用二进制日志
log-bin=mysql-bin# 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)
binlog-ignore-db=mysql# 确保binlog日志写入后与硬盘同步
sync_binlog = 1# 跳过所有的错误,继续执行复制操作
slave-skip-errors = all# relay_log配置中继日志
relay_log=mysql-relay-bin# log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1# default
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0#log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
2.2.3 编写docker-compose文件
cd /data/mysqlvim mysql.yaml
version: '3'
services:mysql:container_name: mysql-slaveimage: mysql:5.7restart: alwaysvolumes:- ./log:/var/log/mysql- ./conf/my.cnf:/etc/mysql/my.cnf- ./data:/var/lib/mysqlports:- '3306:3306'command: ['--explicit_defaults_for_timestamp=true','--character-set-server=utf8mb4','--collation-server=utf8mb4_general_ci','--max_connections=3000']environment:MYSQL_ROOT_PASSWORD: "shield"
2.2.4 启动mysql容器
cd /data/mysql
docker-compose -f mysql.yaml up –d
2.2.5 进入容器,连接mysql主服务docker exec -it mysql-slave bashmysql -uroot -pshield#查看server_id是否生效
mysql> show variables like '%server_id%';# 连接主mysql服务 master_log_file 和 master_log_pos的值要填写主master里查出来的值 注意这里使用的docker-compose 内部服务的端口和ip,实际应该为真实的master_host,参数看后面的介绍
mysql> change master to master_host='192.168.5.152',master_user='sync',master_password='123456',master_port=3306,master_log_file='mysql-bin.000004', master_log_pos=313,master_connect_retry=30;# 开启slave
mysql> start slave;mysql> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.5.152Master_User: syncMaster_Port: 3306Connect_Retry: 30Master_Log_File: mysql-bin.000004Read_Master_Log_Pos: 313Relay_Log_File: mysql-relay-bin.000007Relay_Log_Pos: 526Relay_Master_Log_File: mysql-bin.000004Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 313Relay_Log_Space: 899Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 104Master_UUID: 5f25a087-8379-11ee-8a89-0242ac120002Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)看到下列信息代表链接成功
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
至此docker-compose部署mysql5.7主从完毕。
3. 主从切换
3.1停止原主mysql
docker stop mysql-master
3.2停止从数据库的复制线程
mysql> stop slave;
mysql> reset slave all;
3.3 将从数据库升为主数据库
mysql> reset master;
3.4 原主数据库降为从数据库
mysql> CHANGE MASTER TO MASTER_HOST = '192.168.5.144', MASTER_USER = 'sync', MASTER_PASSWORD = '123456';mysql> start slave;mysql> show slave status \G
*************************** 1. row ***************************Slave_IO_State: Waiting for master to send eventMaster_Host: 192.168.5.144Master_User: syncMaster_Port: 3306Connect_Retry: 60Master_Log_File: mysql-bin.000001Read_Master_Log_Pos: 304Relay_Log_File: e75962e1de15-relay-bin.000007Relay_Log_Pos: 517Relay_Master_Log_File: mysql-bin.000001Slave_IO_Running: YesSlave_SQL_Running: YesReplicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0Last_Error: Skip_Counter: 0Exec_Master_Log_Pos: 304Relay_Log_Space: 731Until_Condition: NoneUntil_Log_File: Until_Log_Pos: 0Master_SSL_Allowed: NoMaster_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0
Master_SSL_Verify_Server_Cert: NoLast_IO_Errno: 0Last_IO_Error: Last_SQL_Errno: 0Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 106Master_UUID: 616899ae-837e-11ee-b615-0242ac120002Master_Info_File: /var/lib/mysql/master.infoSQL_Delay: 0SQL_Remaining_Delay: NULLSlave_SQL_Running_State: Slave has read all relay log; waiting for more updatesMaster_Retry_Count: 86400Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version:
1 row in set (0.00 sec)
至此手动切换mysql主从完成。