MySQL的主從複製、半同步複製、主主複製詳解

luashin發表於2016-03-11
   複製其最終目的是讓一臺伺服器的資料和另外的伺服器的資料保持同步,已達到資料冗餘或者服務的負載均衡。一臺主伺服器可以連線多臺從伺服器,並且從伺服器也可以反過來作為主服務 器。主從伺服器可以位於不同的網路拓撲中,由於mysql的強大複製功能,其複製目標可以是所有的資料庫,也可以是某些資料庫,甚至是某個資料庫中的某些 表進行復制。

MySQL支援的兩種複製方案:基於語句複製、基於行復制
   基於語句複製基於行復制,這兩種複製方式都是透過記錄主伺服器的二進位制日誌中任何有可能導致資料庫內資料發生改變的SQL語句到中繼日誌,並且在從伺服器上 執行以下中繼日誌內的SQL語句,而達到與主伺服器的資料同步。不同的是,當主伺服器上執行了一個基於變數的資料並將其更新到資料庫中,如now()函 數,而此時基於語句複製時記錄的就是該SQL語句的整個語法,而基於行復制就是將now()更新到資料庫的數值記錄下來。

例如:在主伺服器上執行以下語句:
mysql> update user set createtime=now() where sid=16;
假如此時now()返回的值是:2012-04-16 20:46:35
基於語句的複製就會將其記錄為:update user set createtime=now() where sid=16;
基於行復制的就會將其記錄為:update user set createtime='2012-04-16 20:46:35' where sid=16;

進行主從複製啟動的三個執行緒
Binlog dump執行緒:將二進位制日誌的內容傳送給從伺服器
I/O從執行緒:將接受的的資料寫入到中繼日誌
SQL執行緒:一次從中繼日誌中讀出一句SQL語句在從伺服器上執行

一、主從複製:
準備工作:
1.修改配置檔案(server_id一定要修改)
2.建立複製使用者
3.啟動從伺服器的從服務程式
規劃:
Master:IP地址:172.16.4.11   版本:mysql-5.5.20
Slave:IP地址:172.16.4.12    版本:mysql-5.5.20

這裡需注意,mysql複製大部分都是後向相容,所以,從伺服器的版本一定要高於或等於主伺服器的版本。
1、Master
修改配置檔案,將其設為mysql主伺服器
# vim /etc/my.cnf
server_id=11                #修改server_id=11
log_bin=mysql-bin            #開啟二進位制日誌
sync_binlog=1               #任何一個事務提交之後就立即寫入到磁碟中的二進位制檔案
innodb_flush_logs_at_trx_commit=1       #任何一個事物提交之後就立即寫入到磁碟中的日誌檔案

儲存退出
# service mysql reload             #重新載入mysql的配置檔案


2、Master上建立使用者,授予複製許可權
mysql> grant replication client,replication slave on *.* to repl@172.16.4.12 identified by '135246';
mysql> flush privileges;


3、Slave
修改配置檔案,將其設定為一個mysql從伺服器
# vim /etc/my.cnf
server_id=12                #修改server_id=12
#log-bin                #註釋掉log-bin,從伺服器不需要二進位制日誌,因此將其關閉
relay-log=mysql-relay                #定義中繼日誌名,開啟從伺服器中繼日誌
relay-log-index=mysql-relay.index     #定義中繼日誌索引名,開啟從伺服器中繼索引
read_only=1                    #設定從伺服器只能進行讀操作,不能進行寫操作

儲存退出
# service mysql reload             #重新載入mysql的配置檔案

4、驗證Slave上中繼日誌以及server_id是否均生效
mysql> show variables like 'relay%';
+-----------------------+-----------------+
| Variable_name         | Value           |
+-----------------------+-----------------+
| relay_log             | relay-bin       |
| relay_log_index       | relay-bin.index |
| relay_log_info_file   | relay-log.info  |
| relay_log_purge       | ON              |
| relay_log_recovery    | OFF             |
| relay_log_space_limit | 0               |
+-----------------------+-----------------+

mysql> show variables like 'server_id';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 12    |
+---------------+-------+

5、啟動從伺服器的從服務程式
場景一、如果主伺服器和從伺服器都是新建立的,並沒有新增其他資料,則執行以下命令:
mysql> change master to master_host='172.16.4.11',master_user='repl',master_password='135246';

mysql> show slave status\G

*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 172.16.4.11
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 25520
              Relay_Log_Space: 2565465
              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: NULL
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: 0

mysql> start slave;

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Queueing master event to the relay log
                  Master_Host: 172.16.4.11
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 107
               Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000001
             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: 360
              Relay_Log_Space: 300
              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: 11

場景二、如果主伺服器已經執行過一段了,從伺服器是新新增的,則需要將主伺服器之前的資料匯入到從伺服器中:
Master:

# mysqldump -uroot -hlocalhost -p123456 --all-databases --lock-all-tables --flush-logs --master-data=2 > /backup/alldatabase.sql
mysql> flush tables with read lock;
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      360 |              |                  |
+------------------+----------+--------------+------------------+
mysql> unlock tables;
# scp /backup/alldatabase.sql 172.16.4.12:/tmp

Slave:
# mysql -uroot -p123456 < /tmp/alldatabase.sql
mysql> change master to master_host='172.16.4.11',master_user='repl',master_password='135246',master_log_file='mysql-bin.000004',master_log_pos=360;

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State:
                  Master_Host: 172.16.4.11
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 360
               Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000004
             Slave_IO_Running: No
            Slave_SQL_Running: No
              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: 360
              Relay_Log_Space: 107
              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: NULL
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: 0

mysql> start slave;

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Queueing master event to the relay log
                  Master_Host: 172.16.4.11
                  Master_User: repl
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000004
          Read_Master_Log_Pos: 360
               Relay_Log_File: relay-bin.000001
                Relay_Log_Pos: 4
        Relay_Master_Log_File: mysql-bin.000004
             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: 360
              Relay_Log_Space: 300
              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: 11

說明MySQL的主從複製架構成功
注1:MySQL的複製可以基於某個資料庫或庫中的默寫表進行復制,要想實現該功能,只需在其配置檔案中新增以下配置:
Master:
binlog-do-db=db_name        只複製db_name資料庫
binlog-ignore-db=db_name    不復制db_name資料庫

注2:在Master上定義過濾規則,意味著,任何不涉及到該資料庫相關的寫操作都不會被記錄到二進位制日誌中,因此不建議在Master上定義過濾規則,並且不建議binlog-do-db與binlog-ignore-db同時定義。
Slave:
replicate_do_db=db_name              只複製db_name資料庫
replicate_ignore_db=db_name          不復制db_name資料庫
replicate_do_table=tb_name           只複製tb_name表
replicate_ignore_table=tb_name       只複製tb_name表
replicate_wild_do_table=test%        只複製以test為開頭並且後面跟上任意字元的名字的表
replicate_wild_ignore_table=test_    只複製以test為開頭並且後面跟上任意單個字元的名字的表

注3:如果需要指定多個db或table時,則只需將命令多次寫入

二、半同步複製
   由於Mysql的複製都是基於非同步進行的,在特殊情況下不能保證資料的成功複製,因此在mysql 5.5之後使用了來自google補丁,可以將Mysql的複製實現半同步模式。所以需要為主伺服器載入對應的外掛。在Mysql的安裝目錄下的 lib/plugin/目錄中具有對應的外掛semisync_master.so,semisync_slave.so

在Master和Slave的mysql命令列執行如下命令:
Master:
mysql> install plugin rpl_semi_sync_master soname 'semisync_master.so'; 
mysql> set global rpl_semi_sync_master_enabled = 1; 
mysql> set global rpl_semi_sync_master_timeout = 1000; 
mysql> show variables like '%semi%';
+------------------------------------+-------+
| Variable_name                      | Value |
+------------------------------------+-------+
| rpl_semi_sync_master_enabled       | ON    |
| rpl_semi_sync_master_timeout       | 1000  |
| rpl_semi_sync_master_trace_level   | 32    |
| rpl_semi_sync_master_wait_no_slave | ON    |
+------------------------------------+-------+

Slave:
mysql> install plugin rpl_semi_sync_slave soname 'semisync_slave.so'; 
mysql> set global rpl_semi_sync_slave_enabled = 1; 
mysql> stop slave;
mysql> start slave;
mysql> show variables like '%semi%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| rpl_semi_sync_slave_enabled     | ON    |
| rpl_semi_sync_slave_trace_level | 32    |
+---------------------------------+-------+

檢查半同步是否生效:
Master:
mysql> show global status like 'rpl_semi%';
+--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 1     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 0     |
| Rpl_semi_sync_master_no_times              | 0     |
| Rpl_semi_sync_master_no_tx                 | 0     |
| Rpl_semi_sync_master_status                | ON    |
| Rpl_semi_sync_master_timefunc_failures     | 0     |
| Rpl_semi_sync_master_tx_avg_wait_time      | 0     |
| Rpl_semi_sync_master_tx_wait_time          | 0     |
| Rpl_semi_sync_master_tx_waits              | 0     |
| Rpl_semi_sync_master_wait_pos_backtraverse | 0     |
| Rpl_semi_sync_master_wait_sessions         | 0     |
| Rpl_semi_sync_master_yes_tx                | 0     |
+--------------------------------------------+-------+
說明半同步成功。

讓半同步功能在MySQL每次啟動都自動生效,在Master和Slave的my.cnf中編輯:
Master:
[mysqld] 
rpl_semi_sync_master_enabled=1 
rpl_semi_sync_master_timeout=1000     #1秒

Slave:
[mysqld] 
rpl_semi_sync_slave_enabled=1 

也可透過設定全域性變數的方式來設定是否啟動半同步外掛:
Master:
mysql> set global rpl_semi_sync_master_enabled=1
取消載入外掛
mysql> uninstall plugin rpl_semi_sync_master;

Slave:
mysql> set global rpl_semi_sync_slave_enabled = 1;
mysql> uninstall plugin rpl_semi_sync_slave;

三、主主複製架構
1、在兩臺伺服器上各自建立一個具有複製許可權的使用者;
Master:
mysql> grant replication client,replication slave on *.* to repl@172.16.4.12 identified by '135246';
mysql> flush privileges;

Slave:
mysql> grant replication client,replication slave on *.* to repl@172.16.4.11 identified by '135246';
mysql> flush privileges;

2、修改配置檔案:
Master:
[mysqld]
server-id = 11
log-bin = mysql-bin
auto-increment-increment = 2
auto-increment-offset = 1
relay-log=mysql-relay           
relay-log-index=mysql-relay.index

Slave:
[mysqld]
server-id = 12
log-bin = mysql-bin
auto-increment-increment = 2
auto-increment-offset = 2
relay-log=mysql-relay           
relay-log-index=mysql-relay.index

3、如果此時兩臺伺服器均為新建立,且無其它寫入操作,各伺服器只需記錄當前自己二進位制日誌檔案及事件位置,以之作為另外的伺服器複製起始位置即可
Master:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000004 |      360 |              |                  |
+------------------+----------+--------------+------------------+

Slave:
mysql> show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000005 |      107 |              |                  |
+------------------+----------+--------------+------------------+

4、各伺服器接下來指定對另一臺伺服器為自己的主伺服器即可:
Master:
mysql> change master to master_host='172.16.4.12',master_user='repl',master_password='135246',master_log_file='mysql-bin.000005',
master_log_pos=107;

Slave:
mysql> change master to master_host='172.16.4.11',master_user='repl',master_password='135246',
master_log_file='mysql-bin.000004',master_log_pos=360;

5、啟動從伺服器執行緒:
Master:
mysql> start slave;

Slave:
mysql> start slave;
到此主主架構已經成功!

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/9034054/viewspace-2055784/,如需轉載,請註明出處,否則將追究法律責任。

相關文章