DEV Community

Cover image for Play with Docker: install MySQL and realize data persistence
Yongchang He
Yongchang He

Posted on • Edited on

Play with Docker: install MySQL and realize data persistence

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 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.

sudo su
docker version
Enter fullscreen mode Exit fullscreen mode

When we run this command, we should see a list of result related with docker version that installed in your VM.

Congratulations! the Docker has been on your remote machine. Now let's start docker engine.

systemctl start docker
Enter fullscreen mode Exit fullscreen mode

Next, let's run 'docker images' to see what happens.

docker images
Enter fullscreen mode Exit fullscreen mode

We should see the following information because currently we do not have any images on our VM.

REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
Enter fullscreen mode Exit fullscreen mode

Now it is time to pull the MySQL image(MySQL 8 as example) using the following command:

docker pull mysql:8
Enter fullscreen mode Exit fullscreen mode

You should see a list of stuff that have been pulled from the library:

Image description

Let's check again to see our image:

docker images
Enter fullscreen mode Exit fullscreen mode

You should see your first image now on your VM:

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
mysql        8         d1dc36cf8d9e   2 weeks ago   519MB
Enter fullscreen mode Exit fullscreen mode

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:

mkdir docker
mkdir docker/mysql
mkdir docker/mysql/conf
mkdir docker/mysql/data
Enter fullscreen mode Exit fullscreen mode

Now it is time to turn our MySQL image into container.

Let's run the following command:

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
Enter fullscreen mode Exit fullscreen mode

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.

The docker container is now running, to double-check we can run the command:

docker ps 
Enter fullscreen mode Exit fullscreen mode

We can see the following information for the running container:

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
Enter fullscreen mode Exit fullscreen mode

Now enter our MySQL container:

docker exec -it docker_mysql bash
Enter fullscreen mode Exit fullscreen mode

We will find the command line changes(with your special container ID).

We can now login into MySQL using the following command:

mysql -u root -p
Enter fullscreen mode Exit fullscreen mode

The following screenshot shows the changed command line appearance and the command to login into MySQL.
Image description

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

Image description

Create our new database in MySQL command-line interface:

create database testdatabase;
Enter fullscreen mode Exit fullscreen mode

Show all databases:

show databases;
Enter fullscreen mode Exit fullscreen mode

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

Image description

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

Image description

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:

cd docker/mysql/data/
Enter fullscreen mode Exit fullscreen mode

Using command 'ls' to list all the files in this directory, we find that the database "testdatabase" exists at the bottom:

Image description

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

Keep practising and good luck!

Top comments (0)