Detailed explanation of replication configuration example between mysql containers

Detailed explanation of replication configuration example between mysql containers

background

Last week the company trained on MySQL replication, and this weekend I plan to put what I learned into practice.

Master server: MySQL container mysql_master on NAS

  • NAS server IP: 192.168.1.108
  • mysql_master inner ip: 172.17.0.6

Slave server: MySQK container mysql_slave on Mac mini

  • Mac mini docker host IP: 192.168.1.139
  • mysql_slave inner ip: 172.17.0.2

Prepare the MySQL container

Prepare mysql_master

Create two directories to store MySQL files

mkdir -p /mnt/md1/disk4/mysql
mkdir -p /mnt/md1/disk4/mysql-files

Create a master mysql node for testing

[root@TNAS-2664 disk4]# docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql_master -v /mnt/md1/disk4/mysql:/var/lib/mysql -v /mnt/md1/disk4/mysql-files:/var/lib/mysql-files mysql
3bebf0e21df6d9034ce8275b14ebb1616e11f5e2678b1e084d03c087ed91a72a

View the container ID of MySQL currently running on the NAS

[root@TNAS-2664 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
40db0be51460 mysql "docker-entrypoint..." 44 seconds ago Up 29 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql_master
db5f6a287a21 mautic/mautic "/entrypoint.sh ap..." 2 weeks ago Up 11 days 0.0.0.0:8082->80/tcp mautic
dc1eac509c70 qianliu/mediawikiwithcomposer "docker-php-entryp..." 2 weeks ago Up 11 days 0.0.0.0:8086->80/tcp sarawiki
b5c0a00f5f42 mysql "docker-entrypoint..." 2 weeks ago Up 11 days 0.0.0.0:3306->3306/tcp, 33060/tcp mysql2
911c0a8987ba qianliu/mediawikiwithcomposer "docker-php-entryp..." 2 weeks ago Up 11 days 0.0.0.0:8083->80/tcp qianliuwiki

Use the docker cp command to copy my.cnf to the host machine and make changes

docker cp 40db0be51460:/etc/mysql/my.cnf .

Add the following configuration to my.cnf

server-id = 1
 gtid-mode = ON # (replicated by GTID)
 enforce_gtid_consistency =1 #(replicated by GTID) 
 log-bin = master-log
 binlog_format = mixed
 expire-logs-days = 14
 sync-binlog = 1
 log-bin-trust-function-creators= 1
 
 # MASTER DB # 
 binlog-ignore-db = mysql,information_schema,performance_schema,sys
 auto-increment-increment = 2
 auto-increment-offset = 1
 
 # SLAVE DB #
 replicate-ignore-db = mysql,information_schema,performance_schema,sys
 relay_log = relay-log
 log-slave-updates = ON

Copy my.cnf to the mysql_master container using docker cp

docker cp my.cnf 40db0be51460:/etc/mysql/

Enter mysql_slave and find that my.cnf is ignored because of the permission file. This will cause the configuration just written to my.cnf to fail to take effect.

[root@TNAS-2664 ~]# docker exec -it mysql_master /bin/bash
root@40db0be51460:/#mysql -uroot -p123456
mysql: [Warning] World-writable config file '/etc/mysql/my.cnf' is ignored.
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.

Change the permissions of my.cnf to 664

root@40db0be51460:/# chmod 644 /etc/mysql/my.cnf
root@40db0be51460:/# exit

Restart mysql_slave to make my.cnf take effect

[root@TNAS-2664 ~]# docker restart mysql_master

9. Enter mysql_master to view master status

mysql> show master status;
+-------------------+----------+--------------+-------------------------------------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-------------------+----------+--------------+-------------------------------------------------+-------------------+
| master-log.000001 | 156 | | mysql,information_schema,performance_schema,sys | |
+-------------------+----------+--------------+-------------------------------------------------+-------------------+
1 row in set (0.00 sec)

mysql> exit

Prepare the mysql_slave container

Create two directories on the Mac mini to store MySQL files

mkdir -p /Volumes/MacintoshHDD_Data/mysql_slave_db/mysql
mkdir -p /Volumes/MacintoshHDD_Data/mysql_slave_db/mysql-files

Create a mysql_slave container for testing

docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql_slave -v /Volumes/MacintoshHDD_Data/mysql_slave_db/mysql:/var/lib/mysql -v /Volumes/MacintoshHDD_Data/mysql_slave_db/mysql-files:/var/lib/mysql-files mysql

View the container ID of mysql_slave currently running on the macmini

/Volumes/MacintoshHDD_Data/mysql_slave_db  docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8623ac99e5d4 mysql "docker-entrypoint.s…" 5 seconds ago Up 4 seconds 33060/tcp, 0.0.0.0:3307->3306/tcp mysql_slave

Use the docker cp command to copy my.cnf to the host machine and make changes

docker cp 8623ac99e5d4:/etc/mysql/my.cnf .

Add the following configuration to my.cnf

server-id = 2
 gtid-mode = ON
 enforce_gtid_consistency = 1
 log-bin = slave-log
 binlog_format = mixed
 expire-logs-days = 14
 sync-binlog = 1
 log-bin-trust-function-creators= 1
 
 # MASTER DB # 
 binlog-ignore-db = mysql,information_schema,performance_schema,sys
 auto-increment-increment = 2
 auto-increment-offset = 2
 
 # SLAVE DB #
 replicate-ignore-db = mysql,information_schema,performance_schema,sys
 relay_log = relay-log
 log-slave-updates = ON

Copy my.cnf to the mysql_master container using docker cp

docker cp my.cnf 8623ac99e5d4:/etc/mysql/

Restart mysql_slave to make my.cnf take effect

docker restart mysql_slave

Enter mysql_slave to view the master status

mysql> show master status;
+------------------+----------+--------------+-------------------------------------------------+------------------------------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+-------------------------------------------------+------------------------------------------+
| slave-log.000001 | 1460 | | mysql,information_schema,performance_schema,sys | f102ae13-5341-11eb-a578-0242ac110002:1-5 |
+------------------+----------+--------------+-------------------------------------------------+------------------------------------------+
1 row in set (0.00 sec)

Prepare mysql user for replication

Prepare the mysql user for replication in mysql_master

mysql> CREATE USER 'slave'@'192.168.1.139' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.59 sec)

mysql> CREATE USER 'slave'@'172.17.0.2' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.60 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.1.139';
Query OK, 0 rows affected (0.19 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'172.17.0.2';
Query OK, 0 rows affected (0.19 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.10 sec)

mysql> exit

Prepare the mysql user for replication in mysql_slave

mysql> CREATE USER 'slave'@'192.168.1.108' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.02 sec)

mysql> CREATE USER 'slave'@'172.17.0.6' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'192.168.1.108';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'172.17.0.6';
Query OK, 0 rows affected (0.01 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql>

replication configuration

Configure mysql_master

mysql> change master to master_host='192.168.1.139',master_user='slave',master_password='slave',master_log_file='slave-log.000001',master_port=3307, master_log_pos=1460;
Query OK, 0 rows affected, 2 warnings (1.17 sec)

mysql> change master to master_host='192.168.1.139',master_user='slave',master_password='slave',master_auto_position=1,get_master_public_key=1;
Query OK, 0 rows affected, 2 warnings (0.45 sec)

Configure mysql_slave

mysql> change master to master_host='192.168.1.108',master_user='slave',master_password='slave',master_log_file='master-log.000001',master_port=3307, master_log_pos=156;
Query OK, 0 rows affected, 2 warnings (0.15 sec)

mysql> change master to master_host='192.168.1.108',master_user='slave',master_password='slave',master_auto_position=1,get_master_public_key=1;
Query OK, 0 rows affected, 2 warnings (0.14 sec)

Enable replication

Start slave on mysql_master
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)

Checking the slave status, I found that replication was not started successfully. An error message appears: Access denied for user 'slave'@'172.17.0.1' (using password: YES). You need to create the corresponding user and permissions on mysql_slave.

mysql> show slave status \G;
*************************** 1. row ***************************
  Slave_IO_State: Connecting to master
   Master_Host: 192.168.1.139
   Master_User: slave
   Master_Port: 3307
  Connect_Retry: 60
  Master_Log_File:
  Read_Master_Log_Pos: 4
  Relay_Log_File: relay-log.000001
  Relay_Log_Pos: 4
 Relay_Master_Log_File:
  Slave_IO_Running: Connecting
  Slave_SQL_Running: Yes
  Replicate_Do_DB:
  Replicate_Ignore_DB: mysql,information_schema,performance_schema,sys
  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: 0
  Relay_Log_Space: 156
  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: 1045
  Last_IO_Error: error connecting to master '[email protected]:3307' - retry-time: 60 retries: 2 message: Access denied for user 'slave'@'172.17.0.1' (using password: YES)
  Last_SQL_Errno: 0
  Last_SQL_Error:
 Replicate_Ignore_Server_Ids:
  Master_Server_Id: 0
   Master_UUID:
  Master_Info_File: mysql.slave_master_info
   SQL_Delay: 0
  SQL_Remaining_Delay: NULL
 Slave_SQL_Running_State: Slave has read all relay logs; waiting for more updates
  Master_Retry_Count: 86400
   Master_Bind:
 Last_IO_Error_Timestamp: 210110 13:02:12
 Last_SQL_Error_Timestamp:
  Master_SSL_Crl:
  Master_SSL_Crlpath:
  Retrieved_Gtid_Set:
  Executed_Gtid_Set:
  Auto_Position: 1
  Replicate_Rewrite_DB:
   Channel_Name:
  Master_TLS_Version:
 Master_public_key_path:
 Get_master_public_key: 1
  Network_Namespace:
1 row in set (0.01 sec)

ERROR:
No query specified

mysql> exit

mysql_slave starts the slave and checks the slave status to see that everything is normal.

mysql> show slave status;
mysql> show slave status\G;
*************************** 1. row ***************************
  Slave_IO_State: Waiting for master to send event
   Master_Host: 192.168.1.108
   Master_User: slave
   Master_Port: 3307
  Connect_Retry: 60
  Master_Log_File: master-log.000001
  Read_Master_Log_Pos: 156
  Relay_Log_File: relay-log.000002
  Relay_Log_Pos: 373
 Relay_Master_Log_File: master-log.000001
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
  Replicate_Do_DB:
  Replicate_Ignore_DB: mysql,information_schema,performance_schema,sys
  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: 156
  Relay_Log_Space: 576
  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: 9627309d-5341-11eb-aaa3-0242ac110006
  Master_Info_File: mysql.slave_master_info
   SQL_Delay: 0
  SQL_Remaining_Delay: NULL
 Slave_SQL_Running_State: Slave has read all relay logs; 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: f102ae13-5341-11eb-a578-0242ac110002:1-5
  Auto_Position: 1
  Replicate_Rewrite_DB:
   Channel_Name:
  Master_TLS_Version:
 Master_public_key_path:
 Get_master_public_key: 1
  Network_Namespace:
1 row in set, 1 warning (0.01 sec)

ERROR:
No query specified

Create a user on mysql_slave

mysql> CREATE USER 'slave'@'172.17.0.1' IDENTIFIED BY 'slave';
Query OK, 0 rows affected (0.01 sec)

mysql> GRANT REPLICATION SLAVE ON *.* TO 'slave'@'172.17.0.1';
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

Check the slave status on mysql_master again, everything is normal

mysql> show slave status \G;
*************************** 1. row ***************************
  Slave_IO_State: Waiting for master to send event
   Master_Host: 192.168.1.139
   Master_User: slave
   Master_Port: 3307
  Connect_Retry: 60
  Master_Log_File: slave-log.000001
  Read_Master_Log_Pos: 2022
  Relay_Log_File: relay-log.000002
  Relay_Log_Pos: 2237
 Relay_Master_Log_File: slave-log.000001
  Slave_IO_Running: Yes
  Slave_SQL_Running: Yes
  Replicate_Do_DB:
  Replicate_Ignore_DB: mysql,information_schema,performance_schema,sys
  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: 2022
  Relay_Log_Space: 2440
  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: 2
   Master_UUID: f102ae13-5341-11eb-a578-0242ac110002
  Master_Info_File: mysql.slave_master_info
   SQL_Delay: 0
  SQL_Remaining_Delay: NULL
 Slave_SQL_Running_State: Slave has read all relay logs; 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: f102ae13-5341-11eb-a578-0242ac110002:1-7
  Executed_Gtid_Set: f102ae13-5341-11eb-a578-0242ac110002:1-7
  Auto_Position: 1
  Replicate_Rewrite_DB:
   Channel_Name:
  Master_TLS_Version:
 Master_public_key_path:
 Get_master_public_key: 1
  Network_Namespace:
1 row in set (0.01 sec)

ERROR:
No query specified

Test replication
Create test_db_slave on mysql_slave

mysql> CREATE DATABASE test_db_slave;
Query OK, 1 row affected (0.01 sec)

Create test_db_master on mysql_master

mysql> CREATE DATABASE test_db_master;
Query OK, 1 row affected (0.24 sec)

View databases on mysql_slave

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
|mysql |
| performance_schema |
|sys|
| test_db_master |
| test_db_slave |
+--------------------+
6 rows in set (0.00 sec)

mysql>

View databases on mysql_master

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
|mysql |
| performance_schema |
|sys|
| test_db_master |
| test_db_slave |
+--------------------+
6 rows in set (0.00 sec)

mysql>

At this point, mysql replication configuration is complete.

This is the end of this article about the replication configuration between MySQL containers. For more information about the replication configuration of MySQL containers, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • MySQL-group-replication configuration steps (recommended)
  • MySQL5.6 Replication master-slave replication (read-write separation) configuration complete version
  • Introduction to MySQL Semisynchronous Replication
  • MySQL 5.7 Enhanced Edition Semisync Replication Performance Optimization
  • MySQL-MMM Installation Guide (Multi-Master Replication Manager for MySQL)
  • Summary of issues based on mysql replication
  • mysql5.5 master-slave (Replication) configuration method
  • mysql5.5 master-slave (Replication) master-slave configuration
  • MySQL master-slave replication semi-sync replication

<<:  HTML table markup tutorial (14): table header

>>:  Detailed explanation of how to use Tomcat Native to improve Tomcat IO efficiency

Recommend

MySQL installation and configuration method graphic tutorial (CentOS7)

1. System environment [root@localhost home]# cat ...

Markup Language - List

Standardized design solutions - markup languages ...

MySQL database implements OLTP benchmark test based on sysbench

Sysbench is an excellent benchmark tool that can ...

What are Web Slices?

IE8 new feature Web Slices (Web Slices) Microsoft...

MySQL Community Server compressed package installation and configuration method

Today, because I wanted to install MySQL, I went ...

How to import/save/load/delete images locally in Docker

1. Docker imports local images Sometimes we copy ...

How to use HTML+CSS to create TG-vision homepage

This time we use HTML+CSS layout to make a prelim...

Instance method for mysql string concatenation and setting null value

#String concatenation concat(s1,s2); concatenate ...

MySQL exposes Riddle vulnerability that can cause username and password leakage

The Riddle vulnerability targeting MySQL versions...

js to realize the function of uploading pictures

The principle of uploading pictures on the front ...

Incomplete solution for using input type=text value=str

I encountered a very strange problem today. Look a...

Vue implements automatic jump to login page when token expires

The project was tested these days, and the tester...

JS uses canvas technology to imitate echarts bar chart

Canvas is a new tag in HTML5. You can use js to o...