Docker配置原理跟通常的master-salve配置一样,使用Docker可快速实现主从复制的环境搭建。
目录
1.更新系统并安装docker
yum update
yum install docker
2.服务器开启端口访问
#添加端口访问
firewall-cmd --zone=public --add-port=3366/tcp --permanent
firewall-cmd --zone=public --add-port=3367/tcp --permanent
#重载防火墙
firewall-cmd --reload
#开启端口列表
firewall-cmd --zone=public --list-ports
#移除端口
firewall-cmd --zone=public --remove-port=22/tcp --permanent
3.运行Master和Slave实例
需要保证MySQL数据目录映射和配置方便
docker run -p 3366:3306 --name master -v /usr/local/mysql-master-salve/master/my.cnf:/etc/mysql/my.cnf -v /usr/local/mysql-master-salve/master/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
docker run -p 3367:3306 --name slave -v /usr/local/mysql-master-salve/slave/my.cnf:/etc/mysql/my.cnf -v /usr/local/mysql-master-salve/slave/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root -d mysql:5.7
4.进入容器修改主从配置
4.1容器进入操作
#进入容器
docker exec -it master /bin/bash
docker exec -it slave /bin/bash
#进入mysql配置目录
cd /etc/mysql
#编辑my.cnf vi命令不支持需要安装 apt-get install vim(因无法保存编辑文件所以采用-v映射my.cnf)
4.2Master配置
[mysqld]
## 同一局域网内注意要唯一
server-id=10000
## 开启二进制日志功能,可以随便取(关键)
log-bin=mysql-bin
4.3Slave配置
[mysqld]
## 设置server_id,注意要唯一
server-id=10001
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-slave-bin
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin
5.主从关联
5.1获取容器实例的IP
#获取容器实例IP
docker inspect --format='{{.NetworkSettings.IPAddress}}' master
172.17.0.2
docker inspect --format='{{.NetworkSettings.IPAddress}}' slave
172.17.0.3
5.2mysql-master操作
mysql>grant all privileges on *.* to 'root'@'172.17.0.3' identified by 'root';
flush privileges;
#查询master的状态
mysql>show master status;
5.3mysql-slave操作
slave与master关联需要用到File和Position。
mysql>stop slave;
mysql>change master to master_host='172.17.0.2', master_user='root', master_password='root', master_log_file='mysql-bin.000005', master_log_pos=595;
mysql>start slave;
#查询slave的状态
mysql>show salve status;
5.4结果验证
#查询slave的状态格式化显示
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 172.17.0.2
Master_User: root
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000005
Read_Master_Log_Pos: 796
Relay_Log_File: edu-mysql-relay-bin.000002
Relay_Log_Pos: 521
Relay_Master_Log_File: mysql-bin.000005
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB:
Replicate_Ignore_DB:
Replicate_Do_Table:
Replicate_Ignore_Table:
Replicate_Wild_Do_Table:
Replicate_Wild_Ignore_Table:
Last_Errno: 0
Last_Error:
Skip_Counter: 0
Exec_Master_Log_Pos: 796
Relay_Log_Space: 732
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_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: No
Last_IO_Errno: 0
Last_IO_Error:
Last_SQL_Errno: 0
Last_SQL_Error:
Replicate_Ignore_Server_Ids:
Master_Server_Id: 10000
Master_UUID: 3dd5a53a-6275-11e9-934c-0242ac110002
Master_Info_File: /var/lib/mysql/master.info
SQL_Delay: 0
SQL_Remaining_Delay: NULL
Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates
Master_Retry_Count: 86400
Master_Bind:
Last_IO_Error_Timestamp:
Last_SQL_Error_Timestamp:
Master_SSL_Crl:
Master_SSL_Crlpath:
Retrieved_Gtid_Set:
Executed_Gtid_Set:
Auto_Position: 0
Replicate_Rewrite_DB:
Channel_Name:
Master_TLS_Version:
1 row in set (0.00 sec)
ERROR:
No query specified
mysql>
只要看到如下两点就标识主从配置成功:
-
Slave_IO_Running: Yes
-
Slave_SQL_Running: Yes
查看主从复制测试(master创建一个service_db,slave同步service_db):
数据备份映射路径数据查看:
slave同上。
6.异常处理
6.1容器不能运行
参考:https://www.cnblogs.com/wang--lei/p/10626308.html
6.2Navicat打开mysql慢
在my.cnf中加入如下配置:
[mysqld]
#解决Navicat打开很慢问题
skip-name-resolve