MySQL Master-Slave Replication on different AWS instances.

Share via:

MySQL Master-Slave Replication on different AWS instances.

In this article i want show how to configure master-slave replication in two different machines.

What is Replication ?

Replication is the continuous copying of data changes from one database (MASTER) to another database (SLAVE ).

The two databases are generally located on a different physical servers, resulting in a load balancing framework by distributing assorted database queries and providing fail-over capability.

The server for the SLAVE  database may be configured as a backup in the event of failure of the server for the MASTER database.

What is MySQL Replication ?

MySQL replication is a process that enables data from one MySQL database server (the master) to be copied automatically to one or more MySQL database servers (the slaves). It is usually used to spread read access on multiple servers for Scalability, although it can also be used for other purposes such as for fail-over, or analyzing data on the slave in order not to overload the master.

As the master-slave replication is a one-way replication (from master to slave), only the master database is used for the write operations, while read operations may be spread on multiple slave databases. What this means is that if master-slave replication is used as the scale-out solution, you need to have at least two data sources defined, one for write operations and the second for read operations.

In MySQL , How replication will  work internally ?

 

 

Important lines in MySQL Replication : 

–>The slave server always connects with Master server.

–>MySQL Replication works based on Three types threads

Binary thread

I/O thread

Slave SQL thread

Binary thread :
The Binary thread is running on the master side  and it will sending those binary logs to slave.

–>On slave side we have two thread

I/O thread
Slave  SQL thread

–>i/o thread will connecting to master and getting those binary logs and Slave sql thread will execute those binary logs to slave machine.

At slave side , Relays will be created

Relay logs :

The relay log is a set of log files created by a slave during replication. It’s the same format as the binary log, containing a record of events that affect the data or structure; thus, mysqlbinlog can be used to display its contents.

 

Prerequisite for MySQL replication :

  1. Both master and slave should be running with different server id.
  2. Master server should be enabled with binary logs.

Step : 1

Make sure the  id of both the servers should be different.

Master Server : 

Check server id need to connect mysql and issue following command.

For master server the id is 1.

Slave Server   :

For slave process  also the id is same.

If server id is same replication is not possible, so we need change the server id.

How to change sever id :

Open my.cnf file and write

Restart the service and check server id.

There serverid & server uuid will be written for every sql statement in binary logs :

Step  2 :

Check binary log is enabled or not on both side .

Master Server : 

Slave Server   :

Step 3 :

In Master side we need to create a user with required privileges.

Here i have created a user called replica with privileges called replication slave , replication client.

When we are performing changes to server , content will be added to binary logs.

check binary logs on master.

Step 3 :

Check ip address of master .

The Master ip address will be given to server to get binary to slave side.

Slave Server :

What is the use of each variable ?

Master_host            –>  Need to give master host address
master_port             –>  Need to write Master mysql db running port number
master_user            –>  The user which is created for replication and privs
master_password   –>  Password of that user
master_log_file       –>  Binary logfile  which currently running at master side
master_log_pos      –>  Binary logfile master log postion

Step 4 :

Check status of slave process at slave side :

The \G rearranges the text to make it more readable.

In above example, the below two variables are showing NO.

Once we start the slave process, the above two variable  will show as yes.

Activate the slave server:

Use the database crm and create a table.

Now insert the records and check at slave side.

Step 6:

Slave Side :

IN Slave Sever and check records.

Check Relay logs at slave side :

In above example we are able to see relay logs with contents  and also relay-log.info and master.info files.

 

 

 

Share via:
Note: Please test scripts in Non Prod before trying in Production.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

One thought on “MySQL Master-Slave Replication on different AWS instances.

Add Comment