Docker----build mysql master-slave replication based on docker, delayed replication

Docker-based Mysql master-slave replication construction

  • Limited resources
  • Building a virtual machine requires machine configuration, and the steps to install mysql are cumbersome
  • Multiple Docker containers can run on one machine
  • Docker containers are independent of each other, have independent ip, and do not conflict with each other
  • Docker is easy to use, and the container can be started in seconds

Because I built it locally, this time I will use the docker intranet address for master-slave connection

master slave
ip: 172.17.0.2 ip: 172.17.0.3
  • You can use docker inspect 容器名to view the docker address

1. Use Docker to build a master-slave server

1. The first step is to create a simple container and copy the /etc/mysql folder in the container to the local

  • I changed the copied mysql folder to the config folder and placed it in my local /docker/mysql-master/folder
docker run --name mysql -d registry.cn-beijing.aliyuncs.com/qianjia2018/qianjia_dev:mysql5.7.21

2. Build a complete mysql container
1.mysql-master (main):

docker run -d --name mysql-master --restart always -e MYSQL_ROOT_PASSWORD=123456 -v /docker/mysql-master/config/:/etc/mysql -v /docker/mysql-master/data/:/var/lib/mysql -p 3316:3306 registry.cn-beijing.aliyuncs.com/qianjia2018/qianjia_dev:mysql5.7.21

2.mysql-slave (from):

docker run -d --name mysql-slave --restart always -e MYSQL_ROOT_PASSWORD=123456 -v /docker/mysql-slave/config/:/etc/mysql -v /docker/mysql-slave/data/:/var/lib/mysql -p 3317:3306 registry.cn-beijing.aliyuncs.com/qianjia2018/qianjia_dev:mysql5.7.21

2. Configure the master (master)

1. Modify the mysqld.cnf file of the local /docker/mysql-master/config/mysql.conf.d

  • Corresponding to the /etc/mysql/mysql.conf.d/mysqld.cnf file in the container
[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
#log-error	= /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address	= 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

# 开启二进制日志功能
log-bin=mysql-bin
# 定义mysql支持的语法类型
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

# 一般设置为IP,同一局域网内注意要唯一
server_id=1
# 复制过滤:也就是指定哪个数据库不用同步(mysql库一般不同步)
binlog-ignore-db=mysql
# log_slave_updates表示slave将复制事件写进自己的二进制日志
log_slave_updates=1

[mysql_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

2. After the configuration is complete, restart mysql and use the following command:

docker restart mysql-master

3. Next, create a data synchronization user:
1. Enter the database:

docker exec -it mysql-master bash

2. Log in to the database:

mysql> mysql -u root -p

3. Add mysql master-slave account:

mysql> grant replication slave on *.* to repl@'172.17.0.%' identified by '123456';
 mysql> flush privileges;

4. Check the log_bin option:
log_bin is on means open

mysql> show variables like '%log_bin%';
+---------------------------------+--------------------------------+
| Variable_name                   | Value                          |
+---------------------------------+--------------------------------+
| log_bin                         | ON                             |
| log_bin_basename                | /var/lib/mysql/mysql-bin       |
| log_bin_index                   | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF                            |
| log_bin_use_v1_row_events       | OFF                            |
| sql_log_bin                     | ON                             |
+---------------------------------+--------------------------------+
6 rows in set (0.00 sec)

5. View master status:

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |     1607 |              | mysql            |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
  • Next, the value in the master state will be used for master-slave connection

3. Configure slave (slave)

1. Modify the configuration file:

[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
#log-error	= /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address	= 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#开启二进制日志功能
log-bin=mysql-slave-bin
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

server_id=2
binlog-ignore-db=mysql
log_slave_updates=1

[mysql_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
  • Restart the database after saving

2. Enter to configure the master-slave connection from mysql

1. Master-slave synchronous connection

mysql> change master to master_host='172.17.0.2', master_user='repl', master_password='123456', master_port=3306, master_log_file='edu-mysql-bin.000002', master_log_pos=1607, master_connect_retry=30;  

Command explanation:

master_host: Master 的IP地址
master_user: 在 Master 中授权的用于数据同步的用户
master_password: 同步数据的用户的密码
master_port: Master 的数据库的端口号
master_log_file: 指定 Slave 从哪个日志文件开始复制数据,即上文中提到的master状态中的 File 字段的值
master_log_pos: 从哪个 Position 开始读,即上文中提到的 Position 字段的值
master_connect_retry: 当重新建立主从连接时,如果连接失败,重试的时间间隔,单位是秒,默认是60秒。
  • As mentioned above, because I build it locally, I use the docker intranet address to connect, and the port uses the default 3306. If you want to use the ip address to connect, please replace it with the ip address and the port master_host='172.17.0.2'. It is enough to become the port mapped by the host machine.

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

show slave status \G;

Shown below, the simple part:

mysql> show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.17.0.2
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 30
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 1339
               Relay_Log_File: 17ca0a0f1e57-relay-bin.000002
                Relay_Log_Pos: 856
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: No
            Slave_SQL_Running: No
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
  • Slave_IO_Running and Slave_SQL_Running are No, indicating that the Slave has not started the replication process. On the contrary, Slave_IO_Running and Slave_SQL_Running are Yes, indicating that it has started to work, because I have already run it, so my display is Yes.

3. Execute the following command to start master-slave synchronization:

start slave;
  • Check again after execution and find that SlaveIORunning and SlaveSQLRunning have changed to Yes, which proves success.

4. Verify
1.master operation, create a test database, and usertb table, and insert a row of data

mysql> create database test;
mysql> use test;
mysql> create table usertb ( username varchar(20) not null, password varchar(20) not null);
mysql> insert into usertb values ('user1','111');
mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 111      |
+----------+----------+

2. Slave operation, the query data is synchronized normally

mysql> select * from test.usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 111      |
+----------+----------+
1 row in set (0.00 sec)

3. Delayed replication configuration

  • Delayed replication configuration is realized by setting the MASTER TO MASTER_DELAY parameter on the slave.
  • CHANGE MASTER TO MASTER_DELAY = N;
  • N is the number of seconds. This statement sets a delay of N seconds for the slave database before synchronously replicating data with the master database.

1. Configure delay for one minute

mysql> stop slave;
mysql> change master to MASTER_DELAY = 60;
mysql> start slave;

2. Test, insert the following piece of data in the main database

mysql> insert into usertb values ('user2','222');

3. After the data is inserted, it can be queried quickly on the master node, but it will take 60 seconds to be queried on the slave node. If the effect after your configuration is like this, it means that mysql5.7 delayed replication configuration is successful.

mysql> select * from usertb;
+----------+----------+
| username | password |
+----------+----------+
| user1    | 111      |
| user2    | 222      |
+----------+----------+
2 rows in set (0.01 sec)

4. Check that the value of SQL_Delay is 60, which means the setting is successful.
The checking method isshow slave status \G;

 mysql> show slave status \G;
 Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 60
  • note
  • SQL_Delay: A non-negative integer, indicating the number of seconds, how many seconds the Slave lags behind the master.
  • SQL_Remaining_Delay: When Slave_SQL_Running_State waits until MASTER_DELAY seconds, the Master executes the event.
  • This field contains an integer indicating how many seconds or so of delay there is. At other times, this field is NULL.

5. Analysis of delayed replication principle

  • Mysql's delayed replication actually affects only the SQL thread applying data to the slave database, and the I/O thread has already updated the master database to the data and written it to the relay log of the slave database. Therefore, during delayed replication, even if the master When the library is down, the slave library will still update the data to be consistent with the time when the main library is down when it is time for delayed replication.

Guess you like

Origin blog.csdn.net/weixin_44006354/article/details/103651843