让MySQL拓扑中的从节点延迟适当的时间,可以帮助避免在主节点上发生的灾难性的错误。
MASTER_DELAY这个属性指定SQL_THREAD会在从节点上暂定多长时间,时间以秒为单位。默认值为0,上限为68年。
mysql> stop slave;
Query OK, 0 rows affected (0.06 sec)
mysql> change master to master_delay=20;
Query OK, 0 rows affected (0.01 sec)
mysql> start slave;
点击(此处)折叠或打开
mysql> stop slave;
Query OK, 0 rows affected (0.01 sec)
mysql> change master to master_delay=20 ;
Query OK, 0 rows affected (0.02 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.8.57
Master_User: repl
Master_Port: 3306
Connect_Retry: 10
Master_Log_File: mysql-bin.000021
Read_Master_Log_Pos: 154
Relay_Log_File: relay-log.000002
Relay_Log_Pos: 320
Relay_Master_Log_File: mysql-bin.000021
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: 154
Relay_Log_Space: 521
Until_Condition: None
Until_Log_File:
Until_Log_Pos: 0
Master_SSL_Allowed: No
Master_SSL_CA_File: /usr/local/mysql/certs/ca-cert.pem
Master_SSL_CA_Path:
Master_SSL_Cert: /usr/local/mysql/certs/client-cert.pem
Master_SSL_Cipher:
Master_SSL_Key: /usr/local/mysql/certs/client-key.pem
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: 9ad24233-aeef-11e7-aa1b-080027768e58
Master_Info_File: mysql.slave_master_info
SQL_Delay: 20
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:
1 row in set (0.00 sec)
可以看到SQL_Delay: 20
下边做一个测试:
在主节点对测试表做truncate
点击(此处)折叠或打开
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
| tb_admin_bak |
+----------------+
2 rows in set (0.00 sec)
mysql> truncate table tb_admin_bak;
Query OK, 0 rows affected (0.02 sec)
在从节点查看表信息
点击(此处)折叠或打开
mysql> select count(*) from tb_admin_bak;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.02 sec)
mysql> select count(*) from tb_admin_bak;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)
mysql> show full processlist \G
*************************** 1. row ***************************
Id: 3
User: root
Host: localhost
db: test
Command: Query
Time: 0
State: starting
Info: show full processlist
*************************** 2. row ***************************
Id: 8
User: system user
Host:
db: NULL
Command: Connect
Time: 334
State: Waiting for master to send event
Info: NULL
*************************** 3. row ***************************
Id: 9
User: system user
Host:
db: NULL
Command: Connect
Time: 100
State: Waiting until MASTER_DELAY seconds after master executed event
Info: NULL
3 rows in set (0.00 sec)
mysql> select count(*) from tb_admin_bak;
+----------+
| count(*) |
+----------+
| 0 |
+----------+
1 row in set (0.00 sec)
发现从节点此表数据刚开始并没有被清理,20S之后数据才被清理掉。
在主库删除测试表
点击(此处)折叠或打开
mysql> drop table tb_admin_bak;
Query OK, 0 rows affected (0.06 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
+----------------+
1 row in set (0.00 sec)
在从库查看表是否存在
点击(此处)折叠或打开
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
| tb_admin_bak |
+----------------+
2 rows in set (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
| tb_admin_bak |
+----------------+
2 rows in set (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
| tb_admin_bak |
+----------------+
2 rows in set (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
| tb_admin_bak |
+----------------+
2 rows in set (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
| tb_admin_bak |
+----------------+
2 rows in set (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
| tb_admin_bak |
+----------------+
2 rows in set (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
| tb_admin_bak |
+----------------+
2 rows in set (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
+----------------+
1 row in set (0.00 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| tb_admin |
+----------------+
1 row in set (0.00 sec)
一段时间之后,测试表才被删除。