Fix increasing Seconds_Behind_Master issue
February 25, 2011 6 Comments
The problem
Sometimes on innodb replication the slave is being late behind the master. This can be observed by the Seconds_Behind_Master value always increasing when the master is inserting/updating rows.
Exemple :
mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: masterdb.somewhere.com Master_User: replication Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.007781 Read_Master_Log_Pos: 365235732 Relay_Log_File: mysqld-3306-relay-bin.004386 Relay_Log_Pos: 259967669 Relay_Master_Log_File: mysql-bin.007781 Slave_IO_Running: Yes Slave_SQL_Running: Yes (...) Seconds_Behind_Master: 33 1 row in set (0.00 sec)
In this example we can see that the slave is 33s behind the master while both CPU and IO are not overloaded.
Why ?
Because the log buffer is written out to the log file at each transaction commit and the flush to disk operation is performed on the log file. Writing and flushing operations are time expensive for only one transaction. It will be better to perform these operations for several transactions instead of only one.
How to do it ?
Just change the value of innodb_flush_log_at_trx_commit from 1 (default) to 0 :
mysql> SET GLOBAL innodb_flush_log_at_trx_commit = 0 ;
And add it in the my.cnf in order to take this setting in account at the next restart of mysql engine.
More information : http://dev.mysql.com/doc/refman/5.0/en/innodb-parameters.html#sysvar_innodb_flush_log_at_trx_commit