1: The process of realizing the master-slave replication was wading through the pits, and then I decided to record it after I did it myself several times and succeeded. (So some screenshots and contexts do not match, such as docker containers. The name does not match, everyone can use their own), I plan to implement master-slave replication from the installation of docker on centos8 to the end, so I uninstalled the original docker and recorded it from the beginning. If you have docker, you can skip it Follow the steps to uninstall docker.
2: It is recommended that friends who have read the post do not rush to get started, and read it first before getting started.
3: If the description is wrong, please feel free to enlighten me. Find me v:star2020082220210611
uninstall docker
1: Delete the docker container
sudo docker stop $(docker ps -aq)
Note: This command will stop all containers, including running containers and stopped containers
2: Delete all containers:
sudo docker rm $(docker ps -aq)
Note: Since I only have one docker here, and the command docker rm container id/container name has been executed to delete the container. So no pics here
3: Delete all mirrors (I have a mirror of mysql5.7 here, but the process of deletion takes a bit of time, wait for a while)
sudo docker rmi $(docker images -q)
Note: This command will delete all mirrors, including running mirrors and stopped mirrors; when executing this command, the command line will give a second confirmation prompt y/n, select y to continue the delete operation. The second interface above appears to indicate that the deletion is complete.
4: Uninstall docker
sudo yum remove docker-ce docker-ce-cli containerd.io
5: Delete the docker data directory
sudo rm -rf /var/lib/docker
Note: This command will delete the Docker data directory, including all containers, images, and data volumes.
The above operations are because I want to record from the beginning to the end from the installation of docker. I lay in the pit several times during the construction process, and I deleted it several times after the construction was successful. Repeated experiments can achieve master-slave replication, so record it . If you don’t need to delete docker, you can start directly from the following steps, hoping to help other friends.
Next, start by installing the docker image in centos.
First install the docker image in centos:
1: Update the data source
sudo yum update
2: Install some tools and dependent libraries needed for docker runtime, because these centos8 are not complete and need to be installed
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
3: Download the docker package
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
4: Install the docker community version: (This process also needs to wait for a while)
sudo yum install -y docker-ce
5: Set up the docker service and start it
sudo systemctl enable docker
sudo systemctl start docker
Note: It can also be executed together: sudo systemctl enable docker &&sudo systemctl start docker
6: Query docker version
sudo docker --version
So far docker has been installed, next is mysql
Pull the mysql image, here is mysql5.7 version as an example
1: Pull the mysql5.7 image
docker pull mysql:5.7
Note: This process needs to wait for Ha
2: View the mirror image
docker images
3: Create a mysql container casually, the main purpose here is to copy the mysql configuration file, because I want to mount the mysql configuration file my.cnf for easy modification
docker run -d --name master mysql:5.7
Note: Here is to create a mysql container and run it. If you need to view the mysql configuration file my.cnf, you need to execute the following command. If you don’t need it, you can skip it.
Step 1: Create a container and start it
docker run -d -p 3333:3306 --name master -e MYSQL_ROOT_PASSWORD=123456 docker.io/mysql:5.7
Explain the meaning:
docker run -d -p 3333:3306 --name master -e MYSQL_ROOT_PASSWORD=123456 docker.io/mysql:5.7
这个 docker 命令用于在 docker 中运行一个 MySQL 容器,以下是命令中各个参数的含义:
docker run:用于在 docker 中运行一个容器的命令。
-d:表示在后台(detached 模式)运行容器,即不在终端上显示容器的输出。
-p 3333:3306:将主机(Host)的 3333 端口映射到容器的 3306 端口。这意味着你可以通过主机的 3333 端口访问容器中的 MySQL 服务。
--name master:指定容器的名称为 "master"。
-e MYSQL_ROOT_PASSWORD=123456:设置 MySQL 根用户的密码为 "123456"。这里使用环境变量来传递密码给容器内的 MySQL 服务。
docker.io/mysql:5.7:表示使用 Docker Hub 上的 mysql 镜像,并指定标签为 5.7。
综合起来,这个命令的作用是在 Docker 中创建一个 MySQL 5.7 版本的容器,
容器名称为 "master",
通过主机的 3333 端口访问 MySQL 服务,并设置根用户密码为 "123456"。
容器在后台运行,不在终端上显示输出。
Step 2: Enter the container's shell
docker exec -it master bash
Step 3: View the default my.cnf configuration file
ls /etc/my.cnf
cat /etc/my.cnf
或者是
ls /etc/mysql/my.cnf
cat /etc/mysql/my.cnf
4: Create and start docker to start creating a folder, copy in the default configuration file of mysql, then modify the configuration, and mount it out, so that we can modify the configuration file of mysql.
mkdir mysql // home文件下创建mysql文件
mkdir /home/mysql/master // mysql文件夹下创建主库文件夹master
mkdir /home/mysql/slave // mysql文件夹下创建从库文件夹slave
5: Copy the configuration file to the my.cnf configuration file of the master-slave file created above
Main folder:
docker cp master:/etc/my.cnf /home/mysql/master
From folder:
docker cp master:/etc/my.cnf /home/mysql/slave
Check whether the copy is successful
6: Delete the above running container
docker ps -a //查看
docker stop master //停止 ,test是我上面创建的容器的名字
docker rm master //删除
7: Start the main container
docker run -d \
--restart=always \
-p 3306:3306 \
-v /home/mysql/master/my.cnf:/etc/my.cnf \
-e MYSQL_ROOT_PASSWORD=123456 \
--name master mysql:5.7
The meaning of each parameter in the above docke command:
docker run:用于在 Docker 中运行一个容器的命令。
-d:表示在后台(detached 模式)运行容器,即不在终端上显示容器的输出。
-p 3333:3306:将主机(Host)的 3333 端口映射到容器的 3306 端口。这意味着你可以通过主机的 3333 端口访问容器中的 MySQL 服务。
--name master:指定容器的名称为 "master"。
-e MYSQL_ROOT_PASSWORD=123456:设置 MySQL 根用户的密码为 "123456"。这里使用环境变量来传递密码给容器内的 MySQL 服务。
docker.io/mysql:5.7:表示使用 Docker Hub 上的 mysql 镜像,并指定标签为 5.7。
8: Enter the configuration file of the main library and add the following content under the file [mysqld], save and exit.
vim /home/mysql/master/my.cnf
## 设置server_id, 同一个局域网中需要唯一
server_id=101
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql,information_schema
## 开启二进制日志功能
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
9: Restart the main library container
docker restart master
Note: Make sure that the master library is started successfully before continuing with the following steps, otherwise an error will be reported when logging in to mysql, prompting that the username and password are incorrect, as shown in the figure. You can view the log. When the red box in Figure 2 appears, it means that the startup is successful. At this time, continue the following steps, and no error will be reported when you log in to mysql.
docker logs master //查看容器名为master的日志
10: Create data synchronization in the main library, which is divided into the following steps
Step 1: mysql container
docker exec -it master bash
Step 2: Login to mysql
mysql -uroot -p123456
Step 3: Grant permissions to a user named "slave" to replicate data from the master database
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123456';
Specific meaning:
GRANT REPLICATION SLAVE:授予从数据库复制数据的权限。
ON *.*:将权限应用于所有数据库和所有表。
TO 'slave'@'%':授予 "slave" 用户在任何主机上的访问权限。'%' 表示任意主机。
IDENTIFIED BY '123456':设置用户 "slave" 的密码为 "123456"。
Step 4: Grant the user named "slave" the permissions needed to copy data from the database and perform some client operations
grant replication slave, replication client on *.* to 'slave'@'%';
Specific meaning:
grant replication slave:授予从数据库复制数据的权限。
replication client:授予从数据库执行一些客户端操作的权限,例如读取主数据库的二进制日志信息。
on *.*:将权限应用于所有数据库和所有表。
to 'slave'@'%':授予 "slave" 用户在任何主机上的访问权限。'%' 表示任意主机
Step 5: Refresh
flush privileges;
Step 6: View the master-slave synchronization status record master_log_file and master_log_pos in this status, the values of these two parameters need to be remembered and will be used later
show master status;
Then ctrl+d to exit.
Note: The above steps can also be operated in navicat.
1: Create a new mysql link to log in to the database
2: Execute commands (execute in order)
GRANT REPLICATION SLAVE ON *.* TO 'slave'@'%' IDENTIFIED BY '123456';
grant replication slave, replication client on *.* to 'slave'@'%';
flush privileges;
show master status;
11: Start the slave container
docker run -d \
--restart=always \
-p 3307:3306 \
-v /home/mysql/slave/my.cnf:/etc/my.cnf \
-e MYSQL_ROOT_PASSWORD=123456 \
--name slave mysql:5.7
12: Modify the slave library configuration file, and add the following content under [mysqld]:
vim /home/mysql/slave/my.cnf
Add the following to the file [mysqld]:
## 设置server_id, 同一个局域网内需要唯一
server_id=102
## 指定不需要同步的数据库名称
binlog-ignore-db=mysql,information_schema
## 开启二进制日志功能,以备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
13: Restart the slave container
docker restart slave
14: Create a data synchronization user from the library, which is divided into the following steps
Step 1: Enter the mysql container
docker exec -it slave bash
Step 2: Login to mysql
mysql -uroot -p123456
Step 3: Configure the connection and synchronization information of the slave database in MySQL master-slave replication, which tells the slave database how to connect to the master database to obtain data replication updates.
CHANGE MASTER TO master_host = '192.168.0.25',
master_user = 'slave',
master_password = '123456',
master_port = 3306,
master_log_file = 'mall-mysql-bin.000001',
master_log_pos = 807,
master_connect_retry = 30;
The meaning of each parameter in the above paragraph of SQL:
master_host = '192.168.0.25':这是主数据库的 IP 地址或主机名。从数据库将连接到该主数据库来获取复制数据。
master_user = 'slave':这是连接主数据库所使用的用户名。通常创建一个用于复制的专用用户。
master_password = '123456':连接主数据库所使用的密码,与上述用户名对应。
master_port = 3306:主数据库的端口号,通常为 MySQL 默认的端口号 3306。
master_log_file = 'mall-mysql-bin.000001':这是主数据库二进制日志中的日志文件名,它包含复制数据的更新记录。
master_log_pos = 807:这是主数据库二进制日志文件中的位置,指示从数据库在何处开始读取二进制日志以进行复制。
master_connect_retry = 30:在连接主数据库失败时,从数据库会尝试重新连接的时间间隔(以秒为单位)。
总体而言,这些配置参数告诉从数据库应该连接到哪个主数据库,使用哪个用户和密码来获取复制数据,并从主数据库的指定二进制日志位置开始同步数据。这是主从复制配置的一部分,使得从数据库可以与主数据库保持数据同步。在执行 CHANGE MASTER TO 后,从数据库可以开始运行复制进程,从主数据库同步数据更新。
13: Check the status. At this time, Slave_IO_Running and Slave_SQL_Running are both No
show slave status \G;
14: Enable master-slave synchronization
start slave;
15: Check the master-slave synchronization status again. At this time, Slave_IO_Running and Slave_SQL_Running are both Yes
show slave status \G;
Note: The above steps can also be performed in navicat, the steps are as follows:
Step 1: Create a new mysql link to log in to the database
Step 2: Configure the connection and synchronization information of the slave database in MySQL master-slave replication
CHANGE MASTER TO master_host = '192.168.0.25',
master_user = 'slave',
master_password = '123456',
master_port = 3306,
master_log_file = 'mall-mysql-bin.000001', //与主库中一致
master_log_pos = 1444, // 与主库中一致
master_connect_retry = 30;
Step 3: Check the master-slave synchronization status# At this time, Slave_IO_Running and Slave_SQL_Running are both No
show slave status;
Step 4: Turn on master-slave synchronization
start slave;
Step 5: Check the master-slave synchronization status again# At this time, Slave_IO_Running and Slave_SQL_Running are both Yes
show slave status;
16: When Slave_IO_Running and Slave_SQL_Running of the slave library are both Yes, it means that the master-slave configuration is successful. Next, you can create a link in nacivat. After the connection is successful, create a database and insert data. You can verify whether the master-slave is synchronized. In the command line, I take nacivat as an example to create a library, table, insert data, and see if it is synchronized
Verification 1: Whether the library is synchronized
Create a new database in the main database:
refresh the test from the database and then check:
Conclusion: It also exists in the slave database Created a test library
Verification 2: Whether the table is synchronized
Create a table in the main library
Refresh from the library and look again:
conclusion: the user table also exists in the slave library
Verification 3: Whether the data is synchronized
Refresh from the library and look again:
Conclusion: The data in the user table also exists in the library
. At this point, even if it is over, please correct me if the description is wrong.