摘要:
在主从复制的架构中,正常情况下主上操作的记录也会在从上进行操作,虽说是异步复制,但操作会“实时”的同步到从。比如在主上不小心误操作了,还没等反应过来从上也会马上执行误操作,后期只有通过二进制或则备份恢复数据了,费时,又费力,没有任何回旋的余地,而且也会影响到网站的功能。而pt-slave-delay故意让主上的操作延迟制定的时间写入到从,这样就可以快速的处理上面说的问题了。下面介绍下使用方法,其实挺简单的。
前提:
下载地址:wget www.percona.com/downloads/percona-toolkit/2.2.2/percona-toolkit-2.2.2.tar.gz
安装方法:perl Makefile.PL;make;make install
使用方法:
pt-slave-delay [OPTIONS] SLAVE_DSN [MASTER_DSN]
执行该命令链接数据库的账号需要有 PROCESS、REPLICATION CLIENT、and SUPER权限。他是通过Slave的relay log(中继日志)的position(偏移量),不断启动,关闭 replication SQL thread 来保持主从一直延时固定的时间来实现的,所以没有必要链接主服务器。如果想在运行中停止的话,按CTRL+C中断就可以了。具体执行的命令:
pt-slave-delay --delay=1m --interval=15s --run-time=10m u=root,p=123456,h=192.168.200.25,P=3307
#--delay :从库延迟主库的时间,上面为1分钟。
#--interval :检查的间隔时间,上面为15s检查一次。(可选),不选则1分钟检查一次。
#--run-time :该命令运行时间,上面为该命令运行10分钟关闭。(可选),不选则永远运行。
#--ask-pass :隐性输入密码。
其他的为链接数据库的账号信息,账号要有PROCESS, REPLICATION CLIENT, and SUPER权限
注意:延迟的时间实际为 delay+interval,即该命令的让从延迟主75s。
还有一些具体的参数请见:http://www.percona.com/doc/percona-toolkit/2.2/pt-slave-delay.html
背景:
主:
root@localhost : aaa 02:37:21>select * from test;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
+----+------+
3 rows in set (0.00 sec)
从:
root@192.168.200.25 : aaa 03:11:44>select * from test;
+----+------+
| id | name |
+----+------+
| 1 | a |
| 2 | b |
| 3 | c |
+----+------+
3 rows in set (0.00 sec)
root@192.168.200.25 : aaa 03:12:05>show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: 192.168.200.25
Master_User: rep
Master_Port: 3306
Connect_Retry: 60
Master_Log_File: mysql-bin.000003
Read_Master_Log_Pos: 1919
Relay_Log_File: zhoujy-relay-bin.000131
Relay_Log_Pos: 1636
Relay_Master_Log_File: mysql-bin.000003
Slave_IO_Running: Yes
Slave_SQL_Running: Yes
Replicate_Do_DB: aaa
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: 1919
Relay_Log_Space: 1951
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:
Master_Info_File: /opt/mysql/mysql5.6/master.info
SQL_Delay: 0
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