MySQL复制中seconds_behind_master的计算 1.seconds_behind_master定义为从库相对于主库的延迟时间。 具体的计算公式为: clock_of_slave - last_timestamp_executed_by_SQL_thread-clock_diff_with_master. 备注:位于rpl_mi.h中定义了clock_diff_with_master,该值为从库相对于主库的时间差。 /* The difference in seconds between the clock of the master and the clock of the slave (second - first). It must be signed as it may be <0 or >0. clock_diff_with_master is computed when the I/O thread starts; for this the I/O thread does a SELECT UNIX_TIMESTAMP() on the master. "how late the slave is compared to the master" is computed like this: clock_of_slave - last_timestamp_executed_by_SQL_thread - clock_diff_with_master */ long clock_diff_with_master; // clock_diff_with_master // mi->clock_diff_with_master= (long) (time((time_t*) 0) - strtoul(master_row[0], 0, 10)); master_row[0]就是在主库上执行SELECT UNIX_TIMESTAMP()的操作。 从如下到(rpl_slave.cc)可以看出: start_slave_thread-> //启动start slave handle_slave_io-> //启动start io thread get_master_version_and_clock //获取当前slave和主机之间的时间差(clock_diff_with_master) /* Used to defer stopping the SQL thread to give it a chance to finish up the current group of events. The timestamp is set and reset in @c sql_slave_killed(). */ time_t last_event_start_time; //slave端最后执行的reply log的最后一个事件的时间