When replication runs out of sync first question you often ask is if someone could be writing to the slave. Of course there is read_only setting which is good to set in the slave but it is not set always and also users with SUPER privilege bypass it.
Looking into binary log is obvious choice – this is a good reason to have binary log on the slave if you do not need it for anything else. By default MySQL will only write statements which come to the server directly (not via replication thread) so you will know offender at once.
In many cases however log_slave_updates is enabled which makes slave to write all updates to binary logs – the ones executed directly on the host as well as coming through replication thread. There is however a way to know which is which – based on server_id.
Here is snippet from Slave binary log which has updates one on Master and another on Slave directly:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # at 666 #100411 19:24:01 server id 1 end_log_pos 725 Query thread_id=68 exec_time=1 error_code=0 SET TIMESTAMP=1271039041/*!*/; BEGIN /*!*/; # at 725 #100411 19:24:01 server id 1 end_log_pos 819 Query thread_id=68 exec_time=0 error_code=0 SET TIMESTAMP=1271039041/*!*/; update tmp set c1=5 where id=10 /*!*/; # at 819 #100411 19:24:01 server id 1 end_log_pos 846 Xid = 27 COMMIT/*!*/; # at 846 #100411 19:24:45 server id 101 end_log_pos 914 Query thread_id=5 exec_time=0 error_code=0 SET TIMESTAMP=1271039085/*!*/; BEGIN /*!*/; # at 914 #100411 19:24:45 server id 101 end_log_pos 1007 Query thread_id=5 exec_time=0 error_code=0 SET TIMESTAMP=1271039085/*!*/; update tmp set c1=6 where id=8 /*!*/; # at 1007 #100411 19:24:45 server id 101 end_log_pos 1034 Xid = 37 |
First query has server_id=1 and thus same through the master, another has server_id=101 which is slave id.
So to see if we have any queries ran on the slave directly we can do:
1 2 3 4 5 | [root@centos data]# mysqlbinlog mysql-bin.000002 | grep "server id 101" | grep Query #100411 19:11:22 server id 101 end_log_pos 360 Query thread_id=4 exec_time=0 error_code=0 #100411 19:11:22 server id 101 end_log_pos 453 Query thread_id=4 exec_time=0 error_code=0 #100411 19:24:45 server id 101 end_log_pos 914 Query thread_id=5 exec_time=0 error_code=0 #100411 19:24:45 server id 101 end_log_pos 1007 Query thread_id=5 exec_time=0 error_code=0 |
Any query events with local server_id means it is being written directly.
Also, the read-only setting can only be set at server start. This means that any high-availability scheme will need to edit the my.cnf and restart the slave before switching operations to it, while without the read-only “stop slave; reset master;” is enough.
@Dmitriy: read_only is a dynamic variable and can be changed at runtime, a restart is not required.
Off-Topic:
Is it possible to grep the queries for a specific server?
i.e. all queries done on server id 101?
Dmitriy: You can use SET GLOBAL read_only=1/0
I don’t think you can do that in MySQL 5.0.x.
Dmitriy: with MMM you must start your servers with read_only and MMM will disable/enable this flag for you.
Dmitry, yes, you can in MySQL 5.0. Why don’t you look up the docs, or test it, instead of just posting here without even checking first?
Bon Scott,
Yes as you have server_id in the binary logs you can look for queries done on any server.