How SHOW SLAVE STATUS relates to CHANGE MASTER TO
As you probably know MySQL Replication (statement based) works by fetching statements from MASTERs binary log and executing them on the SLAVE. Since MySQL 4.0 this process is a bit more involved having events passing via relay logs on the Slave which also means there are two replication threads "IO Thread" and "SQL Thread" used in the process but idea remains the same.
For replication to work properly you need to have proper "snapshot" - consistent state of the database as it was on master and corresponding position in masters binary logs. There are multiple ways to get the "snapshot" - shutting down MySQL Server and copying data, using LVM, Using Innodb Hot Backup Tool, Using another Slave, using backup image etc but in all the cases you have to be sure the snapshot you're dealing with corresponds to correct binary log position.
There are two sources of information for binary log position SHOW MASTER STATUS - this shows position as master writes its own binary and is helpful when you for example take backup on the Master to bring up first slave. This command output is pretty simple and leaves no room for mistakes:
-
mysql> SHOW master STATUS \G
-
*************************** 1. row ***************************
-
File: db04-bin.000068
-
Position: 98
-
Binlog_Do_DB:
-
Binlog_Ignore_DB:
-
1 row IN SET (0.00 sec)
If you're backing up from the Slave or you're cloning the Slave to make another one you need to use SHOW SLAVE STATUS which provides much more information.
-
mysql> SHOW slave STATUS\G
-
*************************** 1. row ***************************
-
Slave_IO_State: Waiting FOR master TO send event
-
Master_Host: 192.168.10.171
-
Master_User: replicant
-
Master_Port: 3306
-
Connect_Retry: 60
-
Master_Log_File: db01-bin.003814
-
Read_Master_Log_Pos: 744615652
-
Relay_Log_File: mysqld-relay-bin.000953
-
Relay_Log_Pos: 744615788
-
Relay_Master_Log_File: db01-bin.003814
-
Slave_IO_Running: Yes
-
Slave_SQL_Running: Yes
-
Replicate_Do_DB:
-
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: 744615652
-
Relay_Log_Space: 744615788
-
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
-
1 row IN SET (0.00 sec)
As you can see in this output there are 3 log file names and log file positions which can get confusing, and indeed I often see people using wrong values to restore the replication using CHANGE MASTER TO statement.
It is very important to understand the difference between IO Thread position in Master logs (Master_Log_File:Read_Master_Log_Pos) and SQL Thread position in Master logs (Relay_Master_Log_File:Exec_Master_Log_Pos).
When you're using CHANGE MASTER TO to set start position for the slave you're specifying position for SQL thread and so you should use Relay_Master_Log_File:Exec_Master_Log_Pos. Otherwise you're going to ruin your replication.
It does not help SHOW MASTER STATUS has Master_Log_File value and CHANGE MASTER TO accepts Master_Log_File parameter, while you actually want to use Relay_Master_Log_File instead. This naming was left from pre MySQL 4.0 era and it still causes confusion so many years later.
The mistake of using Master_Log_File is in fact VERY typical. I've seen multiple "internal instructions" for cloning the slave or slave recovery from backup using this value.
Infact Master_Log_File will work in most cases as well - you can note in the output about Master_Log_File and Relay_Master_Log_File are the same, and it will be the case in vast majority of cases.
If your replication does not fall behind the SQL Thread and IO thread will be running close, quite often being at same master position at all. Even if replication gets a bit behind in most cases you would still see SQL thread and IO threads close enough to share same master log file. Only in rare cases of switching master binary log file or replication being delayed very significantly you will see different log files for these variables which makes it very easy to make mistake and very hard to catch.
6 Comments











del.icio.us
digg
Thanks for clarifying that - it is easy to be misled by the seemingly-matching terminology.
Do you mean “It does not help SHOW SLAVE STATUS has …” rather than “It does not help SHOW MASTER STATUS has …” in the fourth-to-last paragraph?
Comment :: July 7, 2008 @ 8:55 pm
Paul,
The point is Master_Log_File exists both in CHANGE MASTER TO as parameter and SHOW MASTER STATUS as a column while they mean different thing (you should not take one from SHOW MASTER STATUS and pass it to CHANGE MASTER TO to restart replication)
Comment :: July 7, 2008 @ 9:18 pm
It’s one of those niggley things which - as you say - is so very easy to do, but you feel ever so silly once you realise because it’s really so obvious! A true “I need to wear shades and a baseball cap so no one can see it’s me for the next six months”-moment.
Comment :: July 8, 2008 @ 12:40 am
Peter, I think you’re missing Paul’s point: not only SHOW MASTER STATUS has a Master_Log_File column. SHOW SLAVE STATUS does too. And the value in SHOW SLAVE STATUS is the one that people wrongly use.
Comment :: July 8, 2008 @ 4:57 am
Baron,
Actually SHOW MASTER STATUS has column named “File” and SHOW SLAVE STATUS has Master_Log_File - check the example printouts above:
Master_Log_File: db01-bin.003814
Comment :: July 8, 2008 @ 10:29 am
So the bottom line is
1) When you are using the master as a consistent snapshot, use SHOW MASTER STATUS to determine the position.
2) When you are using a slave as a consistent snapshot, use SHOW SLAVE STATUS and Exec_Master_Log_Pos.
Comment :: July 9, 2008 @ 5:11 am