Demonstrate data persistence between two different MySQL databases hosted on two separate EC2 instances in Docker using Elastic Block Storage (EBS)

Biswajit Nandi
3 min readSep 21, 2023

--

Demonstrate data persistence between two different MySQL databases hosted on two separate EC2 instances in Docker using Elastic Block Storage (EBS).

To demonstrate data persistence between two different MySQL databases hosted on two separate EC2 instances using external volume Elastic Block Storage (EBS) in Docker, we’ll go through the steps to set up and configure the environment.

This will involve creating Docker containers for MySQL instances and configuring them to use EBS volumes for data persistence.

Section 1:

Step 1: Create an EC2 Instance EC2_1(mysql), If you don't know how to create you can follow the instructions here.

Connect to the instance and install docker & docker-compose.

sudo apt update
sudo apt install docker docker-compose

Step 2: Create an extra volume here, If you don’t know how to create you can follow the instructions here.

# Create a directory on EC2_1(mysql)
sudo mkdir mydata

# Check available volume
sudo lsblk

# Formate new volume
sudo mkfs.ext4 /dev/xvdf

# Mount new volume to the new directory
sudo mount /dev/xvdf ./mydata

Step 3: Create a docker-compose file and up docker-compose.

sudo nano docker-compose.yml

Put the code on yml file.

version: '3.8'

services:
db:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: 1234
ports:
- "3306:3306"

volumes:
- ./mydata:/var/lib/mysql

Up the docker-compose in detached mode and check the container is running.

sudo docker-compose up -d
sudo docker ps

Login to the container.

sudo docker exec -it container_id sh

Step 4: Login to the mysql database & create a table with insert data.

mysql -p

Create a table and insert data into the database.

# Check database
show databases;

# Create database
create database db;

# Select database
use db;

# Create table
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
email VARCHAR(100)
);

# Insert data to table
INSERT INTO users (first_name, last_name, email)
VALUES
('John', 'Doe', 'john.doe@example.com'),
('Jane', 'Smith', 'jane.smith@example.com'),
('Michael', 'Johnson', 'michael.johnson@example.com');

# Check Insert Data
select * from users;

We are done for EC2_1(mysql), Now exit from mysql & container.

# Exit from mysql
exit

# Exit from container
exit

Section 2:

Step 1: Create an EC2 Instance EC2_2(backup_mysql), If you don’t know how to create you can follow the instructions here.

Detach the extra volume from EC2_1(mysql) & attach it to EC2_2(backup_mysql).

Connect to EC2_2(backup_mysql) and install docker & docker-composer.

sudo apt update
sudo apt install docker docker-compose

Create a directory & mount the volume.

# Create a directory on EC2_2(backup_mysql)
sudo mkdir backup_data

# Check available volume
sudo lsblk

# Mount new volume to the new directory
sudo mount /dev/xvdf ./backup_data

Step 2: Create a docker-compose file.

nano docker-compose.yml

Put the code on yml file.

version: '3.8'

services:
db:
image: mysql:8.0
restart: always
environment:
MYSQL_ROOT_PASSWORD: 1234
ports:
- "3306:3306"

volumes:
- ./backup_data:/var/lib/mysql

Up the docker-compose in detached mode and check the container is running.

sudo docker-compose up -d
sudo docker ps

Login to the container.

sudo docker exec -it container_id sh

Step 3: Login to the mysql.

mysql -p

Check database & data.

# Check databases;
show databases;

# Select database;
use db;

# Check previse inset data which we insert from EC2_1(mysql)
select * from users;

We are done for EC2_2(backup_mysql), Now exit from mysql & container.

# Exit from mysql
exit

# Exit from container
exit
Data we insert from EC2_1(mysql) show in EC2_2(backup_mysql)
Data we insert from EC2_1(mysql) show in EC2_2(backup_mysql)

At this stage, we see which data we insert from EC2_1(mysql) that see on this EC2_2(backup_mysql) instance.

Congratulations, we have successfully demonstrated data persistence between two different MySQL databases hosted on two separate EC2 instances using Elastic Block Storage (EBS).

If you face any issues with these demonstrated data persistence feel free to contact me. I will try my best. Thank you

--

--

Biswajit Nandi

Hello! I'm a DevOps engineer with experience in AWS, GCP, Kubernetes, Docker, Terraform and full CI/CD pipelines.