Master-Slave Replication

Master-Slave Replication

Introduction

This blog covers the basics of how replication really works on the high level, and the configuration of Master-Slave replication. With this replication we can share load between Master and Slave (only read operations), take backups from Slave server without effecting the Master server.

Use Case

This use case describes replication and configuration of a Master-Slave replication.

What we need to do:

  • Theoretical explanation of how replication works.
  • Configuration of Master Server.
  • Configuration of Slave Server.

Solution

Before solving our use case, let’s get some pre-requisites satisfied.

Pre-requisites:

Minimum two Linux servers along with MySQL software should be installed.

  • Master  ip: 192.168.0.1
  • Slave  ip: 192.168.0.2

Theoretical explanation of how replication works:

  • Types of mysql replication:

Replication is based on events written to the binary log, which are read from master and then processed on the slave.

  • Statement Based Replication:

Replication work is based on propagation of SQL statements from master to slave. This is called statement-based replication. Often it called SBR, Which corresponds to the standard statement-based binary logging format.

  • Row Based Replication:

Replication based on row-based logging which changes binary logging logs in individual table row. This is known as row-based replication. It is also called as RBR. In row-based replication, the master writes events to the binary log that indicates how individual table rows are changed.

  • Mixed Based Replication:

The server can change the binary logging format in real time according to the type of event using mixed-format logging. When the mixed format is in effect, statement-based logging is used by default, but automatically switches to row-based logging in particular cases. Replication using the mixed format is often referred to as mixed-based replication or mixed-format replication.

So now let’s start with what is happening on the master. For replication to work, first and foremost, master needs to write replication events to a special log called binary log. 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.

Slaves that are up to date will mostly be reading events that are still cached in OS cache on the master, so there will not be any physical disk reads on the master in order to feed binary log events to slave(s). 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. If master does not have free IO resources, you may feel a bump at that point.

Now let’s see what is happening on the slave. When you start replication, two threads are started on the slave:

  • 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. That’s all.

Even though there are only one thread reading binary log from the master and one writing relay log on the slave, very rarely copying of replication events is a slower element of the replication. There could be a network delay, causing a steady delay of few hundred milliseconds, but that’s about it.

To see IO thread status, just type “show slave status\G” on slave.

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 – This shows the position where binary log copied over the relay log on the slave.

  • SQL thread:

This process reads the events from a relay log stored locally on the replication slave and then applies them as fast as possible and it is a single thread.

To see SQL thread status, just type “show slave status\G” on slave.

Relay_Master_Log_File – The name of the master binary log file containing the most recent event executed by the SQL thread.
Exec_Master_Log_Pos – The position in the current master binary log file through which the SQL thread has read and executed.

SQL thread

Configuration of Master Server:

Take backup of database from Master server. The command to take consistent backup is given below:

Edit my.cnf file on the Master server to enable binary logging and set the server’s id.

Add these lines under [mysqld] section:

Restart MySQL for the changes to take effect.

Login into MySQL as root user and create the slave user and grant privileges for replication.

Now execute ‘SHOW MASTER STATUS’ command to get all the data we need.

Show master status

Note the current binary log and position. In our example, the Master server is currently on mysql- bin.00003 binary log and on position 239. Here Binlog_Do_DB  means to capture the DB changes into binary file and Binlog_Ignore_DB  means do not capture the DB changes into  binary file and these are empty because we did not mentioned these parameters in my.cnf file.

Configuration of Slave Server:

Edit my.cnf file on the Slave server.

Add these lines under the [mysqld] section:

Restart MySQL for the changes to take effect.

Now import the dump file that we exported from Master server.

Login into MySQL as root user  and change the Master information with the  ‘CHANGE MASTER TO’ statement as shown below:

Note the values for each field. The MASTER_HOST is the private IP of the Master server, MASTER_USER is the user we created for replication, MASTER_PASSWORD is the password for the replication user, MASTER_LOG_FILE is the binary log that we recorded from the Master server status earlier, and MASTER_LOG_POS is the position the Master was in that we recorded.

Now start the slave thread on the Slave server.

Let’s make sure that replication is working with the ‘SHOW SLAVE STATUS’ statement:

If Slave_IO_Running and   Slave_SQL_Running is YES then your replication is working fine.

Conclusion

  • One of the biggest advantages to have master-slave set up in MySQL is to be able to use master for all of the inserts and send some, if not all, select queries to slave. This will most probably speed up your application without having to diving into optimizing all the queries or buying more hardware.
  • Do backups from slave. That way site is not affected at all when doing backups. This becomes a big deal when your database has grown to multiple gigs and every time you do backups using mysqldump, site lags when table locks happen. For some sites, this could mean that site goes down for few secs to minutes. If we have slave, we just take slave out of rotation and run backups off the slave.

References

3462 Views 5 Views Today