您的位置:首页 > 运维架构

主从或者主主手工维护同步数据

2012-12-31 16:52 423 查看
1.使用一个函数master_pos_wait;

master_pos_wait(file, pos[, timeout]).

这里的file和pos对应主库show master status得到的值,代表执行位置。 函数逻辑是等待当前从库达到这个位置后返回, 返回期间执行的事务个数。

参数timeout可选,若缺省则无限等待,timeout<=0时与缺省的逻辑相同。若为正数,则等待这么多秒,超时函数返回-1.

其他返回值:若当前slave为启动或在等待期间被终止,返回NULL; 若指定的值已经在之前达到,返回0

2.在从服务器上进行执行以下语句

select MASTER_POS_WAIT('binlog.000022','592429')\G

注意:手动维护前,需要注意主服务器上几点;

flush tables with read lock; 用读锁锁住所有的表阻止对它的更新(主)

unlock tables; (主)

阿里的分享记录

MASTER_POS_WAIT(log_name,log_pos[,timeout])

This function is useful for control of master/slave synchronization. It blocks until the slave has read and applied all updates up to the specified position in the master log. The return value is the number of log events the slave had to wait for to advance
to the specified position. The function returns NULL if the slave SQL thread is not started, the slave’s master information is not initialized, the arguments are incorrect, or an error occurs. It returns -1 if the timeout
has been exceeded. If the slave SQL thread stops while MASTER_POS_WAIT() is
waiting, the function returns NULL. If the slave is past the specified position, the function returns immediately.

If a timeout value is specified, MASTER_POS_WAIT() stops
waiting when timeout seconds have elapsed. timeoutmust be greater than 0; a zero or negative timeout means no timeout.

This function is unsafe for statement-based replication. Beginning with MySQL 5.5.1, a warning is logged if you use this function when binlog_format is
set to STATEMENT. (Bug #47995)

【分享】

后来同事分享其实start slave直接可以指定停止的pos,汗

START SLAVE [SQL_THREAD] UNTIL MASTER_LOG_FILE = ‘log_name‘, MASTER_LOG_POS = log_pos

An UNTIL clause may be added to specify that the slave should start and run until the SQL thread reaches a given point in the master binary log or in the slave relay log. When the SQL thread reaches that point, it stops. If the SQL_THREAD option is specified
in the statement, it starts only the SQL thread. Otherwise, it starts both slave threads. If the SQL thread is running, the UNTIL clause is ignored and a warning is issued.

For an UNTIL clause, you must specify both a log file name and position. Do not mix master and relay log options.

Any UNTIL condition is reset by a subsequent STOP SLAVE statement, a START SLAVE statement that includes noUNTIL clause, or a server restart.

The UNTIL clause can be useful for debugging replication, or to cause replication to proceed until just before the point where you want to avoid having the slave replicate an event. For example, if an unwise DROP TABLE statement was executed on the master,
you can use UNTIL to tell the slave to execute up to that point but no farther. To find what the event is, use mysqlbinlog with the master binary log or slave relay log, or by using a SHOW BINLOG EVENTS statement.

If you are using UNTIL to have the slave process replicated queries in sections, it is recommended that you start the slave with the –skip-slave-start option to prevent the SQL thread from running when the slave server starts. It is probably best to use this
option in an option file rather than on the command line, so that an unexpected server restart does not cause it to be forgotten.

【最终正向恢复方式】

选择数据出问题时间之前最近的备份集恢复在一个新实例(恢复工具支持,操作方便)

start slave until master_log_file=’mysql-bin.000396′,master_log_pos=67698920;

show slave status\G; (保存复制关系)

change master to master_host=’ ‘, master_user=’slave’,master_password=’slave’; (取消复制关系,防止误操作开启复制)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: