MySQL 5.6 已经支持延迟的流复制, 可设置备节点的延迟时间, 延迟复制是有意义的,例如防止主节点数据误删,查看数据库历史状态等, PostgreSQL 在即将发行的 9.4 版本也支持延迟复制,有兴趣的朋友可以查看之前写的 blog: PostgreSQL9.4 : 支持备库延迟复制(delayed standbys) , 这里简单介绍 MySQL 延迟复制的配置。
MySQL 的主从搭建本文略。 CHANGE MASTER TO 命令指定 MASTER_DELAY 选项即可设置,如下:
一 语法
1
|
CHANGE MASTER TO MASTER_DELAY = 30;
|
备注:设置备节点延迟的时间,单位秒。
二 延迟复制配置
从节点设置
1 2 3 4 5 6 7 8
|
root@localhost:mysql>stop slave; Query OK, 0 rows affected (0.21 sec) root@localhost:mysql>CHANGE MASTER TO MASTER_DELAY = 30; Query OK, 0 rows affected (0.17 sec) root@localhost:mysql>start slave; Query OK, 0 rows affected (0.27 sec)
|
查看从节点状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
root@localhost:mysql>select * from slave_relay_log_infoG * 1. row * Number_of_lines: 7 Relay_log_name: ./db2-relay-bin.000007 Relay_log_pos: 281 Master_log_name: bin-log.000041 Master_log_pos: 361 Sql_delay: 30 Number_of_workers: 0 Id: 1 1 row in set (0.00 sec) root@localhost:mysql>show slave statusG * 1. row * Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.37 Master_User: rep1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin-log.000040 Read_Master_Log_Pos: 604 Relay_Log_File: db2-relay-bin.000002 Relay_Log_Pos: 281 Relay_Master_Log_File: bin-log.000040 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: 604 Relay_Log_Space: 452 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: 0c130d47-22bb-11e4-aaaa-000c2986ac80 Master_Info_File: mysql.slave_master_info SQL_Delay: 30 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for the slave I/O thread to update it 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 1 row in set (0.00 sec)
|
主节点创建表测试
1 2 3 4 5
|
root@localhost:mysql>create table test_delay(id int4 primary key, create_time datetime default current_timestamp); Query OK, 0 rows affected (0.59 sec) root@localhost:mysql>insert into test_delay (id) values(1); Query OK, 1 row affected (0.07 sec)
|
备注:在主节点上创建一张测试表并插入一条测试数据。
备节点查询
1 2 3 4 5
|
root@localhost:mysql>select * from test_delay; Empty set (0.00 sec) root@localhost:mysql>select * from test_delay; Empty set (0.00 sec)
|
这时在备节点上还查不到表。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
root@localhost:mysql>show slave statusG * 1. row * Slave_IO_State: Waiting for master to send event Master_Host: 192.168.2.37 Master_User: rep1 Master_Port: 3306 Connect_Retry: 60 Master_Log_File: bin-log.000040 Read_Master_Log_Pos: 1013 Relay_Log_File: db2-relay-bin.000002 Relay_Log_Pos: 281 Relay_Master_Log_File: bin-log.000040 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: 604 Relay_Log_Space: 861 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: 15 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: 0c130d47-22bb-11e4-aaaa-000c2986ac80 Master_Info_File: mysql.slave_master_info SQL_Delay: 30 SQL_Remaining_Delay: 14 Slave_SQL_Running_State: Waiting until MASTER_DELAY seconds after master executed event 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 1 row in set (0.00 sec) root@localhost:mysql>select * from test_delay; +----+---------------------+ | id | create_time | +----+---------------------+ | 1 | 2014-12-12 14:38:47 | +----+---------------------+ 1 row in set (0.00 sec)
|
备注: 发现备节点在 30 秒后才能查到新建的表,这里有三个字段显示延迟的信息:
SQL_Delay: 显示已设置的主备延迟的时间,单位秒;
SQL_Remaining_Delay: 显示剩余的主备延迟时间,单位秒;
Slave_SQL_Running_State: 表示 SQL thread 状态;
三 参考
原创文章,作者:Maggie-Hunter,如若转载,请注明出处:https://blog.ytso.com/239612.html