实验前说明:

    其实,MySQL主主复制的本质,就是MySQL的双向主从,没有我们想的那么复杂。配置也很简单。明白了这点,我们下来开始实践!

实验前提:已经做好主从关系的master与slave两台机器(当然,多实例也可以)

master 192.168.0.102   hostname:master 

slave  192.168.0.103   hostname:slave

OK,如果上述环境已经完成,接下来开始做主主。


步骤一:修改master,与slave的my.cnf配置文件(当然了,此步最好提前.)

关键核心代码:

master数据库的my.cnf核心:
relay-log = /application/mysql/data/relay-bin
relay-log-info-file = /application/mysql/data/relay-log.info
log-bin = /application/mysql/data/mysql-bin
log-slave-updates
auto_increment_increment = 2  #自增为2
auto_increment_offset = 1     #起始为1

……

slave数据库的my.cnf核心:
relay-log = /application/mysql/data/relay-bin
relay-log-info-file = /application/mysql/data/relay-log.info
log-bin = /application/mysql/data/mysql-bin
log-slave-updates
auto_increment_increment = 2  #自增为2
auto_increment_offset = 2     #起始位2

代码解说:主从都开启binlog和relay-log就不用说了吧?同步的核心...

    为了防止主主结构的数据库架构在写入时发生冲突故障,因此需要对主主数据库写入数据进行特殊编号,按照上边我发的代码来看,master的起始是1,每次自增2,那么下次记录数据编号就是3,以此类推,主数据库中的记录编号依次为 1,3,5,7……

    同理,slave起始位2,每次自增2,那么从数据库中的记录依次是2,4,6,8……这样避免了同事写入记录时的冲突问题.

步骤二:重启master,slave数据库

步骤三:在slave数据库上边锁表,记录postion信息,记录之后解锁。

mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status; 
+------------------+----------+--------------+--------------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB         |
+------------------+----------+--------------+--------------------------+
| mysql-bin.000004 |      347 |              | mysql,information_schema |
+------------------+----------+--------------+--------------------------+
1 row in set (0.00 sec)
mysql> unlock tables;

步骤四:在slave数据库授权主数据库同步用户

mysql> grant replication slave on *.* to 'slave'@'192.168.0.102' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

步骤五:在master数据库执行change操作

mysql> CHANGE MASTER TO  MASTER_HOST='192.168.0.103', MASTER_PORT=3306, MASTER_USER='slave', MASTER_PASSWORD='123456', MASTER_LOG_FILE='mysql-bin.000004', MASTER_LOG_POS=347; 
Query OK, 0 rows affected (0.01 sec)

步骤六:开启master数据库slave同步,并且查看同步状态

mysql> slave start;
Query OK, 0 rows affected (0.00 sec)
mysql> show slave status\G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.103
                  Master_User: slave
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 347
               Relay_Log_File: relay-bin.000002
                Relay_Log_Pos: 253
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
              Replicate_Do_DB: 
          Replicate_Ignore_DB: mysql
           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: 347
              Relay_Log_Space: 403
              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: 2883
1 row in set (0.00 sec)

ERROR: 
No query specified

步骤七:测试工作,测试原始的主从是否正常。

        测试工作2,在slave端写入,是否可以同步到master。

至此,MySQL主主模式搭建完成.