Mysql master-slave replication built by docker-compose
Note the following points before setting:
1) It is necessary to ensure the network connection between the synchronization service period. That is, they can ping each other, and can use the authorization information of the other party to connect to the database of the other party (the firewall opens port 3306).
2) close selinux.
3) Before synchronization, the data that needs to be synchronized in the databases of both parties must be consistent. In this way, after the synchronization environment is realized, the updated data will be synchronized as scheduled. If the main library is a new library, ignore this step.
Create a directory
mkdir -p /usr/local/docker/mysqlMS
cd /usr/local/docker/mysqlMS
Write docker-compose.yml
version: '3.3'
services:
# 服务名, 主节点
mysql-master:
# 容器名
container_name: mysql-master
# mysql 镜像
image: mysql:5.7.31
restart: always
# 暴露端口号: 宿主机端口:容器内端口号
ports:
- 8306:3306
privileged: true
# 按照路径挂载目录: 日志、配置文件、数据
volumes:
- $PWD/msql-master/volumes/log:/var/log/mysql
- $PWD/msql-master/volumes/conf/my.cnf:/etc/mysql/my.cnf
- $PWD/msql-master/volumes/data:/var/lib/mysql
# 环境变量: mysql密码
environment:
MYSQL_ROOT_PASSWORD: "123456"
command: [
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_general_ci',
'--max_connections=3000'
]
# 使用的网卡
networks:
- myweb
# 从节点
mysql-slave:
container_name: mysql-slave
image: mysql:5.7.31
restart: always
ports:
- 8307:3306
privileged: true
volumes:
- $PWD/msql-slave/volumes/log:/var/log/mysql
- $PWD/msql-slave/volumes/conf/my.cnf:/etc/mysql/my.cnf
- $PWD/msql-slave/volumes/data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: "123456"
command: [
'--character-set-server=utf8mb4',
'--collation-server=utf8mb4_general_ci',
'--max_connections=3000'
]
networks:
- myweb
# 配置网卡
networks:
# 使用网卡,桥接模式
myweb:
driver: bridge
Create configuration folder
1. The main master configuration file my.cnf
vim msql-master/volumes/conf/my.cnf
[mysqld]
#D,默认是1,一般取IP最后一段
server-id=1
# [必须]启用二进制日志
log-bin=mysql-bin
# 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)# 确保binlog日志写入后与硬盘同步
sync_binlog = 1
# 跳过所有的错误,继续执行复制操作
slave-skip-errors = all
binlog-ignore-db=mysql
# 设置需要同步的数据库 binlog_do_db = 数据库名;
# 如果是多个同步库,就以此格式另写几行即可。
# 如果不指明对某个具体库同步,表示同步所有库。除了binlog-ignore-db设置的忽略的库
# binlog_do_db = test #需要同步test数据库。
2. From the slave configuration file my.cnf
vim msql-slave/volumes/conf/my.cnf
[mysqld]
#D,默认是1,一般取IP最后一段
server-id=2
# 如果想实现 主-从(主)-从 这样的链条式结构,需要设置:
# log-slave-updates 只有加上它,从前一台机器上同步过来的数据才能同步到下一台机器。
# 设置需要同步的数据库,主服务器上不限定数据库,在从服务器上限定replicate-do-db = 数据库名;
# 如果不指明同步哪些库,就去掉这行,表示所有库的同步(除了ignore忽略的库)。
# replicate-do-db = test;
# 不同步test数据库 可以写多个例如 binlog-ignore-db = mysql,information_schema
replicate-ignore-db=mysql
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=mysql-bin
log-bin-index=mysql-bin.index
## relay_log配置中继日志
#relay_log=edu-mysql-relay-bin
## 还可以设置一个log保存周期:
#expire_logs_days=14
# 跳过所有的错误,继续执行复制操作
slave-skip-errors = all
start service
root@haima-PC:/usr/local/docker/mysqlMM# docker-compose up -d
Creating network "mysqlms_myweb" with driver "bridge"
Creating mysql-master ... done
Creating mysql-slave ... done
Query service ip address
Get the network name mysqlms_myweb created by the service from the above information
docker network inspect mysqlms_myweb
found the result
mysql-master ip为192.168.112.3
mysql-slave ip为192.168.112.2
Enter the main mysql service
docker exec -it mysql-master bash
mysql -uroot -p123456
#查看server_id是否生效
mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id | 1 |
| server_id_bits | 32 |
+----------------+-------+
#看master信息 File 和 Position 从服务上要用
mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000005 | 154 | | mysql | |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
#开权限
mysql> grant replication slave,replication client on *.* to 'slave'@'%' identified by "123456";
mysql> flush privileges;
Enter slave service
docker exec -it mysql-slave bash
mysql -uroot -p123456
#查看server_id是否生效
mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name | Value |
+----------------+-------+
| server_id | 2 |
| server_id_bits | 32 |
+----------------+-------+
# 连接主mysql服务 master_log_file 和 master_log_pos的值要填写主master里查出来的值
change master to master_host='192.168.112.3',master_user='slave',master_password='123456',master_port=3306,master_log_file='mysql-bin.000005', master_log_pos=154,master_connect_retry=30;
#启动slave
mysql> start slave;
mysql> show slave status \G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.112.3
Master_User: slave
Master_Port: 3306
Connect_Retry: 30
Master_Log_File: mysql-bin.000004
Read_Master_Log_Pos: 617
Relay_Log_File: 7fee2f1fd5d2-relay-bin.000002
Relay_Log_Pos: 783
Relay_Master_Log_File: mysql-bin.000004
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: 617
Relay_Log_Space: 997
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: 1
Master_UUID: 8f6e9f5a-61f4-11eb-ac84-0242c0a86002
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.01 sec)
Connection main mysql parameter description:
**master_port**:Master的端口号,指的是容器的端口号
**master_user**:用于数据同步的用户
**master_password**:用于同步的用户的密码
**master_log_file**:指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值
**master_log_pos**:从哪个 Position 开始读,即上文中提到的 Position 字段的值
**master_connect_retry**:如果连接失败,重试的时间间隔,单位是秒,默认是60秒
As seen above, there are two Yes, indicating that it has been successful
Relay_Master_Log_File: mysql-bin.000004
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Set the slave server to read-only mode
Operate on slave server
SHOW VARIABLES LIKE '%read_only%'; #查看只读状态
SET GLOBAL super_read_only=1; #super权限的用户只读状态 1.只读 0:可写
SET GLOBAL read_only=1; #普通权限用户读状态 1.只读 0:可写
At this point, the setting has been successful, you can test it below, and the master-slave synchronization is already possible
Common operations on the slave server
stop slave;
start slave;
show slave status;
Reference: https://www.cnblogs.com/smallfa/p/15183605.html