The construction of master-slave replication of mysql database in docker environment

The construction of master-slave replication of mysql database in docker environment

1. Prepare two mysql servers

Install the mysql master database on the first server using the dicker command:

docker run --name mysql_master -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest

Command explanation:

通过镜像 mysql:latest 启动一个名为 mysql_master 的 MySQL 服务器,
端口号是3306,
映射的宿主机端口号是3306,
root 账号密码是123456

image-20210813164653463

Use the same command to install the slave database:

docker run --name mysql_slave -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:latest

View container installation: docker ps -a

image-20210813182923732

Go inside the container and modify the configuration file –my.cnf:

docker exec -it a448f237b877 /bin/bash
#进入到文件夹中
cd /etc/mysql
# 修改配置文件
vi my.cnf

#如果出现报错:vim: command not found
#则先安装编辑插件:先执行:
apt-get update
#再执行
apt-get install vim


Modify the configuration file:

[mysqld]
## 设置server_id,一般设置为IP,同一局域网内注意要唯一
server_id=100  
## 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)
binlog-ignore-db=mysql  
## 开启二进制日志功能,可以随便取,最好有含义(关键就是这里了)
log-bin=edu-mysql-bin  
## 为每个session 分配的内存,在事务过程中用来存储二进制日志的缓存
binlog_cache_size=1M  
## 主从复制的格式(mixed,statement,row,默认格式是statement)
binlog_format=mixed  
## 二进制日志自动删除/过期的天数。默认值为0,表示不自动删除。
expire_logs_days=7  
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062  

Exit and restart the mysql container

dockers restart mysql_master

Create a data synchronization user in the primary database:

CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';   

2. Configure the slave server

Also modify the configuration file –my.cnf

[mysqld]
## 设置server_id,一般设置为IP,注意要唯一
server_id=101  
## 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)
binlog-ignore-db=mysql  
## 开启二进制日志功能,以备Slave作为其它Slave的Master时使用
log-bin=edu-mysql-slave1-bin  
## 为每个session 分配的内存,在事务过程中用来存储二进制日志的缓存
binlog_cache_size=1M  
## 主从复制的格式(mixed,statement,row,默认格式是statement)
binlog_format=mixed  
## 二进制日志自动删除/过期的天数。默认值为0,表示不自动删除。
expire_logs_days=7  
## 跳过主从复制中遇到的所有错误或指定类型的错误,避免slave端复制中断。
## 如:1062错误是指一些主键重复,1032错误是因为主从数据库数据不一致
slave_skip_errors=1062  
## relay_log配置中继日志
relay_log=edu-mysql-relay-bin  
## log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1  
## 防止改变数据(除了特殊的线程)
read_only=1  

3. Complete the Master and Slave links

Note that it is necessary to ensure that the data in other databases must be consistent between the Master and Slave except for the databases that are not synchronized.
Enter MySQL on the Master, and then execute the command:

show master status;

image-20210813185749805

Record the values ​​of the File and Position fields, which will be used later.

Then go to mysql in Slave and execute the command:

change master to master_host='10.10.246.13', master_user='slave', master_password='123456', master_port=3306, master_log_file='edu-mysql-bin.000001', master_log_pos=156, master_connect_retry=30;  

Command explanation:

master_host: Master 的IP地址
master_user:Master 中授权的用于数据同步的用户
master_password: 同步数据的用户的密码
master_port: Master 的数据库的端口号
master_log_file: 指定 Slave 从哪个日志文件开始复制数据,即上文中提到的 File 字段的值
master_log_pos: 从哪个 Position 开始读,即上文中提到的 Position 字段的值
master_connect_retry: 当重新建立主从连接时,如果连接失败,重试的时间间隔,单位是秒,默认是60秒。

Execute on the MySQL terminal of the Slave to view the master-slave synchronization status:

show slave status \G;

result:

image-20210813190342406

SlaveIORunning and SlaveSQLRunning are No, indicating that the Slave has not started the replication process. On the contrary, SlaveIORunning and SlaveSQLRunning are Yes, indicating that they have started to work, because I have already run them, so my display is Yes.

Execute the following command to start master-slave synchronization:

start slave;

Result: SlaveSQLRunning becomes yes

image-20210813190704477

You're done!

4. Actual test

The last step is to test whether it can be synchronized. There are many testing methods. You can add a database to the Master, and then go to the Slave to check whether it has been synchronized. If not, please check your configuration file and configuration process carefully.

After creating a new database on the master, a new database is created on the slave at the same time, and the configuration is successful:

image-20210813191737937

Guess you like

Origin blog.csdn.net/SHUKAI618/article/details/119721201