mysql 主從複製

MySQL複製容許將主實例(master)上的數據同步到一個或多個從實例(slave)上,默認狀況下複製是異步進行的,從庫也不須要一直鏈接到主庫來同步數據
MySQL複製的數據粒度能夠是主實例上全部的數據庫,也能夠是指定的一個或多個數據庫,也能夠是一個數據庫裏的指定的表
MySQL複製所帶來的優點在於:
• 擴展能力:經過複製功能能夠將MySQL的性能壓力分擔到一個或多個slave上。這要求全部 的寫操做和修改操做都必須在Master上完成,而讀操做能夠被分配到一個或多個slave上。 將讀寫分離到不一樣服務器執行以後,MySQL的讀寫性能獲得提高
• 數據庫備份:因爲從實例是同步主實例的數據,因此能夠將備份做業部署到從庫
• 數據分析和報表:一樣,一些數據分析和報表的實現能夠在從實例執行,以減小對主庫的性能影響
• 容災能力:能夠在物理距離較遠的另外一個數據中心創建一個slave,保證在主實例所在地區遭遇災難時,在另外一個數據中心能快速恢復;

mysql

MySQL複製有兩種方法:
• 傳統方式:基於主庫的bin-log將日誌事件和事件位置複製到從庫,從庫再加以 應用來達到主從同步的目的
• Gtid方式:global transaction identifiers是基於事務來複制數據,所以也就不依賴日誌文件位置,同時又能更好的保證主從庫數據一致性
MySQL複製有多種類型:
• 異步複製:一個主庫,一個或多個從庫,數據異步同步到從庫
• 同步複製:在MySQL Cluster中特有的複製方式
• 半同步複製:在異步複製的基礎上,確保任何一個主庫上的事務在提交以前至 少有一個從庫已經收到該事務並日志記錄下來
• 延遲複製:在異步複製的基礎上,人爲設定主庫和從庫的數據同步延遲時間,即保證數據延遲至少是這個參數linux

複製的工做原理是數據庫修改事件記錄到bin log中並傳遞到slave,而後slave在本地還原的過程。而事件記錄到bin log的格式會有所不一樣。
MySQL複製有三種核心格式
• 基於語句的複製(statement based replication):基於主庫將SQL語句寫入到 bin log中完成複製
• 基於行數據的複製(row based replication):基於主庫將每個行數據變化的信息做爲事件寫入到bin log中完成日誌
• 混合複製(mixed based replication):上述二者的結合。默認狀況下優先使用基於語句的複製,只有當部分語句若是基於語句複製不安全的狀況下才會自動切換爲基於行數據的複製sql

主庫 mysql> show variables like '%binlog_format%'; +---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.01 sec) mysql> show variables like '%log_bin%'; +---------------------------------+------------------------------------+
| Variable_name                   | Value                              |
+---------------------------------+------------------------------------+
| log_bin                         | ON                                 |
| log_bin_basename                | /usr/local/mysql/data/binlog       |
| log_bin_index                   | /usr/local/mysql/data/binlog.index |
| log_bin_trust_function_creators | OFF                                |
| log_bin_use_v1_row_events       | OFF                                |
| sql_log_bin                     | ON                                 |
+---------------------------------+------------------------------------+
6 rows in set (0.01 sec) 主庫和每一個從庫都必須有一個惟一ID,server_id; mysql> show variables like '%server_id%'; +----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 1     |
| server_id_bits | 32    |
+----------------+-------+
2 rows in set (0.01 sec)

從庫1和2配置相同數據庫

從庫
mysql> show variables like '%log_bin%';
+---------------------------------+------------------------------------+
| Variable_name                   | Value                              |
+---------------------------------+------------------------------------+
| log_bin                         | ON                                 |
| log_bin_basename                | /usr/local/mysql/data/binlog       |
| log_bin_index                   | /usr/local/mysql/data/binlog.index |
| log_bin_trust_function_creators | OFF                                |
| log_bin_use_v1_row_events       | OFF                                |
| sql_log_bin                     | ON                                 |
+---------------------------------+------------------------------------+
6 rows in set (0.03 sec)

mysql> show variables like '%server_id%';
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| server_id      | 2     |
| server_id_bits | 32    |
+----------------+-------+
2 rows in set (0.00 sec)


mysql>  show variables like '%binlog_format%'; 
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | ROW   |
+---------------+-------+
1 row in set (0.00 sec)

在主庫建立一個專門用來作複製的數據庫用戶,這樣全部從庫均可以用這個用戶來鏈接主庫,也能夠確保這個用戶只有複製的權限雖然能夠用任何擁有複製權限的MySQL用戶來創建複製關係,但因爲被使用的用戶名和密碼會明文保存在備庫的master.info文件中,因此爲安全起見,最好是使用僅有複製權限的獨立用戶centos

建立主從複製用戶 主庫 mysql> create user 'repl'@'192.168.204.%' identified by 'mysql'; Query OK, 0 rows affected (0.12 sec) mysql> grant replication slave on *.* to 'repl'@'192.168.204.%'; Query OK, 0 rows affected (0.07 sec)

mysql 基於binlog 複製,初始化方法1安全

主庫上鎖 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 | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000008 |      709 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) 主庫 [root@centos7 ~]# mysqldump --all-databases --master-data -u root -p > dbdump.db Enter password: 
mysql> unlock tables; [root@centos7
~]# ls anaconda-ks.cfg dbdump.db mysql-8.0.13-linux-glibc2.12-x86_64.tar.xz [root@centos7 ~]# sftp -oPort=22 192.168.204.133 root@192.168.204.133's password: Connected to 192.168.204.133. sftp> put dbdump.db Uploading dbdump.db to /root/dbdump.db dbdump.db 100% 906KB 905.8KB/s 00:00 從庫導入 mysql> source dbdump.db; mysql> show databases; +--------------------+ | Database | +--------------------+ | A1 | | A2 | | course | | course2 | | course3 | | course4 | | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 10 rows in set (0.01 sec)

方法2服務器

拷文件的方式 主庫和從庫都關閉 主庫 [root@centos7 ~]# /etc/init.d/mysql.server stop Shutting down MySQL..... SUCCESS! [root@centos7 ~]# cd /usr/local/mysql/data/ [root@centos7 data]# cd .. [root@centos7 mysql]# tar -zcvf data.tar.gz ./data/ [root@centos7 mysql]# sftp -oPort=22 root@192.168.204.133 root@192.168.204.133's password: 
Permission denied, please try again. root@192.168.204.133's password: 
Connected to 192.168.204.133. sftp> lls bin data data.tar.gz docs include lib LICENSE LICENSE.router man README README.router share support-files sftp> put data.tar.gz Uploading data.tar.gz to /root/data.tar.gz data.tar.gz 100% 5594KB   5.5MB/s   00:00 sftp> 從庫 [root@centos7 mysql]# mv data data_bak [root@centos7 mysql]# tar xf data.tar.gz [root@centos7 mysql]# ll 總用量 6080 drwxr-xr-x.  2 mysql mysql    4096 2月  17 10:05 bin drwxr-xr-x. 12 mysql mysql    4096 3月  30 14:32 data drwxr-xr-x.  6 mysql mysql    4096 3月  30 19:25 data_bak -rw-r--r--.  1 mysql mysql 5751938 3月  30 14:42 data.tar.gz drwxr-xr-x.  2 mysql mysql      82 2月  17 10:05 docs drwxr-xr-x.  3 mysql mysql    4096 2月  17 10:04 include drwxr-xr-x.  6 mysql mysql    4096 2月  17 10:05 lib -rw-r--r--.  1 mysql mysql  335809 10月  7 16:44 LICENSE -rw-r--r--.  1 mysql mysql  101807 10月  7 16:44 LICENSE.router drwxr-xr-x.  4 mysql mysql      28 2月  17 10:04 man -rw-r--r--.  1 mysql mysql     687 10月  7 16:44 README -rw-r--r--.  1 mysql mysql     700 10月  7 16:44 README.router drwxr-xr-x. 28 mysql mysql    4096 2月  17 10:05 share drwxr-xr-x.  2 mysql mysql      86 2月  17 10:14 support-files [root@centos7 mysql]# cd data [root@centos7 data]# rm auto.cnf -f

建立主從關係session

[root@centos7 data]# /etc/init.d/mysql.server start mysql> mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.204.132', -> MASTER_PORT=3306, -> MASTER_USER='repl', -> MASTER_PASSWORD='mysql', -> MASTER_LOG_FILE='binlog.000008', -> MASTER_LOG_POS=709; Query OK, 0 rows affected, 2 warnings (0.01 sec) mysql> start slave; Query OK, 0 rows affected (0.02 sec) mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.204.132 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000009 Read_Master_Log_Pos: 155 Relay_Log_File: centos7-relay-bin.000003 Relay_Log_Pos: 363 Relay_Master_Log_File: binlog.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: 155 Relay_Log_Space: 737 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: 1 Master_UUID: e30d70f7-325a-11e9-811b-000c2940fa33 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: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0
1 row in set (0.00 sec) mysql> stop slave; Query OK, 0 rows affected (0.03 sec)

建立主從複製報錯app

主從複製保錯 Last_IO_Error: Fatal error: The slave I/O thread stops because master and slave have equal MySQL server ids; Last_IO_Error: error connecting to master 'repl@192.168.204.132:3306' - retry-time: 60  retries: 1 Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find first log file name in binary log index file' 重置主從關係 主庫操做 mysql> stop slave; mysql> reset slave all;

若是主庫不能鎖表,主庫上有業務操做異步

模擬操做 mysql> use A1; mysql> delimiter // mysql> create procedure proc1() -> begin -> declare n int default 1; -> while n<=20000 do
    -> insert into temp values(n,'mike'); -> set n=n+1; -> end while; -> end; -> // mysql> delimiter ; mysql> show master status; +---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000009 |     1158 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) mysql> create table temp(id int,name varchar(20)); Query OK, 0 rows affected (0.03 sec) mysql> call proc1(); Query OK, 1 row affected (2 min 10.79 sec)

錯誤的方式建立主從關係

mysql> show master status; +---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000009 |  5642155 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) mysql> call proc1(); Query OK, 1 row affected (31.77 sec) [root@centos7 ~]# mysqldump --all-databases --master-data -u root -p > dbdump.db Enter password: mysql> select count(*) from temp; +----------+
| count(*) |
+----------+
|    20000 |
+----------+
1 row in set (0.02 sec) [root@centos7 ~]# sftp -oPort=22 192.168.204.133 root@192.168.204.133's password: 
Connected to 192.168.204.133. sftp> put dbdump.db Uploading dbdump.db to /root/dbdump.db dbdump.db 100%  965KB 964.6KB/s   00:00 sftp> 從庫 mysql> source dbdump.db mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.204.132', -> MASTER_PORT=3306, -> MASTER_USER='repl', -> MASTER_PASSWORD='mysql', -> MASTER_LOG_FILE='binlog.000009', -> MASTER_LOG_POS=5642155; Query OK, 0 rows affected, 2 warnings (0.04 sec) mysql> start slave; Query OK, 0 rows affected (0.10 sec) mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.204.132 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000009 Read_Master_Log_Pos: 11282155 Relay_Log_File: centos7-relay-bin.000002 Relay_Log_Pos: 3577207 Relay_Master_Log_File: binlog.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: 9219043 Relay_Log_Space: 5640529 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: 124 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: 1 Master_UUID: e30d70f7-325a-11e9-811b-000c2940fa33 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: waiting for handler commit 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 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0
1 row in set (0.00 sec)

從庫報錯數據不一致

mysql> select count(*) from temp; +----------+
| count(*) |
+----------+
|    24230 |
+----------+
1 row in set (0.01 sec) mysql> select id,count(*) from temp group by id having count(*)>=2; +------+----------+
| id   | count(*) |
+------+----------+
| 1794 |        2 |
| 1795 |        2 |
| 1796 |        2 |
| 1797 |        2 |
| 1798 |        2 |
| 1799 |        2 |
| 1800 |        2 |

正確方案

正確方案 主庫操做 mysqldump --all-databases --master-data=2 -u root -p > dbdump.db mysqldump --all-databases --master-data=2 --single-transaction -u root -p >dbdump.db vi dbdump.db CHANGE MASTER TO MASTER_LOG_FILE='binlog.000009', MASTER_LOG_POS=18811367; 從庫操做 mysql> reset slave all; Query OK, 0 rows affected (0.01 sec) mysql> mysql> mysql> CHANGE MASTER TO -> MASTER_HOST='192.168.204.132', -> MASTER_PORT=3306, -> MASTER_USER='repl', -> MASTER_PASSWORD='mysql', -> MASTER_LOG_FILE='binlog.000009', -> MASTER_LOG_POS=18811367; Query OK, 0 rows affected, 2 warnings (0.05 sec) mysql> start slave; Query OK, 0 rows affected (0.04 sec) mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.204.132 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000009 Read_Master_Log_Pos: 22964381 Relay_Log_File: centos7-relay-bin.000002 Relay_Log_Pos: 2073019 Relay_Master_Log_File: binlog.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: 20884067 Relay_Log_Space: 4153543 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: 429 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: 1 Master_UUID: e30d70f7-325a-11e9-811b-000c2940fa33 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: waiting for handler commit 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 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0
1 row in set (0.00 sec) ERROR: No query specified mysql> show databases; +--------------------+
| Database           |
+--------------------+
| A1                 |
| A2                 |
| A3                 |
| course             |
| course2            |
| course3            |
| course4            |
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
11 rows in set (0.00 sec) mysql> use A1; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select count(*) from temp; +----------+
| count(*) |
+----------+
|    20000 |
+----------+
1 row in set (0.01 sec)

第二個從節點建立主從關係方法同上

第二個從節點 [root@oldboy-mysql-slave2 ~]# vi dbdump.db  #查看MASTER_LOG_FILE/MASTER_LOG_POS root@oldboy-mysql-slave2 ~]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 9 Server version: 8.0.13 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. mysql> source dbdump.db mysql> CHANGE MASTER TO -> MASTER_HOST='10.0.0.200', -> MASTER_PORT=3306, -> MASTER_USER='repl', -> MASTER_PASSWORD='mysql', -> MASTER_LOG_FILE='binlog.000043', -> MASTER_LOG_POS=7570317; Query OK, 0 rows affected, 2 warnings (0.07 sec) mysql> start slave; Query OK, 0 rows affected (0.06 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.200 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000043 Read_Master_Log_Pos: 11644030 Relay_Log_File: oldboy-mysql-slave2-relay-bin.000002 Relay_Log_Pos: 226135 Relay_Master_Log_File: binlog.000043 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: 7796133 Relay_Log_Space: 4074254 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: 2947 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: 1 Master_UUID: fc58cc2d-164b-11e9-95af-000c29129a95 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: waiting for handler commit 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 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0
1 row in set (0.00 sec)

方法2 利用從庫1

[root@oldboy-mysql-slave1 mysql]#/etc/init.d/mysql.server stop [root@oldboy-mysql-slave1 mysql]# tar -zcvf data.tar.gz data data/ data/ibdata1 data/ib_logfile1 data/undo_001 data/undo_002 data/ib_logfile0 [root@oldboy-mysql-slave1 mysql]# sftp root@10.0.0.202 The authenticity of host '10.0.0.202 (10.0.0.202)' can't be established.
ECDSA key fingerprint is SHA256:/yqIM0T3ZqFIt1SdWZb50q8qffjj7PbwKr+aLXFSw+4. ECDSA key fingerprint is MD5:6c:33:8b:79:5b:b3:a6:5e:78:b8:e6:4f:47:bf:0b:07. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '10.0.0.202' (ECDSA) to the list of known hosts. root@10.0.0.202's password: 
Connected to 10.0.0.202. sftp> put data.tar.gz Uploading data.tar.gz to /root/data.tar.gz data.tar.gz 100%  285MB  27.7MB/s   00:10 sftp> exit 二從節點 [root@oldboy-mysql-slave2 mysql]# tar -zxvf data.tar.gz data/ data/ibdata1 data/ib_logfile1 data/undo_001 data/undo_002 data/ib_logfile0 data/#innodb_temp/ data/mysql.ibd [root@oldboy-mysql-slave2 data]# rm -rf auto.cnf [root@oldboy-mysql-slave2 data]# /etc/init.d/mysql.server start Starting MySQL.Logging to '/data/mysql/mysql/data/oldboy-mysql-slave2.err'. .... SUCCESS! [root@oldboy-mysql-slave2 data]# mysql -u root -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.13 MySQL Community Server - GPL Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Master_Host: 10.0.0.200 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000043 Read_Master_Log_Pos: 11644030 Relay_Log_File: oldboy-mysql-slave1-relay-bin.000002 Relay_Log_Pos: 4074032 Relay_Master_Log_File: binlog.000043 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: 11644030 Relay_Log_Space: 0 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 Master_UUID: fc58cc2d-164b-11e9-95af-000c29129a95 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: 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 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0
1 row in set (0.00 sec) 我在這裏就已經成功了

聯級複製

從庫打開log_slave_updates,主庫修改數據會記錄到從庫的binlog中,用於從庫之間作聯級複製 mysql> show variables like "%log_slave_updates%"; +-------------------+-------+
| Variable_name     | Value |
+-------------------+-------+
| log_slave_updates | ON    |
+-------------------+-------+
1 row in set (0.12 sec) mysql> show variables like "%log_bin%"; +---------------------------------+------------------------------------+
| Variable_name                   | Value                              |
+---------------------------------+------------------------------------+
| log_bin                         | ON                                 |
| log_bin_basename                | /usr/local/mysql/data/binlog       |
| log_bin_index                   | /usr/local/mysql/data/binlog.index |
| log_bin_trust_function_creators | OFF                                |
| log_bin_use_v1_row_events       | OFF                                |
| sql_log_bin                     | ON                                 |
+---------------------------------+------------------------------------+
6 rows in set (0.00 sec) [root@mysql-slave1 data]# mysqlbinlog -v binlog.000007 > abc.log ### INSERT INTO `test`.`dept2` ### SET ### @1=1 ### @2='math' # at 895587 #190401 22:58:11 server id 1  end_log_pos 895618 CRC32 0xeff2d5c4       Xid = 531 COMMIT/*!*/; SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/; DELIMITER ; # End of log file /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/; [root@mysql-slave1 data]# cat /etc/my.cnf [mysqld] server-id=2 log_slave_updates=1 replicate-do-db=test

replicate-do-db:該參數用來指定須要複製的數據庫。在基於語句複製的環境中,指定該參數以後,則slave的SQL thread進程只會應用在本數據庫下的對象相關的語句。若是有多個數據庫須要複製,則這

個參數要使用屢次。但若是是涉及到跨庫操做語句,則複製會丟失;

[root@mysql-slave1 data]# /etc/init.d/mysql.server restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.138.131 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000007 Read_Master_Log_Pos: 1490 Relay_Log_File: mysql-slave1-relay-bin.000006 Relay_Log_Pos: 319 Relay_Master_Log_File: binlog.000007 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: test 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: 1490 Relay_Log_Space: 534 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: 1 Master_UUID: 849ecaa5-3386-11e9-9db6-000c29c16499 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: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0
1 row in set (0.00 sec) ERROR: No query specified 主庫 mysql> use test; Database changed mysql> insert into dept2 values(3,'chinese'); Query OK, 1 row affected (0.07 sec) mysql> use course; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from test; +------+-------+
| id   | name  |
+------+-------+
|    1 | houpj |
+------+-------+
1 row in set (0.00 sec) mysql> insert into test values(2,'wang'); Query OK, 1 row affected (0.07 sec) 從庫 只有test庫更新 mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from dept2; +------+---------+
| id   | name    |
+------+---------+
|    1 | history |
|    1 | math    |
|    3 | chinese |
+------+---------+
3 rows in set (0.00 sec) mysql> use course; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> select * from test; +------+-------+
| id   | name  |
+------+-------+
|    1 | houpj |
+------+-------+
1 row in set (0.00 sec)

另外一個基於SQL語句複製和基於行復制的區別在於當語句中包含對多個數據庫的表進行
操做時。好比設置replicate-do-db=db1,
USE db1;
UPDATE db1.table1 SET col1 = 10, db2.table2 SET col2 = 20;
基於SQL語句的複製會將table1和table2都在備庫修改,而基於行的複製只會在備庫修改table1表

主庫修改 [root@mysql ~]# cat /etc/my.cnf [mysqld] server-id=1 binlog-format=statement mysql> use test; Database changed mysql> update course.test2 set name='jiaojiao'; Query OK, 2 rows affected (0.03 sec) Rows matched: 2  Changed: 2  Warnings: 0 mysql> use course; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> update test.dept2 set name='pengju'; Query OK, 3 rows affected (0.00 sec) Rows matched: 3  Changed: 3  Warnings: 0 從庫1 [root@mysql-slave1 ~]# cat /etc/my.cnf [mysqld] server-id=2 log_slave_updates=1 replicate-do-db=test mysql> use test; Database changed mysql> select * from dept2; +------+-------+
| id   | name  |
+------+-------+
|    1 | houpj |
|    1 | houpj |
|    3 | houpj |
+------+-------+
3 rows in set (0.00 sec) mysql> use course; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A mysql> select * from test2; +----+----------+
| id | name     |
+----+----------+
|  1 | jiaojiao |
|  2 | jiaojiao |
+----+----------+
2 rows in set (0.00 sec)

replicate-do-table=db_name.tbl_name:經過該參數告知slave的SQL thread僅複製指定表上的數據。若是有多個表,則該參數要使用屢次replicate-wild-do-table=db_name.tbl_name:經過該參數告知SQL的SQL thread僅複製符合匹配的表,可使用_和%做爲通配符。好比replicate-wild-do-table=foo%.bar%表示複製以foo打頭的數據庫下全部bar打頭的表數據。若是是replicate-wild-do-table=foo%.%,則表示即複製foo打頭的全部表的數據,也複製create/drop/alter database foo打頭的命令

主庫 mysql> use test; Database changed mysql> show tables; +----------------+
| Tables_in_test |
+----------------+
| course         |
| dept           |
| dept2          |
| dept3          |
| students       |
| students2      |
| teacher        |
| teacher_backup |
+----------------+
8 rows in set (0.00 sec) mysql> insert into dept3 values(1,'english'),(2,'math'),(3,'miniso'); Query OK, 3 rows affected (0.10 sec) Records: 3  Duplicates: 0  Warnings: 0 mysql> select * from dept2; +------+-------+
| id   | name  |
+------+-------+
|    1 | houpj |
|    1 | houpj |
|    3 | houpj |
+------+-------+
3 rows in set (0.00 sec) mysql> insert into dept2 values(1,'english'),(2,'math'),(3,'miniso'); Query OK, 3 rows affected (0.04 sec) Records: 3  Duplicates: 0  Warnings: 0 從庫1 [root@mysql-slave1 mysql]# cat /etc/my.cnf [mysqld] server-id=2 log_slave_updates=1 replicate-do-table=test.dept3 mysql> use test; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> show tables; +----------------+
| Tables_in_test |
+----------------+
| course         |
| dept           |
| dept2          |
| dept3          |
| students       |
| students2      |
| teacher        |
| teacher_backup |
+----------------+
8 rows in set (0.00 sec) 沒變化 mysql> select * from dept2; +------+-------+
| id   | name  |
+------+-------+
|    1 | houpj |
|    1 | houpj |
|    3 | houpj |
+------+-------+
3 rows in set (0.00 sec) 有變化 mysql> select * from dept3; +------+---------+
| id   | name    |
+------+---------+
|    1 | english |
|    2 | math    |
|    3 | miniso  |
+------+---------+
3 rows in set (0.00 sec)

show processlist

在主庫能夠經過執行show processlist命令查看主庫的bin log日誌生成進程 mysql> show processlist\G; *************************** 1. row *************************** Id: 4 User: event_scheduler Host: localhost db: NULL Command: Daemon Time: 4882 State: Waiting on empty queue Info: NULL *************************** 2. row *************************** Id: 14 User: root Host: localhost db: test Command: Query Time: 0 State: starting Info: show processlist *************************** 3. row *************************** Id: 17 User: repl Host: 192.168.138.133:49137 db: NULL Command: Binlog Dump Time: 4653 State: Master has sent all binlog to slave; waiting for more updates Info: NULL *************************** 4. row *************************** Id: 21 User: repl Host: 192.168.138.132:43994 db: NULL Command: Binlog Dump Time: 902 State: Master has sent all binlog to slave; waiting for more updates Info: NULL 4 rows in set (0.00 sec)

在statement(sql)模式下實驗主從一個表數據不一致的狀況下複製是否還能繼續

從庫操做 mysql> delete from dept3; Query OK, 3 rows affected (0.10 sec) 主庫操做 mysql> select * from dept3; +------+---------+
| id   | name    |
+------+---------+
|    1 | english |
|    2 | math    |
|    3 | miniso  |
+------+---------+
3 rows in set (0.00 sec) mysql> insert into dept3 values(4,'history'); Query OK, 1 row affected (0.06 sec) 從庫狀態 mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.138.131 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000008 Read_Master_Log_Pos: 2952 Relay_Log_File: mysql-slave1-relay-bin.000016 Relay_Log_Pos: 625 Relay_Master_Log_File: binlog.000008 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: 2952 Relay_Log_Space: 1837 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: 1 Master_UUID: 849ecaa5-3386-11e9-9db6-000c29c16499 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: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0
1 row in set (0.00 sec)

row模式

從庫 mysql> select * from dept3; +------+---------+
| id   | name    |
+------+---------+
|    4 | history |
+------+---------+
1 row in set (0.00 sec) 主庫操做 mysql> select * from dept3; +------+---------+
| id   | name    |
+------+---------+
|    1 | english |
|    2 | math    |
|    3 | miniso  |
|    4 | history |
+------+---------+
4 rows in set (0.00 sec) mysql> delete from dept3 where id=1; Query OK, 1 row affected (0.09 sec) 從庫狀態 mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.138.131 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000009 Read_Master_Log_Pos: 445 Relay_Log_File: mysql-slave1-relay-bin.000019 Relay_Log_Pos: 363 Relay_Master_Log_File: binlog.000009 Slave_IO_Running: Yes 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: 1032 Last_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 5 failed executing transaction 'ANONYMOUS' at master log binlog.000009, end_log_pos 414. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any. Skip_Counter: 0 Exec_Master_Log_Pos: 155 Relay_Log_Space: 1032 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: 1032 Last_SQL_Error: Coordinator stopped because there were error(s) in the worker(s). The most recent failure being: Worker 5 failed executing transaction 'ANONYMOUS' at master log binlog.000009, end_log_pos 414. See error log and/or performance_schema.replication_applier_status_by_worker table for more details about this failure or others, if any. Replicate_Ignore_Server_Ids: Master_Server_Id: 1 Master_UUID: 849ecaa5-3386-11e9-9db6-000c29c16499 Master_Info_File: mysql.slave_master_info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: 190402 05:23:51 Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0
1 row in set (0.00 sec)

slave-skip-errors=[err_code1,err_code2,...|all|ddl_exist_errors]:該參數決定了當slave的SQL thread執行過程當中碰到何種錯誤時能夠忽略並繼續接下來的數據複製。正常狀況下當有錯誤發生時,複製會中止而須要人工干預修復才能繼續進行。除非很是自信能夠忽略某些錯誤,不然不要使用這個參數,否則會致使雖然複製執行正常,但其實內部的數據已經徹底不一致;

跳過錯誤 [root@mysql-slave1 ~]# cat /etc/my.cnf [mysqld] server-id=2 log_slave_updates=1 slave-parallel-workers=5 slave-skip-errors=1050 重啓mysql mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.138.131 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000009 Read_Master_Log_Pos: 782 Relay_Log_File: mysql-slave1-relay-bin.000006 Relay_Log_Pos: 319 Relay_Master_Log_File: binlog.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: 782 Relay_Log_Space: 1547 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: 1 Master_UUID: 849ecaa5-3386-11e9-9db6-000c29c16499 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: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0
1 row in set (0.00 sec)

默認建立的MySQL複製是異步的,意味着主庫將數據庫修改事件寫入到本身的bin log,而並不知道從庫是否獲取了這些事件並應用在本身身上。因此當主庫崩潰致使要主從切換時,有可能從庫上的數據不是最新的
從5.7版本開始MySQL經過擴展的方式支持了半同步複製當主庫執行一個更新操做事務時,提交操做會被阻止直到至少有一個半同步的複製slave確認已經接收到本次更新操做,主庫的提交操做纔會繼續
半同步複製的slave發送確認消息只會在本次更新操做記錄已經記錄到本地的relay log以後若是沒有任何slave發送確認消息而致使超時時,半同步複製會轉換成異步複製
半同步複製會對MySQL性能產生影響,由於主庫的提交動做只有在收到至少一個從庫的確認消息以後才能執行。但這個功能是性能和數據可靠性方面的權衡

主庫master mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so'; Query OK, 0 rows affected (0.19 sec) mysql> SET GLOBAL rpl_semi_sync_master_enabled = 1; Query OK, 0 rows affected (0.00 sec) mysql> SHOW STATUS LIKE 'rpl_semi_sync%'; +--------------------------------------------+-------+
| 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     |
+--------------------------------------------+-------+
14 rows in set (0.01 sec) 從庫 mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; Query OK, 0 rows affected (0.25 sec) mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1; Query OK, 0 rows affected (0.01 sec) mysql> stop slave; Query OK, 0 rows affected (0.11 sec) mysql> start slave; Query OK, 0 rows affected (0.05 sec) 從庫 mysql> STOP SLAVE IO_THREAD; Query OK, 0 rows affected (0.09 sec) 主庫 mysql> update dept2 set name="haohan" where id=3; Query OK, 2 rows affected (10.03 sec) Rows matched: 2  Changed: 2  Warnings: 0 mysql> show status like 'rpl_semi_sync%'; +--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 0     |
| 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              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| Rpl_semi_sync_master_status                | OFF   |
| 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     |
+--------------------------------------------+-------+
14 rows in set (0.03 sec) 從庫 mysql> START SLAVE IO_THREAD; Query OK, 0 rows affected (0.03 sec) 主庫 mysql> show status like 'rpl_semi_sync%'; +--------------------------------------------+-------+
| 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             | 1     |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| 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     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec) 從庫2 mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so'; Query OK, 0 rows affected (0.19 sec) mysql> SET GLOBAL rpl_semi_sync_slave_enabled =1; Query OK, 0 rows affected (0.01 sec) mysql> stop slave; Query OK, 0 rows affected (0.05 sec) mysql> start slave; Query OK, 0 rows affected (0.05 sec) mysql> show status like 'rpl_semi_sync%'; +--------------------------------------------+-------+
| Variable_name                              | Value |
+--------------------------------------------+-------+
| Rpl_semi_sync_master_clients               | 2     |
| Rpl_semi_sync_master_net_avg_wait_time     | 0     |
| Rpl_semi_sync_master_net_wait_time         | 0     |
| Rpl_semi_sync_master_net_waits             | 1     |
| Rpl_semi_sync_master_no_times              | 1     |
| Rpl_semi_sync_master_no_tx                 | 1     |
| 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     |
+--------------------------------------------+-------+
14 rows in set (0.00 sec) 兩個從庫 slave1 mysql> stop slave io_thread; Query OK, 0 rows affected (0.00 sec) master mysql> insert into test2 values(3,'houhou'); Query OK, 1 row affected (0.09 sec) slave2 mysql> stop slave io_thread; Query OK, 0 rows affected (0.01 sec) master mysql> insert into test2 values(4,'qita'); Query OK, 1 row affected (10.16 sec)

延遲複製是指定從庫對主庫的延遲至少是指定的這個間隔時間,默認是0秒。能夠經過change master to命令來指定
CHANGE MASTER TO MASTER_DELAY = N;其原理是從庫收到主庫的bin log以後,不是當即執行,而是等待指定的秒數以後再執行
延遲複製的使用場景好比:
確保在主庫上被錯誤修改的數據能及時找回測試在從庫IO集中在恢復bin log過程當中對應用程序的訪問影響保留一份若干天前的數據庫狀態,和當前狀態能夠作對比
show slave status中SQL_Delay值代表了設置的延遲時長

延遲複製 slave1 mysql> stop slave; Query OK, 0 rows affected (0.09 sec) mysql> CHANGE MASTER TO MASTER_DELAY = 60; Query OK, 0 rows affected (0.07 sec) mysql> start slave; Query OK, 0 rows affected (0.10 sec) mysql> show slave status \G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.138.131 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: binlog.000009 Read_Master_Log_Pos: 1366 Relay_Log_File: mysql-slave2-relay-bin.000002 Relay_Log_Pos: 319 Relay_Master_Log_File: binlog.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: 1366 Relay_Log_Space: 534 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: 1 Master_UUID: 849ecaa5-3386-11e9-9db6-000c29c16499 Master_Info_File: mysql.slave_master_info SQL_Delay: 60 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: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: Master_public_key_path: Get_master_public_key: 0
1 row in set (0.00 sec) ERROR: No query specified master mysql> show master status; +---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000009 |     1366 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) mysql> insert into teacher_backup values(4,'abc',5); Query OK, 1 row affected (0.00 sec) mysql> show master status; +---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000009 |     1667 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec) 從庫 mysql> select * from teacher_backup; +----+------+---------+
| id | name | dept_id |
+----+------+---------+
|  1 | abc  |       1 |
|  2 | abc  |       4 |
|  3 | abc  |       4 |
+----+------+---------+
3 rows in set (0.01 sec) 60s後 mysql> select * from teacher_backup; +----+------+---------+
| id | name | dept_id |
+----+------+---------+
|  1 | abc  |       1 |
|  2 | abc  |       4 |
|  3 | abc  |       4 |
|  4 | abc  |       5 |
+----+------+---------+
4 rows in set (0.00 sec)

未完待續

相關文章
相關標籤/搜索