Play with Docker: install MySQL and realize data persistence
Tags: mysql, docker, aws, tutorial
Published: 2022-02-11 21:50:20

## Install MySQL Database on AWS EC2 instance with secured data persistence using docker data volume


In this tutorial you will learn:
_How to install MySQL Database using docker on your AWS EC2 instance
How to realize the data persistence_

### Prerequisites

AWS EC2 instance with Linux OS system

Installation of Docker on EC2 instance. [Here] (https://docs.docker.com/engine/install/centos/) is the official instruction to install Docker in Linux OS

Familiar with Linux CLI (Command Line Interface)

### Project setup
To get started, be a root user first and check your docker version using CLI of your EC2 instance to make sure your docker has been installed successfully.
```linux
sudo su
docker version
```
When we run this command, we should see a list of result related with docker version that installed in your VM.

![](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fo9s1511mkjjjgo3if0l.png)

Congratulations! the Docker has been on your remote machine. Now let's start docker engine.
```linux
systemctl start docker
```
Next, let's run 'docker images' to see what happens.
```linux
docker images
```
We should see the following information because currently we do not have any images on our VM. 
```linux
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
```

Now it is time to pull the MySQL image(MySQL 8 as example) using the following command:
```linux
docker pull mysql:8
```
You should see a list of stuff that have been pulled from the library:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yh1742vlwe557eoiiw42.png)

Let's check again to see our image:
```linux
docker images
```
You should see your first image now on your VM:
```linux
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        8         d1dc36cf8d9e   2 weeks ago   519MB
```
OK, MySQL image is ready. 

Let us create folders on our VM to save shared files in the current directory before running the image.
(Hint: use command 'pwd' to check your current directory. Mine is: '/home/ec2-user')

Command to create folders:
```linux
mkdir docker
mkdir docker/mysql
mkdir docker/mysql/conf
mkdir docker/mysql/data
```
Now it is time to turn our MySQL image into container. 

Let's run the following command:
```linux
docker run -d -p 3306:3306 \
-v /home/ec2-user/docker/mysql/conf/:/etc/mysql/conf.d \
-v /home/ec2-user/docker/mysql/data/:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=abcdABCD1234 \
--name docker_mysql d1dc36cf8d9e
```
#### Attention:
Please replace **/home/ec2-user/** part with your own directory; replace **abcdABCD1234** with your own MySQL password and replace **docker_mysql** with your own MySQL container's nickname.

Want to know the exact meanings of each of the command? Please [see docker's official documents] (https://docs.docker.com/storage/volumes/).

The docker container is now running, to double-check we can run the command: 
```linux
docker ps 
```
We can see the following information for the running container:
```linux
CONTAINER ID   IMAGE          COMMAND                  CREATED         STATUS         PORTS                                                  NAMES
62e9c76ac091   d1dc36cf8d9e   "docker-entrypoint.s…"   9 seconds ago   Up 8 seconds   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp, 33060/tcp   docker_mysql
```
Now enter our MySQL container:
```linux
docker exec -it docker_mysql bash
```
We will find the command line changes(with your special container ID). 

We can now login into MySQL using the following command:
```linux
mysql -u root -p
```
The following screenshot shows the changed command line appearance and the command to login into MySQL.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2uzcvyxpf34vlbj26pf.png)

After entering your own MySQL password you set previously, we will successfully login into MySQL:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pzdpujnok324e3p1k1rv.png)

Create our new database in MySQL command-line interface:
```mysql
create database testdatabase;
```
Show all databases:
```mysql
show databases;
```

We should see the database named _testdatabase_ we have just created.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4sh2dh3vzf61iynsv56e.png)

Now just input into command-line interface 'exit' twice and go back to EC2 CLI:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/794556xcyh3zsmlia4my.png)

let's check if we can access the new database from outside the MySQL container.

Simply go to the folder we created previously by typing the command:
```linux
cd docker/mysql/data/
```
Using command 'ls' to list all the files in this directory, we find that the database "testdatabase" exists at the bottom:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x3cgrca7xntwzr18d3yj.png)

We have now realized our data persistence. Because we have saved data on our VM in case the MySQL container crashes or is deleted by mistake.

And finally, do not forget to config the 'Inbound rules' in **Security Groups** on your AWS for your MySQL at port 3306.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0sxvog4r0bdthk1zamht.png)

##### Keep practising and good luck!