MySQL主從複製與主主複製方案實現步驟

系統架構:

IP 主從
10.1.1.20 互爲主從
10.1.1.21

安裝MySQL,上面有過程,不多說。

1、配置10.1.1.20/etc/my.cnf文件,添加以下內容:

[mysqld]
#任意自然數n,只要保證兩臺MySQL主機不重複就可以了。
server-id=20
#開啓二進制日誌
log-bin=mysql-bin
#步進值auto_imcrement。一般有n臺主MySQL就填n
auto_increment_increment=2
#起始值。一般填第n臺主MySQL。此時爲第一臺主MySQL
auto_increment_offset=1
#忽略mysql庫,可不寫
binlog-ignore=mysql
#忽略information_schema庫,可不寫
binlog-ignore=information_schema
#要同步的數據庫,默認所有庫
replicate-do-db=aa

配置10.1.1.21/etc/my.cnf文件,添加以下內容:

[mysqld]
#任意自然數n,只要保證兩臺MySQL主機不重複就可以了。
server-id=21
#開啓二進制日誌
log-bin=mysql-bin
#步進值auto_imcrement。一般有n臺主MySQL就填n
auto_increment_increment=2
#起始值。一般填第n臺主MySQL。此時爲第二臺主MySQL
auto_increment_offset=2
#忽略mysql庫,可不寫
binlog-ignore=mysql
#忽略information_schema庫,可不寫
binlog-ignore=information_schema
#要同步的數據庫,默認所有庫
replicate-do-db=aa

2、重啓10.1.1.20和21上的MySQL:

service mysql restart

3、在10.1.1.20的MySQL上創建一個可以在10.1.1.21上登陸進行主從複製的用戶:

mysql>grant replication slave on *.* to 'master'@'10.1.1.21'identified by '123456';

在10.1.1.21的MySQL上創建一個可以在10.1.1.20上登陸進行主從複製的用戶:

mysql>grant replication slave on *.* to 'rep'@'10.1.1.20'identified by '123456';

4、查看10.1.1.20和10.1.1.21的master信息:
10.1.1.20:

mysql> show master status;
+-----------+-------+--------------+------------------+-------------------+
| File              | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+----------------+-------+-------------+------------ ----+---------------+
| master-bin.000002 |   120  |              |                 |                |
+-------------------+----------+--------------+-------+----------------+
10.1.1.21:
+----------------+----------+--------------+-----------+---------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+-------+-------------+-----------+--------------------+
| mysql-bin.000002 |   120  |             |            |                     |
+-----------------+----------+--------------+--------------+------------+

5、在10.1.1.20和10.1.1.21的MySQL上配置主從信息,並啓動slave:
10.1.1.20:

mysql>change master to
master_host='10.1.1.21',master_user='rep',master_password='123456',master_log_file='mysql-bin.000002',master_log_pos=120;
mysql>start slave;
10.1.1.21:
mysql>change master to
master_host='10.1.1.20',master_user='master',master_password='123456',master_log_file='master-bin.000002',master_log_pos=120;
mysql>start slave;

6、啓動後檢查slave狀態:
10.1.1.20:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.1.20
                  Master_User: master
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: master-bin.000003
          Read_Master_Log_Pos: 120
               Relay_Log_File: localhost-relay-bin.000008
                Relay_Log_Pos: 284
        Relay_Master_Log_File: master-bin.000003
             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: 120
              Relay_Log_Space: 461
              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: 20
                  Master_UUID: 464bd858-8279-11e9-b5a7-000c29c380a1
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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: 
                Auto_Position: 0
1 row in set (0.00 sec)

10.1.1.21:

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.1.1.21
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 120
               Relay_Log_File: localhost-relay-bin.000002
                Relay_Log_Pos: 283
        Relay_Master_Log_File: mysql-bin.000002
             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: 120
              Relay_Log_Space: 460
              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: 21
                  Master_UUID: 462be4d8-8279-11e9-b5a7-000c291e492e
             Master_Info_File: /var/lib/mysql/master.info
                    SQL_Delay: 0
          SQL_Remaining_Delay: NULL
      Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it
           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: 
                Auto_Position: 0
1 row in set (0.00 sec)

上述兩個MySQL的‘Slave_IO_Running’和‘Slave_SQL_Running’均爲‘yes’,說明主從複製正常。
以上就是兩個MySQL互爲主從的配置方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章