Docker implements mysql master-slave replication (huge details!!!)

Create a new host service container instance 3307

docker run -p 3307:3306 --name mysql-master \
-v /mydata/mysql-master/log:/var/log/mysql \
-v /mydata/mysql-master/data:/var/lib/mysql \
-v /mydata/mysql-master/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7

This code is the command used to start a MySQL 5.7 database container named mysql-master in Docker and map it to the host's 3307 port. The specific parameter meanings are as follows:

  • docker run: Docker command, used to create and start Docker containers.
  • -p 3307:3306: Map the 3306 port of the Docker container to the 3307 port of the host.
  • --name mysql-master: Specify a name for the Docker container.
  • -v /mydata/mysql-master/log:/var/log/mysql: Mount the directory /mydata/mysql-master/log on the host to the /var/log/mysql directory of the Docker container, which is used to store the database log files in the container.
  • -v /mydata/mysql-master/data:/var/lib/mysql: Mount the directory /mydata/mysql-master/data on the host to the /var/lib/mysql directory of the Docker container, which is used to store database files in the container.
  • -v /mydata/mysql-master/conf:/etc/mysql: Mount the directory /mydata/mysql-master/conf on the host to the /etc/mysql directory of the Docker container, which is used to store the MySQL configuration file in the container.
  • `-e MYSQL_ROOT_PASSWORD=root: Set the root user password of the MySQL database in the container to root.
  • -d mysql:5.7: Use the mysql:5.7 image in Docker Hub to create a container and run it in the background.

Based on the above parameters, this command will create a MySQL 5.7 database container named mysql-master and map it to port 3307 of the host. At the same time, the log files, data files and configuration files of the database in the container will be mounted to the specified host. Directory. The root user password of the MySQL database in the container will be set to ROOT. The command runs in the background and returns the container's ID.

Enter the /mydata/mysql-master/conf directory and create my.cnf

cd /mydata/mysql-master/conf
vim my.cnf

[mysqld]
##同一局域网中需要唯一
server_id=101
##指定不需要同步的数据库每个in成
binlogn-ignore-db=mysql
##开启二进制日志功能
log_bin=mall-mysql-bin
##设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
##设置使用的二进制日志格式(mixed,statement,row)
binlog_format=mixed
##二进制过期清理时间。默认值为0,表示不自动清理
expire_logs_days=7
##跳过主从复制中遇到的所有错误或之慈宁宫类型的错误,避免slave端复制中断
##如:1062错误是指一些主键重复,1032是指因为主从数据库数据不一致
slave_skip_errors=1062

After modifying the configuration, restart the master instance

docker restart mysql_master

Enter the mysql-master container

docker exec -it mysql_master /bin/bash
mysql-uroot-proot
  • mysql: Start the MySQL client program.
  • -uroot: Specify the connected user name as root.
  • -proot: Specify the password for the connection to be root.

Create a data synchronization user in the master container instance

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

These are two MySQL database commands used to create a user named 'slave' and grant that user permissions on the replication slave and replication client. The specific command meanings are as follows:

  1. CREATE USER 'slave'@'%' IDENTIFIED BY '123456';
    • CREATE USER: Create a new user.
    • 'slave'@'%': Username and hosts allowed to connect. Here, the username is 'slave' and '%' means allowing connections from any host.
    • IDENTIFIED BY '123456': Set the user's password to '123456'.
  2. GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'slave'@'%';
    • GRANT: Grant permissions to specified users.
    • REPLICATION SLAVE, REPLICATION CLIENT: Grant permissions to the replication slave and replication client.
    • *.*: Databases and tables to be authorized. Here, *.*all databases and tables are represented.
    • TO 'slave'@'%': Users granted permissions and hosts allowed to connect.

Combining the above commands, the first command creates a user named 'slave' and sets its password to '123456', allowing connections from any host. The second command grants the user permissions on the replication slave database and replication client, and authorizes all databases and tables. In this way, user 'slave' can connect to the MySQL master server as a replication slave and perform data replication operations.

Create a new server container instance 3308

docker run -p 3308:3306 --name mysql_slave \
-v /mydata/mysql_slave/log:/var/log/mysql \
-v /mydata/mysql-slave/data:/var/lib/mysql \
-v /mydata/mysql-slave/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7

Enter the /mydata/mysql-slave/conf directory and create my.cnf

cd /mydata/mysql-slave/conf
vim my.cnf

[mysqld]
##设置server_id,同一局域网需要唯一
server_id=102
##指定不需要同步的数据库名称
binlog-ignore-db=mysql
##开启二进制日志功能,以备slave作为其他数据库实例的master时使用
log-bin=mall-mysql-slave1-bin
##设置二进制日志使用内存大小(事务)
binlog_cache_size=1M
##设置使用的二进制日志模式(mixed,statement,row)
binlog_format=mixed
##二进制日志过期清理时间。默认值为0,表示不自动清理
expire_logs_days=7
##跳过主从复制中遇到的所有错误或制动类型的错误,避免slave端复制中断
##如:1062错误是指一下主键重复,1032错误是因为主从数据不一致
slave_skip_errors=1062
##relay_log配置中继日志
relay_log=mall-mysql-relay-bin
##log_slave_updates表示slave将表示复制事件写进自己的二进制日志
log_slave_updates=1
##slave设置为只读(具有super权限的用户除外)
read_only-1

After modifying the configuration, restart the slave instance

docker restart mysql-slave

View sync status in master database

show master status;

Enter the mysql-slave container

docker exec -it mysql-slave /bin/bsh
mysql -uroot -proot

Configure master-slave in slave database

change master to master_host='宿主机ip',master_user='slave',master_password='123456',master_port=3307,master_log_file='mall-mysql-bin,000001',master_log_pos=617,master_connect_retry=30;
  • CHANGE MASTER TO`: Specifies to modify the master-slave replication configuration.
  • MASTER_HOST='宿主机ip': Set the IP address (or host name) of the main server.
  • MASTER_USER='slave': Set the username of the main server.
  • MASTER_PASSWORD='123456': Set the password of the main server.
  • MASTER_PORT=3307: Set the port number of the main server.
  • MASTER_LOG_FILE='mall-mysql-bin.000001': Set the binary log file name of the main server.
  • MASTER_LOG_POS=617: Set the binary log location of the master server.
  • MASTER_CONNECT_RETRY=30: Set the interval for the slave server to retry connecting to the master server (unit: seconds).

By executing this command, you configure the slave server to connect to the specified master server and use the specified credentials for authentication and replication operations. Be sure to replace '宿主机ip', 'slave', '123456', 'mall-mysql-bin.000001'and 617with the correct master server information.

Check the master-slave synchronization status in the slave database

show slave status \G;

Insert image description here

Enable master-slave synchronization in the slave database

start slave;

Check the status of the database and find that it has been synchronized

Insert image description here

Master-slave replication test

Create a new library on the host - use the library - create a new table - insert data, ok
Insert image description here

Use the library from the machine - view the record, ok

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_49750432/article/details/133252679