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
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
Next, let's run 'docker images' to see what happens.
docker images
We should see the following information because currently we do not have any images on our VM.
REPOSITORY TAG IMAGE ID CREATED SIZE
Now it is time to pull the MySQL image(MySQL 8 as example) using the following command:
docker pull mysql:8
You should see a list of stuff that have been pulled from the library:
Let's check again to see our image:
docker images
You should see your first image now on your VM:
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:
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:
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.
The docker container is now running, to double-check we can run the command:
docker ps
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
Now enter our MySQL container:
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:
mysql -u root -p
The following screenshot shows the changed command line appearance and the command to login into MySQL.
After entering your own MySQL password you set previously, we will successfully login into MySQL:
Create our new database in MySQL command-line interface:
create database testdatabase;
Show all databases:
show databases;
We should see the database named testdatabase we have just created.
Now just input into command-line interface 'exit' twice and go back to EC2 CLI:
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/
Using command 'ls' to list all the files in this directory, we find that the database "testdatabase" exists at the bottom:
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.
Top comments (0)