MySQL系列詳解九:MySQL級聯複製演示-技術流ken
阿新 • • 發佈:2018-12-17
前言
級聯複製就是master伺服器,只給後端一臺slave伺服器同步資料,然後這個slave伺服器在向後端的所有slave伺服器同步資料,這樣就可以降低master伺服器的寫壓力,和複製資料的網路IO。
實現級聯複製演示
準備
主伺服器IP:10.220.5.137
從1伺服器IP:10.220.5.138
從2伺服器IP:10.220.5.139
配置主伺服器
開啟二進位制日誌
mysql> show global variables like '%log_bin%'; +---------------------------------+--------------------------------------------+ | Variable_name | Value | +---------------------------------+--------------------------------------------+ | log_bin | ON | | log_bin_basename | /data/mysql/mysql3306/logs/mysql-bin | | log_bin_index | /data/mysql/mysql3306/logs/mysql-bin.index | | log_bin_trust_function_creators | ON | | log_bin_use_v1_row_events | OFF | +---------------------------------+--------------------------------------------+ 5rows in set (0.01 sec)
建立用於複製的使用者
mysql> grant replication slave on *.* to 'ken'@'%' identified by 'xx';
檢視主伺服器端狀態
需要檢視現在主伺服器端在哪個日誌檔案寫入日誌以及現在的位置
mysql> show master status\G *************************** 1. row *************************** File: mysql-bin.000012 Position: 2057Binlog_Do_DB: Binlog_Ignore_DB: Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-13, c01b1811-d7b3-11e8-8698-000c29492f7b:3-7 1 row in set (0.00 sec)
配置從1伺服器
與主伺服器端建立連線
MySQL [(none)]> change master to master_host='10.220.5.137',master_user='ken',master_passowrd='xx',master_log_file='mysql-bin.000012',master_log_file=2057;
啟動slave
MySQL [(none)]> start slave;
檢視連線狀態
要確認IO以及SQL執行緒成功開啟
ySQL [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.220.5.137 Master_User: ken Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000012 Read_Master_Log_Pos: 2057 Relay_Log_File: relay-bin.000012 Relay_Log_Pos: 1586 Relay_Master_Log_File: mysql-bin.000012 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: 2057 Relay_Log_Space: 1834 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: /data/mysql/mysql3306/data/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: 987ac782-d7b8-11e8-a462-000c292218ec:8-13 Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-13, c01b1811-d7b3-11e8-8698-000c29492f7b:1-7 Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
檢視從1伺服器端狀態
需要檢視現在住伺服器端在哪個日誌檔案寫入日誌以及現在的位置
mysql> show master status\G
*************************** 1. row ***************************
File: mysql-bin.000007
Position: 2057
Binlog_Do_DB:
Binlog_Ignore_DB:
Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-13,
c01b1811-d7b3-11e8-8698-000c29492f7b:3-7
1 row in set (0.00 sec)
配置從2伺服器端
與從1伺服器端建立連線
MySQL [(none)]> change master to master_host='10.220.5.138',master_user='ken',master_passowrd='xx',master_log_file='mysql-bin.000007',master_log_file=2057;
啟動slave
MySQL [(none)]> start slave;
檢視連線狀態
要確認IO以及SQL執行緒成功開啟
ySQL [(none)]> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.220.5.138 Master_User: ken Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000007 Read_Master_Log_Pos: 2057 Relay_Log_File: relay-bin.000007 Relay_Log_Pos: 1586 Relay_Master_Log_File: mysql-bin.000007 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: 2057 Relay_Log_Space: 1834 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: /data/mysql/mysql3306/data/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: 987ac782-d7b8-11e8-a462-000c292218ec:8-13 Executed_Gtid_Set: 987ac782-d7b8-11e8-a462-000c292218ec:1-13, c01b1811-d7b3-11e8-8698-000c29492f7b:1-7 Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec)
測試
現在從2伺服器是從從1伺服器端同步的資料,沒有直接和主伺服器端建立連線,而是通過從1間接的進行了連線。現在在主伺服器端建庫建表,檢視從2伺服器是否有主伺服器端的庫和表。
主伺服器建庫建表
mysql> create database ken6; <<建立資料庫ken6 mysql> use ken6; <<切換至ken6 Database changed mysql> create table ken(id int); <<建立表ken Query OK, 0 rows affected (0.02 sec) mysql> insert into ken values (1),(2); <<插入資料 Query OK, 2 rows affected (0.01 sec) Records: 2 Duplicates: 0 Warnings: 0 mysql> select * from ken; <<檢視剛才插入的資料 +------+ | id | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec)
在從1伺服器端檢視已經同步過來了主伺服器端新建的庫和表
MySQL [(none)]> select * from ken6.ken; +------+ | id | +------+ | 1 | | 2 | +------+
在從2檢查發現也已經同步過來了主伺服器端的庫和表,這樣就完後了級聯複製的操作
MySQL [(none)]> select * from ken6.ken; +------+ | id | +------+ | 1 | | 2 | +------+ 2 rows in set (0.00 sec)