Friday, November 5, 2010

Troubleshooting MySQL Replication


Here are the tools required to diagnose replication problems along with a few suggestions about how and when to use each:

SHOW MASTER STATUS and SHOW SLAVE STATUS
These SQL commands are your primary tool for diagnosing replication problems. Along with the SHOW PROCESSLIST command, you should execute these commands on the master and then on the slave, then examine the output. The slave command has an extended set of parameters that are invaluable in diagnosing replication problems.

SHOW GRANTS FOR <replication user>
Whenever you encounter slave user access problems, you should first examine the grants for the slave user to ensure they have not changed.

CHANGE MASTER
Sometimes the configuration files have been changed either knowingly or accidentally. Use this SQL command to override the last known connection parameters and to diagnose slave connection problems.

STOP/START SLAVE
Use these SQL commands to start and stop replication. It is sometimes a good idea to stop a slave if it is in an error state.

Examine the configuration files
Sometimes the problem occurs as a result of an unsanctioned or forgotten configuration change. Check your configuration files routinely when diagnosing connection problems.

Examine the server logs
You should make this a habit whenever diagnosing problems. Checking the server logs can sometimes reveal errors that are not visible elsewhere. As cryptic as they can sometimes be, the error and warning messages can be helpful.

SHOW SLAVE HOSTS
Use this command to identify the connected slaves on the master if they use the report-host option.

SHOW PROCESSLIST
When encountering problems, it is always a good idea to see what else is running. This command will tell you the current state of each of the threads involved in replication. Check here first when examining the problem.

SHOW BINLOG EVENTS
This SQL command displays the events in the binary log. If you use statement-based replication, this command will display the changes using SQL statements.

mysqlbinlog
This utility allows you to read events in the binary or relay logs, often indicating when there are corrupt events. Don’t hesitate to use this tool frequently when diagnosing problems related to events and the binary log.

PURGE BINARY LOGS
This SQL command allows you to remove certain events from the binary log, such as those that occur after a specific time or after a given event ID. Your routine maintenance plan should include the use of this command for purging older binary logs that are no longer needed.