1. 程式人生 > >MySQL系列詳解十:MySQL多源複製演示-技術流ken

MySQL系列詳解十:MySQL多源複製演示-技術流ken

前言

多源複製即多主一從結構,多個主伺服器端的資料都會同步到後端一個從伺服器上面。至於為什麼要做多源複製下面的總結很到位。

1、災備作用:將各個庫彙總在一起,就算是其他庫都掛了(整個機房都無法連線了),還有最後一個救命稻草; 2、備份:直接在這個從庫中做備份,不影響線上的資料庫; 3、減少成本:不需要每個庫都做一個例項,也減少了DBA的維護成本; 4、資料統計:後期的一些資料統計,需要將所有的庫彙總在一起。

MySQL多源複製演示

準備

主1伺服器端:10.220.5.137

主2伺服器端:10.220.5.138

從伺服器端:10.220.5.139

配置兩個主服務端

首先要保證三個節點中的server_id不一致,檢查各個伺服器端server_id

檢查主1服務端id

[[email protected] ~]# vim /etc/my.cnf...#binlog
#binlog_format = STATEMENT
binlog_format = row
server-id = 1003307   
log-bin = /data/mysql/mysql3306/logs/mysql-bin
binlog_cache_size = 4M
max_binlog_size = 256M
max_binlog_cache_size = 1M
sync_binlog = 0
expire_logs_days = 10...

檢查主2服務伺服器端id

[
[email protected]
~]# vim /etc/my.cnf ... #binlog #binlog_format = STATEMENT binlog_format = row server-id = 1003308 log-bin = /data/mysql/mysql3306/logs/mysql-bin binlog_cache_size = 4M max_binlog_size = 256M max_binlog_cache_size = 1M sync_binlog = 0 expire_logs_days = 10 ...

檢查從服務伺服器端id

[[email protected]
~]# vim /etc/my.cnf ... #binlog #binlog_format = STATEMENT binlog_format = row server-id = 1003309 log-bin = /data/mysql/mysql3306/logs/mysql-bin binlog_cache_size = 4M max_binlog_size = 256M max_binlog_cache_size = 1M sync_binlog = 0 expire_logs_days = 10 ...

在兩個主伺服器端建立用於複製的使用者

mysql> grant replication slave on *.* to 'ken'@'%' identified by 'xx';

這樣兩個主伺服器端就已經配置完成了,現在來配置從伺服器端

配置從伺服器端

檢查從段的master_info_repository以及relay_log_info_repository 

MySQL [(none)]> show global variables like '%info%';
+--------------------------------+----------------+
| Variable_name                  | Value          |
+--------------------------------+----------------+
| master_info_repository         | FILE         |
| relay_log_info_file            | relay-log.info |
| relay_log_info_repository      | FILE        |
| session_track_transaction_info | OFF            |
| sync_master_info               | 10000          |
| sync_relay_log_info            | 10000          |
+--------------------------------+----------------+
6 rows in set (0.01 sec)

修改從段的master_info_repository以及relay_log_info_repository 為table

MySQL [(none)]> set global master_info_repository = 'table';
MySQL [(none)]> set global relay_log_info_repository = 'table';

從端與主1建立連線

首先需要檢視主1以及主2伺服器的二進位制日誌位置以便開始複製

主1伺服器

mysql> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000014
         Position: 234
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-16,
c01b1811-d7b3-11e8-8698-000c29492f7b:3-7
1 row in set (0.00 sec)

主2 伺服器

MySQL [(none)]> show master status\G
*************************** 1. row ***************************
             File: mysql-bin.000009
         Position: 234
     Binlog_Do_DB: 
 Binlog_Ignore_DB: 
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-16,
c01b1811-d7b3-11e8-8698-000c29492f7b:1-7
1 row in set (0.00 sec)

與主1建立連線

change master to master_host='10.220.5.137',master_user='ken',master_password='xx',master_log_file='mysql-bin.000014',master_log_pos=234 for channel 'm1';

與主2建立連線

change master to master_host='10.220.5.138',master_user='ken',master_password='xx',master_log_file='mysql-bin.000009',master_log_pos=234 for channel 'm2';

啟動slave

MySQL [(none)]> start slave;

檢視連線狀態

MySQL [(none)]> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.220.5.137    <<主1連線成功
                  Master_User: ken
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000014
          Read_Master_Log_Pos: 234
               Relay_Log_File: relay-bin-m1.000003
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000014
             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: 234
              Relay_Log_Space: 690
              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: 1003306
                  Master_UUID: 987ac782-d7b8-11e8-a462-000c292218ec
             Master_Info_File: mysql.slave_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: 
            Executed_Gtid_Set: 19e7075d-d7d2-11e8-8b2d-000c29629b02:1-889,
987ac782-d7b8-11e8-a462-000c292218ec:1-16,
c01b1811-d7b3-11e8-8698-000c29492f7b:1-7
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: m1
           Master_TLS_Version: 
*************************** 2. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 10.220.5.138    <<主2連線成功
                  Master_User: ken
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000009
          Read_Master_Log_Pos: 234
               Relay_Log_File: relay-bin-m2.000002
                Relay_Log_Pos: 320
        Relay_Master_Log_File: mysql-bin.000009
             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: 234
              Relay_Log_Space: 524
              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: 1003307
                  Master_UUID: c01b1811-d7b3-11e8-8698-000c29492f7b
             Master_Info_File: mysql.slave_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: 
            Executed_Gtid_Set: 19e7075d-d7d2-11e8-8b2d-000c29629b02:1-889,
987ac782-d7b8-11e8-a462-000c292218ec:1-16,
c01b1811-d7b3-11e8-8698-000c29492f7b:1-7
                Auto_Position: 0
         Replicate_Rewrite_DB: 
                 Channel_Name: m2
           Master_TLS_Version: 
2 rows in set (0.00 sec)

測試

下面我們就分別在主1以及主2上面建立庫和表,檢查是否可以都同步到從節點上

主1伺服器

 在主1伺服器上面建立一個ken的資料庫,並在裡面建立一張ken1的表,並插入一些資料等待驗證

mysql> create database ken;
Query OK, 1 row affected (0.00 sec)

mysql> use ken;
Database changed
mysql> create table ken1(id int);
Query OK, 0 rows affected (0.02 sec)

mysql> insert into ken1 values(1);
Query OK, 1 row affected (0.05 sec)

mysql> select * from ken1;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

主2伺服器

 在主2伺服器上面建立一個ken6的資料庫,並在裡面建立一張ken的表,並插入一些資料等待驗證

MySQL [(none)]>  create database ken6;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> use ken5;
Database changed
MySQL [ken6]> create table ken(id int);
Query OK, 0 rows affected (0.01 sec)

MySQL [ken6]> insert into ken values(1);
Query OK, 1 row affected (0.05 sec)

MySQL [ken6]> select * from ken;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

從伺服器端

檢視是否有主1以及主2新建的庫表即資料

可以看到下面已經同步過來了主1上面的ken資料庫以及主2上面的ken5

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| ken                |
| ken5               |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
7 rows in set (0.00 sec)

檢視主1同步過來資料

MySQL [(none)]> select * from ken.ken1;
+------+
| id   |
+------+
|    1 |
+------+

檢視主2同步過來的資料

MySQL [ken5]> select * from ken5.ken;
+------+
| id   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

至此,多源複製的架構就完成了。