Hello and welcome back to another topic on TechDBA. Today we will explore how to configure Replication in MySQL. there are different types of replication in MySQL such Master-Slave , Master-Master and Circular Replication. Our today’s topic will be Master-Slave Replication as other 2 types will be covered in separate session.
First we should know what is Replication in MySQL.
A simple answer If you want to distribute your data at different location you can use MySQL replication technique. Replication enable the data from one MySQL server called as Master to be replicated to other MySQL server called as Slave server.
MySQL Replication are mostly asynchronous in nature that slave need not to connect to all the time to master to receive the update from master.
MySQL Replication can be used for DR purpose or Data Security or Reporting purpose where you don’t have have to fetch the data from production server and automatically load on production server will be decreased.
Now let us see how to configure MySQL MASTER-SLAVE Replication.
To Configure Master-Slave Replication in MySQL minimum 2 servers are required. one will act as Master and other will act as Slave.
Now we have below servers.
MASTER 192.168.201.130
SLAVE 192.168.201.1
Basic Requirement of MySQL Replication is that BINLOG Should be enabled and Server should be identified with unique server id.
make those changes in my.cnf in linux and my.ini in windows.
First stop MySQL Services on Master Server.

Now add below lines in my.cnf and restart MySQL Services

Once you have restarted MySQL Services you can login to database and check the variables if they are properly set or not.

Now master changes have done. On slave server provide a unique server id.

Now login to database and check if the same has done properly or not

Once Configuration changes are done. Create a MySQL user and provide with replication slave privileges.This user will be connected to master database and copy the changes to slave server.

Once the user has been created check the required privileges for the user.

Now take the backup of master database you want to replicate. since i want to replicate only zahid database so i am taking backup of that only. if you want to replicate all the databases you can take backup of all.

Now create database on slave server.

Once the database is being created restore it on slave server using below command.

After restoring database check if everything is working properly on slave server.

Now on Slave server execute below command to configure the replication.

Now on slave server Check slave status whether it is in sync or not using below command.since we have configured just now we need to start slave in order to sync.

Execute below command to start slave.

now check if server is in sync or not. Slave_Io_running and Slave_Sql_Running should be in yes mode then only data will be in sync across the server. if any one of them is no mode means there is some issue.

Now on master i have created a database named rehan and it should be automatically created at slave server.
Now on slave server we can see database is being created. it means replication is working properly.
To stop the slave use below command.

When you stop the slave both Slave_Io_running and Slave_Sql_Running will be in no mode. means slave is stopped and no update from master will be copied to slave unless you start it.

To start Slave use below command.

Once started slave you can check both Slave_Io_running and Slave_Sql_Running should be in yes mode.

Now we are replicating all the databases its not a good idea we should not replicate all the database especially system databases such as mysql or information_schema. here i have created one user and it got replicated automatically on slave server.


To avoid such problem on slave server add the name of databases you want to replicate using replicate-do-db variable name in my.cnf or my.ini. doing so will replication only those databases and will not replication any databases.
you can also use replicate-ignore-db=dbname. here it will not replication this database but whenever you will create new database it will start replicating that. good idea is to use replicate-do-db command.
after changes restart MySQL Services.

Now we have done the changes and restarted MySQL service you can check it will display names of databases will be replicated.

This was simple method to achieve replication in MySQL. you can always explore options available for replication. in next topic we will see how to configure Master-Master replication.
Comment and suggestions are always welcome