Voiced by Amazon Polly
EBS volume is a storage device that can be attached to an EC2 instance. It functions in a similar way like Pendrive where we copy all files from one device to another. Using EBS volume we can replicate data from one instance to another using master slave replication.
Before starting make sure to set up a master slave relationship between two MySQL servers hosted on the EC2 instance. In this method of master slave replication, we will take a snapshot of the EBS volume of the master instance. From this snapshot, we will be creating a new volume and attaching it to the slave instance, and transfer data for replication.
Master slave replication can be used for databases that are hosted inside EC2 instances i.e., it’s not AWS managed RDS. By having the data automatically copied from a master to a slave database, master-slave data replication enables us to effortlessly maintain multiple copies of a MySQL database. We can scale out our database, perform a backup of the data, or study it separately from the main database. This method helps in the efficient cloning of large master databases. This method clones 150GB EBS volume containing the master database in an hour.
Helping organizations transform their IT infrastructure with top-notch Cloud Computing services
- Cloud Migration
- AIML & IoT
- Make sure that MySQL is hosted inside the EC2 instance.
- Servers must be running on the same OS and the same OS version.
- Both server SQL must be either 32-bit or 64-bit as this method cannot be used for moving data from MySQL 32-bit to MySQL 64-bit.
Steps to Replicate the Data from Master to Slave
- Login to MySQL from the EC2 instance and create a database and table.
- Make sure to give the required permission to the slave instance from a master instance using the below query:
GRANT REPLICATION SLAVE ON *.* to ‘IP of slave EC2 instance’ IDENTIFIED BY ‘Password’;
3. Close all the tables and lock all the tables of the database by using the below query:
flush tables with read lock;
4. Using “reset master” we can delete all binary log files, reset the binary log index, and create a new binary log file and if you want to maintain the existing master slave relationship this step can be skipped.
- Check the status of the master and note down the current master file name and file position as it will be helpful to check when we check the status of the slave.
5. Stop the MySQL instance in the master using the command:
6. From the EC2 console go to the snapshot and choose the volume of which you need to take a snapshot.
7. After taking the snapshot we can start MYSQL in a master instance by using the command: /etc/init.d/mysql start
8. Then login into MySQL and unlock the tables.
9. Select the snapshot and from that, you can create the new volume.
10. After creating the new volume stop the second EC2 instance (slave) and remove the volume from it. Attach the new volume as the root volume and start the instance.
11. In the slave instance in user data, we can mention this query:
12. sudo dd if=/dev/xvda1 of=/dev/null bs=1M so that when the instance restarts then data from the volume will be copied into the slave instance
13. After the instance is started login into the slave instance’s MySQL and change the master and mention the file name and position from the master instance and start the slave instance.
CHANGE MASTER TO
MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS= 706;
14. After starting the slave, we can check the status of the instance and check the database and tables and make sure data is replicated in the slave instance.
| Database |
| information_schema |
| mydb1 |
| mydb2 |
| mysql |
| performance_schema |
| sys |
| testdb |
NOTE: Sometimes when you start a slave you encounter an error, in that case, stop the slave and then start the slave again.
Using EBS volume it is efficient to replicate the data rather than doing manually as taking a snapshot from the master instance and creating a new volume from the same is efficient. AWS’s lazy loading method allows for quick restoration of the complete volume from a snapshot. Up until the data has been loaded, MySQL performance will be sluggish.
Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.
- Cloud Training
- Customized Training
- Experiential Learning
CloudThat is also the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft gold partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.
Drop a query if you have any questions regarding Database Replication, EC2 and I will get back to you quickly.
1. While creating the snapshot will the master instance be functional?
ANS: – Yes, the master instance will be fully functional but before proceeding to do any activity on the master instance we should wait until the snapshot is created.
2. How can I create a duplicate EBS volume in AWS?
ANS: – If we have the snapshot of the volume of which we must create a duplicate volume, then we can select that snapshot and create a new volume by specifying the region.
3. How can we remove the root volume from the EC2 instance and attach a new volume?
ANS: – Before detaching the root volume, we need to stop the instance then select the EBS volume and detach it. Select the new EBS volume which needs to be attached to the EC2 instance and select attach volume as /dev/sda1 (for Linux) and /dev/xvda (for Windows). Then start the EC2 instance.
WRITTEN BY Hridya Hari
Hridya Hari works as a Research Associate - Data and AIoT at CloudThat. She is a data science aspirant who is also passionate about cloud technologies. Her expertise also includes Exploratory Data Analysis.