MySQL, the most popular Open Source SQL database management system, is developed, distributed, and supported by Oracle Corporation. MySQL is a database management system. MySQL databases are relational. MySQL software is Open Source. The MySQL Database Server is very fast, reliable, scalable, and easy to use. MySQL Server works in client/server or embedded systems. A large amount of contributed MySQL software is available. – by dev.mysql.com
The best way of handling the Data volume in docker container is, keep the data in a separate container and using by other containers which they call “Data Volume Container”.
PULL MYSQL DOCKER IMAGE
First step is, we need to pull the MySQL docker image from the docker hub repository. Execute the below command to pull the latest MySQL docker image from the docker hub repository.
docker pull mysql/mysql-server:latest
VIEW DOCKER IMAGE
Execute the below command, to view the list of docker images and to make sure the MySQL docker image has been downloaded or not.
create a data volume container
Execute the below command to create a new Data volume container for MySQL. The below command will create a new container to keep the data volume and it will acts a Data Volume Container.
docker create -v /var/lib/mysql --name mysqldata mysql/mysql-server:latest
I named the Data Volume Container as “mysqldata“.
Next, we need to mount the data volume container “mysqldata” when we run the MySQL container.
Execute the below command to run the MySQL as a Docker container, which mounted the Docker Volume Container “mysqldata”.
docker run -p 3306:3306 -d -e MYSQL_ROOT_PASSWORD=root --volumes-from mysqldata --name=mysqldb mysql/mysql-server:latest
I named the MySQL container “mysqldb” and the exposed the port number is -p 3306:3306. Also, I set the MySQL root user password as “root” by using docker environment variable itself.
After we run the MySQL container, we can access the database by executing the Docker Exec command and create a new database called “MY_FIRST_DATABASE”.
docker exec -it mysqldb bash
Then, execute the below command to login into the MySQL database.
mysql -uroot -p
Console will ask you the password, provide the root user password we setted by using Docker environment variable.
Now, execute the below SQL command to create a new database.
CREATE DATABASE MY_FIRST_DATABASE
We can delete the “mysqldb” container without affecting our data in “mysqldata”.
To test the data volume, delete the “mysqldb” container. Then, run a new container “mysqldb1”.
docker rm -f mysqldb
docker run --name mysqldb1 --volumes-from mysqldata -e MYSQL_ROOT_PASSWORD=password -p 3306:3306 mysql/mysql-server:latest
Then, login into the database by executing the Docker Exec command once again.
docker exec -it mysqldb1 bash
After login into the database, execute the below SQL command to view the list of databases. Then the “MY_FIRST_DATABASE” database will be available.
Data Volume path has been successfully mounted to MySQL docker container.
How useful was this post?
Click on a star to rate it!
No votes so far! Be the first to rate this post.