SoFunction
Updated on 2025-04-11

Implementation of MySQL5.7.35 dual master construction

Upload and install MySQL 5.7.35

(172.25.0.19 and 172.25.0.20 are both deployed, installed and started)

[root@QYWLAQ_VMC4234 ~]# hostnamectl
   Static hostname: QYWLAQ_VMC4234
         Icon name: computer-vm
           Chassis: vm
        Machine ID: cd3605d12efd430fb723eb507cf5ce3b
           Boot ID: 00f929e5510944efb40d53ef55d92a49
    Virtualization: kvm
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64

[root@QYWLAQ_VMC4234 ~]# ll
-rw-r--r--  1 citc unicom  26658592 12moon 25 17:02 mysql-community-client-5.7.35-1.el7.x86_64.rpm
-rw-r--r--  1 citc unicom    317800 12moon 25 17:03 mysql-community-common-5.7.35-1.el7.x86_64.rpm
-rw-r--r--  1 citc unicom   2473348 12moon 25 17:03 mysql-community-libs-5.7.35-1.el7.x86_64.rpm
-rw-r--r--  1 citc unicom   1263816 12moon 26 14:35 mysql-community-libs-compat-5.7.35-1.el7.x86_64.rpm
-rw-r--r--  1 citc unicom 182213816 12moon 25 17:05 mysql-community-server-5.7.35-1.el7.x86_64.rpm

# Install rpm package[root@QYWLAQ_VMC4234 ~]# yum localinstall -y *.rpm

# Start and set to enable self-start[root@QYWLAQ_VMC4234 ~]# systemctl enable --now mysqld

[root@QYWLAQ_VMC4234 citc]# systemctl status mysqld
●  - MySQL Server
   Loaded: loaded (/usr/lib/systemd/system/; enabled; vendor preset: disabled)
   Active: active (running) since three 2025-02-26 12:24:03 CST; 4min 26s ago
     Docs: man:mysqld(8)
           /doc/refman/en/
  Process: 8441 ExecStart=/usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/ $MYSQLD_OPTS (code=exited, status=0/SUCCESS)
  Process: 8374 ExecStartPre=/usr/bin/mysqld_pre_systemd (code=exited, status=0/SUCCESS)
 Main PID: 8444 (mysqld)
    Tasks: 27
   Memory: 289.2M
   CGroup: //
           └─8444 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/

2moon 26 12:23:57 QYWLAQ_VMC4234 systemd[1]: Starting MySQL Server...
2moon 26 12:24:03 QYWLAQ_VMC4234 systemd[1]: Started MySQL Server.

# Get the initial password[root@QYWLAQ_VMC4234 citc]# sudo grep 'temporary password' /var/log/
2025-02-26T04:23:59.794639Z 1 [Note] A temporary password is generated for root@localhost: IlM3/)Ya6VMH

# Log in[root@QYWLAQ_VMC4234 citc]# mysql -u root -p'IlM3/)Ya6VMH'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.35

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '8th3xY]:NA'; -- Reviserootpassword
Query OK, 0 rows affected (0.00 sec)

mysql> FLUSH PRIVILEGES; -- 刷新权限以使Revise立即生效
Query OK, 0 rows affected (0.01 sec)

Dual-master replication configuration

Example 1: 172.25.0.19:

# Add the following configuration to this configuration file[root@QYWLAQ_VMC4234 ~]# vi /etc/

[mysqld]
# Basic configurationserver-id = 1                     # The server-id of each instance must be uniquelog_bin = /var/log/mysql/  # Enable binary loggingbinlog_format = ROW               # Recommended ROW formatexpire_logs_days = 7              # Automatically clean logs from 7 days agomax_binlog_size = 100M            # Maximum size of each binary log file
# Dual master copy configurationlog_slave_updates = 1             # Log binary logs from the library (must be enabled)auto_increment_increment = 2      # Self-increase step lengthauto_increment_offset = 1         # Increase the starting value (Example 1 is 1, Example 2 is 2)
# Copy filtering (optional)replicate-do-db = my_database     # Copy only the specified databasereplicate-ignore-db = mysql       # Ignore the system library
#Other configurationsbind-address = 0.0.0.0            # Allow remote connections

Restart MySQL

[root@QYWLAQ_VMC4234 ~]# systemctl restart mysqld

Example 2: 172.25.0.20:

# Add the following configuration to this configuration file[root@QYWLAQ_VMC1091 ~]# vi /etc/

[mysqld]
# Basic configurationserver-id = 2                     # The server-id of each instance must be uniquelog_bin = /var/log/mysql/  # Enable binary loggingbinlog_format = ROW               # Recommended ROW formatexpire_logs_days = 7              # Automatically clean logs from 7 days agomax_binlog_size = 100M            # Maximum size of each binary log file
# Dual master copy configurationlog_slave_updates = 1             # Log binary logs from the library (must be enabled)auto_increment_increment = 2      # Self-increase step lengthauto_increment_offset = 2         # Increase the starting value (Example 1 is 1, Example 2 is 2)
# Copy filtering (optional)replicate-do-db = my_database     # Copy only the specified databasereplicate-ignore-db = mysql       # Ignore the system library
#Other configurationsbind-address = 0.0.0.0            # Allow remote connections

Restart MySQL

[root@QYWLAQ_VMC1091 ~]# systemctl restart mysqld

Configure the replication user

Create a user for replication on each MySQL instance.

Execute on instance 1 (172.25.0.19):

mysql> CREATE USER 'replication'@'172.25.0.20' IDENTIFIED BY '@2X0wZY/rq';
Query OK, 0 rows affected (0.01 sec)

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

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

Query the user

mysql> SELECT User, Host FROM ;
+---------------+-------------+
| User          | Host        |
+---------------+-------------+
| replication   | 172.25.0.20 |
|  | localhost   |
|      | localhost   |
| root          | localhost   |
+---------------+-------------+
4 rows in set (0.00 sec)

Execute on instance 2 (172.25.0.20):

mysql> CREATE USER 'replication'@'172.25.0.19' IDENTIFIED BY '@2X0wZY/rq';
Query OK, 0 rows affected (0.00 sec)

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

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

Query the user

mysql> SELECT User, Host FROM ;
+---------------+-------------+
| User          | Host        |
+---------------+-------------+
| replication   | 172.25.0.19 |
|  | localhost   |
|      | localhost   |
| root          | localhost   |
+---------------+-------------+
4 rows in set (0.00 sec)

Configure master-slave replication

Configure the other party as the main library on each instance.

View the instance 2 node status:

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      964 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Execute on Example 1:

CHANGE MASTER TO
  MASTER_HOST='172.25.0.20',
  MASTER_USER='replication',
  MASTER_PASSWORD='@2X0wZY/rq',
  MASTER_LOG_FILE='mysql-bin.000002',     -- Replace with an instance 2 The current binlog document
  MASTER_LOG_POS=1784;                     -- Replace with an instance 2 The current binlog Location
START SLAVE;

View instance 1 node status:

mysql> SHOW MASTER STATUS;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      964 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

Execute on Example 2:

CHANGE MASTER TO
  MASTER_HOST='172.25.0.19',
  MASTER_USER='replication',
  MASTER_PASSWORD='@2X0wZY/rq',
  MASTER_LOG_FILE='mysql-bin.000002',     -- Replace with an instance 1 The current binlog document
  MASTER_LOG_POS=1784;                     -- Replace with an instance 1 The current binlog Location
START SLAVE;

Verify the replication status

Execute the following command on each instance to check the replication status:

SHOW SLAVE STATUS\G;

Make sure the following fields areYes

  • Slave_IO_Running
  • Slave_SQL_Running

Test dual master copy

On Server A:

Create test databases and tables

CREATE DATABASE test_db;
USE test_db;
CREATE TABLE test_table (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50)
) ENGINE=InnoDB;

Insert data

INSERT INTO test_table (name) VALUES ('Server A Data');

On Server B:

Check if the data is synchronized

USE test_db;
SELECT * FROM test_table;

It should be able to seeServer A Data

Insert data

INSERT INTO test_table (name) VALUES ('Server B Data');

Back to Server A to operate:

Check if the data is synchronized

USE test_db;
SELECT * FROM test_table;
table (name) VALUES (‘Server A Data');

On Server B:

Check whether the data is synchronized:

USE test_db;
SELECT * FROM test_table;

You should be able to see Server A Data.

Insert data:

INSERT INTO test_table (name) VALUES (‘Server B Data');

Back to Server A to operate:

Check whether the data is synchronized:

USE test_db;
SELECT * FROM test_table;

You should be able to see Server B Data.

This is the end of this article about the implementation of MySQL 5.7.35 dual-main construction. For more related content on MySQL dual-main construction, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!