brief introduction
The so-called "dual master replication" is actually "mutual master-slave replication". That is, each master host has both a master role and a slave role. In this way, the changes made by any party will be replicated to the database of another host through the master and slave to achieve data consistency. This is "dual master replication".
There are two ways of database replication:
* GTID-based replication * Binary log location-based replication
The latter discussed in this article is "replication based on binary log location", but "replication based on GTID" may be a better choice.
System environment
Attributes | parameter |
---|---|
operating system: | CentOS Linux release 7.4.1708 (Core) |
Software version: | MySQL 5.6.45 |
Internet Information: | master-01: 10.10.50.115 |
master-02: 10.10.50.116 |
The first step is to install the database service
Refer to MySQL 5.6 (CentOS 7) notes, skip the detailed process.
Configuration service
# On master-01
#!/bin/sh ########################################## ################################### # (0) Create the necessary directories: ##### ############################################## ######################## mkdir -pv /var/log/mysql/ chown mysql.mysql /var/log/mysql/ #### ############################################## ######################### # (1) Modify the /etc/mysql/my.cnf file: #! ! ! Different distributions have different configuration locations, please don't copy them! Please don't copy it! Please don't copy it! ############################################## ############################# mv /etc/mysql/my.cnf /etc/mysql/my.cnf.backup cat <<MYCNF >> /etc/mysql/my.cnf [mysqld] server_id = 1 auto-increment-offset = 1 auto-increment-increment = 2 log_bin = /var/log/mysql/mysql-bin.log log_bin_index = /var/log/mysql/mysql-bin.log.index relay_log = /var/log/mysql/mysql-relay-bin relay_log_index = /var/log /mysql/mysql-relay-bin.index expire_logs_days = 10 max_binlog_size = 100M log_slave_updates = 1 # Modify according to the situation: # bind-address = xxxx MYCNF ################## ############################################## ########### # (2) Restart the service: ############################# ############################################## systemctl restart mysqld.service
# On master-02
#!/bin/sh ########################################## ################################### # (0) Create the necessary directories: ##### ############################################## ######################## mkdir -pv /var/log/mysql/ chown mysql.mysql /var/log/mysql/ #### ############################################## ######################### # (1) Modify the /etc/mysql/my.cnf file: #! ! ! Different distributions have different configuration locations, please don't copy them! Please don't copy it! Please don't copy it! ############################################## ############################# mv /etc/mysql/my.cnf /etc/mysql/my.cnf.backup cat <<MYCNF >> /etc/mysql/my.cnf [mysqld] # Specify the server_id parameter, the two databases must be different server_id = 2 # Specify the initial value of the ID of the current database, starting from 2, cooperate with auto-increment-increment to prevent the auto-increment ID conflict of the two main libraries # The auto-increment ID of this library is an even number, and the auto-increment ID of the other library All are odd auto-increment-offset = 2 auto-increment-increment = 2 # Binary log file, and index file # The index file contains a list of all binary log files log_bin = /var/log/mysql/mysql-bin .log log_bin_index = /var/log/mysql/mysql-bin.log.index # Binary log expiration time expire_logs_days = 10 # Maximum size of binary log max_binlog_size = 100M # Make the update executed by its SQL thread from the library write itself The binary log. # https://dba.stackexchange.com/questions/169071/do-log-slave-updates-in-mysql-5-7-fail-the-replication-in-linux?noredirect=1&lq=1 log_slave_updates = 1 # Relay log file, and index file # The index file contains a list of all relay log files relay_log = /var/log/mysql/mysql-relay-bin relay_log_index = /var/log/mysql/mysql-relay-bin.index # Modify according to the situation: # bind-address = xxxx MYCNF ##################### ############################################## ######## # (2) Restart the service: ################################ ############################################ systemctl restart mysqld. service
Create user for replication
Create users for replication on master-01 and master-02 respectively. After creating a user, don't forget to verify whether the user can log in normally.
# On master-01
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED BY 'BU3CGK6x';
# On master-02
GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%' IDENTIFIED BY 'BU3CGK6x';
Enable replication
Respectively enable replication on master-01 and master-02:
# On master-01
-- SQL -- STOP SLAVE; CHANGE MASTER TO master_host='10.10.50.116', master_port=3306, master_user='replication', master_password='BU3CGK6x', master_log_file='mysql-bin.000001', master_log_pos=0; START SLAVE;
# On master-02
-- SQL -- STOP SLAVE; CHANGE MASTER TO master_host='10.10.50.115', master_port=3306, master_user='replication', master_password='BU3CGK6x', master_log_file='mysql-bin.000001', master_log_pos=0; START SLAVE;
Precautions
1) Where numbers are used, do not use quotation marks. For example, master_port='3306' will cause a syntax error
Copy verification
The verification process is not detailed here. Because it is relatively simple, here is only an overview of the idea of verifying whether the copy works:
Create libraries and tables on master-01, and then view them on master-02. Create libraries and tables on master-02, and then view them on master-01.
If there is no problem, there is no problem. If there is no synchronization, you can execute the SHOW SLAVE STATUS \G statement on the database to view the synchronization status and synchronization error information (if any).
Check status
Use SHOW MASTER STATUS \G statement to view your own status; use SHOW SLAVE STATUS \G statement to view your own status as a slave;
related articles
"MySQL"-dual master replication (GTID-based replication)
"MySQL"-build master-slave replication (location-based replication)
"MySQL"-build master-slave replication (GTID-based replication)
references
Configure Master-Master MySQL Database Replication
MariaDB/Relay Log
Do Log_Slave_Updates in MySQL 5.7 fail the replication in Linux?