How Replication transfer events from Master to Slave in MySQL

In MySQL the events (write operations) are transferred from master to slave with the help of threads.

Thread Name Runs On
Binlog Dump Master
IO Slave
SQL Slave

and in the following chronological order.
  • The slave connects to the master (as a client) using the credentials that were given during setup. This credentials are set by executing CHANGE MASTER TO at slave side. It might be possible that MySQL is storing the credentials in a plain form. Hence it is advisable to create a separate user in Master with privileges that grant it the power of serving the purpose of replication only.
  • IO thread from the slave asks for new events in the master if any.
  • Binlog Dump thread from master checks this and send contents to IO thread if new events are do present.
  • IO thread then writes the new events to the relay logs on slave.
  • SQL thread on the slave then reads these events from relay logs and apply them to the database.

MySQL Master Slave Replication Diagram

This is how slave keeps up with master and maintain a copy of the master database. It is even recommended to set read_only server variable to TRUE on slave to prevent writes outside from replication.

Since the replication process is asynchronous, hence we have the flexibility to stop and start the IO and SQL thread independently in situations required.

Also it is possible for slaves to lag behind master within few events up to a bigger extent.

Please note that it is possible to setup MySQL master slave replication by two methods:
  • Binary Log Coordinates → Each replica keeps a record of the binary log coordinates: the binary log file name and position within the file that it has read and processed from the source.
  • GTID → A GTID (Global Transaction IDentifier) is represented as a pair of coordinates (GTID = source_id:transaction_id). The source_id identifies the originating server. Normally, the server's server_uuid is used for this purpose. The transaction_id is a sequence number determined by the order in which the transaction was committed on this server; for example, the first transaction to be committed has 1 as its transaction_id, and the tenth transaction to be committed on the same originating server is assigned a transaction_id of 10. It is not possible for a transaction to have 0 as a sequence number in a GTID.

Comments

Back To Top

Popular posts from this blog

How to save video from Internet Explorer

error 18 at 0 depth lookup: self signed certificate

How to check fragmentation in MySQL tables