【MySQL】MySQL搭建主主复制(双主复制/DUAL Master)环境

时间:2022-09-15 08:44:47
主主复制其实就是Master1和Master2互为主从,下面具体演示其搭建过程。

一 软件环境
  • Mysql 5.7.21
  • Oracle Linux 7.1
二 主机设置
  • Master1 IP:10.24.33.186
  • Master2 IP:10.24.33.188
三 主主配置

1、Master1到Master2的主从复制环境搭建过程可参考:MySQL搭建主从复制环境
注意:因为互为主从,注意自动生成的序列的设置。
Master1的配置文件:
[root@strong mysql]# more /etc/my.cnf 
[mysqld]

character-set-server=utf8

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

log-bin=/usr/local/mysql/binlog/mysql-bin
server-id=1

gtid_mode=ON
enforce-gtid-consistency=true

auto_increment_increment=2
auto_increment_offset=1


[mysql]

default-character-set=utf8
2、Master2到Master2的主从复制环境搭建如下:
1)创建复制用户
mysql> create user 'repl'@'10.24.33.186' identified by 'repl';
Query OK, 0 rows affected (0.04 sec)

mysql> grant replication slave on *.* to 'repl'@'10.24.33.186';
Query OK, 0 rows affected (0.01 sec)

mysql> 
2)设置Master1配置
mysql> change master to
    -> master_host='10.24.33.188',
    -> master_port=3306,
    -> master_user='repl',
    -> master_password='repl',
    -> master_auto_position=1;--基于GTID实现
Query OK, 0 rows affected, 2 warnings (0.05 sec)
mysql> start slave;
Query OK, 0 rows affected (0.03 sec)

mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.24.33.188
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000018
          Read_Master_Log_Pos: 194
               Relay_Log_File: strong-relay-bin.000016
                Relay_Log_Pos: 407
        Relay_Master_Log_File: mysql-bin.000018
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: 
           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: 194
              Relay_Log_Space: 1339
              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: fb42b433-06fc-11e8-b91a-0800275b8da0
             Master_Info_File: /usr/local/mysql/data/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; 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: fb42b433-06fc-11e8-b91a-0800275b8da0:1-5
            Executed_Gtid_Set: 2a3e3fd9-0587-11e8-bdb8-0800272325a8:1-21,
fb42b433-06fc-11e8-b91a-0800275b8da0:1-5
                Auto_Position: 1
         Replicate_Rewrite_DB: 
                 Channel_Name: 
           Master_TLS_Version: 
1 row in set (0.00 sec)

ERROR: 
No query specified
3)Master2的配置文件
[root@strong ~]# more /etc/my.cnf 
[mysqld]

character-set-server=utf8

basedir=/usr/local/mysql
datadir=/usr/local/mysql/data

log-bin=/usr/local/mysql/binlog/mysql-bin
server-id=2

gtid_mode=ON
enforce_gtid_consistency=true

auto_increment_increment=2
auto_increment_offset=2

四 验证主主复制

1、Master1操作
mysql> use test;
Database changed
mysql> insert into t_mm (name) values('Alen'),('Jack'),('Summer');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select *from t_mm;
+----+--------+---------------------+
| id | name   | cdate               |
+----+--------+---------------------+
|  1 | Alen   | 2018-02-28 17:26:08 |
|  3 | Jack   | 2018-02-28 17:26:08 |
|  5 | Summer | 2018-02-28 17:26:08 |
+----+--------+---------------------+
3 rows in set (0.00 sec)
2、Master2操作
mysql> use test;
Database changed
mysql>  insert into t_mm (name) values('China'),('Japon'),('USA');
Query OK, 3 rows affected (0.04 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select *from t_mm;
+----+-------+---------------------+
| id | name  | cdate               |
+----+-------+---------------------+
|  2 | China | 2018-02-28 17:26:26 |
|  4 | Japon | 2018-02-28 17:26:26 |
|  6 | USA   | 2018-02-28 17:26:26 |
+----+-------+---------------------+
3 rows in set (0.00 sec)
3、分别查看Master1和Master2,出现相同的结果
mysql> select *from t_mm;
+----+--------+---------------------+
| id | name   | cdate               |
+----+--------+---------------------+
|  1 | Alen   | 2018-02-28 17:26:08 |
|  2 | China  | 2018-02-28 17:26:26 |
|  3 | Jack   | 2018-02-28 17:26:08 |
|  4 | Japon  | 2018-02-28 17:26:26 |
|  5 | Summer | 2018-02-28 17:26:08 |
|  6 | USA    | 2018-02-28 17:26:26 |
+----+--------+---------------------+
6 rows in set (0.00 sec)
至此,主主复制环境搭建成功。