Home >Database >Mysql Tutorial >How to deploy MySQL using Docker

How to deploy MySQL using Docker

WBOY
WBOYforward
2023-05-27 09:28:051836browse

First we will run tutum-docker-mysql.

docker run -d -p 3306:3306 --name mysql tutum/mysql

If you do not have an image of tutum/mysql locally, docker will download its image first, and this step may take some time. Wait until the execution is completed Let's check it and it should look like the following

How to deploy MySQL using Docker

##tutum-docker-mysql will automatically help us create a random password for us to access, which can be viewed through the log.

How to deploy MySQL using Docker

We log in to mysql through the password in the log

mysql -uadmin -pi6k5usp9km9g -h127.0.0.1

Theoretically, we can successfully log in to mysql at this time. You can create a library, a table, and then exit. .But when the container is stopped and restarted, your data will be lost. How to make your data really saved?

The solution is: mount a local file to the container (mount a local folder from the host on the container to store the database files).

First we stop the previous container

docker stop mysql

We specify a path that can be mounted locally and restart tutum-docker- mysql. We specify /home/walter/softwares/tutum-docker-mysql/data to hang to the /var/lib/mysql directory in the container (-v bind mount a volume). In this way, we can persist the data on the host (host) directory.

sudo docker run -d -p 3306:3306 -v /home/walter/softwares/tutum-docker-mysql/data:/var/lib/mysql -e mysql_pass="mypass" tutum/mysql

We specified the creation password as mypass when we started it above. Now we log in to mysql to create some data and see if it will be saved

shell>mysql -uadmin -pmypass -h127.0.0.1
mysql>create database test;

Exit mysql and restart the container. The operations we have done will be retained. Every time we start this mysql, we can use the following command

docker run -d -p 127.0.0.1:3306:3306 -v /home/walter/softwares/tutum-docker-mysql/data:/var/lib/mysql tutum/mysql

The above is the detailed content of How to deploy MySQL using Docker. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete