Voiced by Amazon Polly |
Introduction
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.
Customized Cloud Solutions to Drive your Business Success
- Cloud Migration
- Devops
- AIML & IoT
Use Case
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.
Prerequisite
- 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:
1 |
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:
1 |
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:
/etc/init.d/mysql stop
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.
1 2 3 4 |
CHANGE MASTER TO MASTER_HOST='10.128.0.15',MASTER_USER='replicauser', MASTER_PASSWORD='replicauser-secret-password', 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.
1 2 3 4 5 6 7 8 9 10 11 |
+--------------------+ | 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.
Conclusion
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
About CloudThat
CloudThat is a leading provider of Cloud Training and Consulting services with a global presence in India, the USA, Asia, Europe, and Africa. Specializing in AWS, Microsoft Azure, GCP, VMware, Databricks, and more, the company serves mid-market and enterprise clients, offering comprehensive expertise in Cloud Migration, Data Platforms, DevOps, IoT, AI/ML, and more.
CloudThat is the first Indian Company to win the prestigious Microsoft Partner 2024 Award and is recognized as a top-tier partner with AWS and Microsoft, including the prestigious ‘Think Big’ partner award from AWS and the Microsoft Superstars FY 2023 award in Asia & India. Having trained 850k+ professionals in 600+ cloud certifications and completed 500+ consulting projects globally, CloudThat is an official AWS Premier Consulting Partner, Microsoft Gold Partner, AWS Training Partner, AWS Migration Partner, AWS Data and Analytics Partner, AWS DevOps Competency Partner, AWS GenAI Competency Partner, Amazon QuickSight Service Delivery Partner, Amazon EKS Service Delivery Partner, AWS Microsoft Workload Partners, Amazon EC2 Service Delivery Partner, Amazon ECS Service Delivery Partner, AWS Glue Service Delivery Partner, Amazon Redshift Service Delivery Partner, AWS Control Tower Service Delivery Partner, AWS WAF Service Delivery Partner, Amazon CloudFront Service Delivery Partner, Amazon OpenSearch Service Delivery Partner, AWS DMS Service Delivery Partner, AWS Systems Manager Service Delivery Partner, Amazon RDS Service Delivery Partner, AWS CloudFormation Service Delivery Partner, AWS Config, Amazon EMR and many more.
FAQs
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.
Comments