How to Replication Works in MySQL

How to – Replication Works in MySQL

MySQL Replication Events:
Statement based – in which case these are write queries
Row based – in this case these are changes to records, sort of row diffs if you will
Master :
Master needs to be writing replication events to a special log called binary log. This is usually very lightweight activity (assuming events are not synchronized to disk), because writes are buffered and because they are sequential. The binary log file stores data that replication slave will be reading later.
Whenever a replication slave connects to a master, master creates a new thread for the connection (similar to one that’s used for just about any other server client).
This connection will do the following
(a) feeding replication slave with events from the binary log.
(b) notifying slave about newly written events to its binary log.
Slaves that are up to date will mostly be reading events that are still cached in OS cache on the master.
However, when you connect a replication slave that is few hours or even days behind, it will initially start reading binary logs that were written hours or days ago – master may no longer have these cached, so disk reads will occur.
Slave : 
When you start replication, two threads are started on the slave
1. IO thread
This process called IO thread connects to a master, reads binary log events from the master as they come in and just copies them over to a local log file called relay log.
Master_Log_File – last file copied from the master (most of the time it would be the same as last binary log written by a master)
Read_Master_Log_Pos – binary log from master is copied over to the relay log on the slave up until this position.
you can compare it to the output of “show master status” from the master.
2. SQL thread
The second process – SQL thread – reads events from a relay log stored locally on the replication slave (the file that was written by IO thread) and then applies them as fast as possible.
Relay_Master_Log_File – binary log from master, that SQL thread is “working on”
Exec_Master_Log_Pos – which position from master binary log is being executed by SQL thread.
Key word to remember is Bin Log and Relay Log

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.