Use Docker to deploy MySQL (data persistence) and dockermysql
Link: http://walterinsh.github.io/2015/01/17/use-mysql-with-docker.html
This article briefly describes how to use Docker to deploy mysql and persist data. We will use the tutum-docker-mysql project to build a mysql, saving the time to rewrite the Dockerfile.
Run tutum-docker-mysql first.
docker run -d -p 3306:3306 --name mysql tutum/mysql
If you do not have a tutum/mysql image locally, docker will first download its image. This step may take some time. After the execution is complete, we should check the image as follows:
Tutum-docker-mysql will automatically create a random password for us to access. You can view it through the log.
We log on to mysql using the password on the log.
mysql -uadmin -pi6k5USp9Km9G -h127.0.0.1
In theory, we have successfully logged on to mysql at this time. You can create a database, a table, and then exit. however, when the container is stopped and restarted, your data will be lost. how can we make your data really saved?
Solution: Mount a local file to the Container (Mount a local folder from the host on the container to store the database files ).
First, stop the previous Container.
docker stop mysql
Specify a local Mount path and restart tutum-docker-mysql. we specify/home/walter/softwares/tutum-docker-mysql/data to mount/var/lib/mysql directory (-v Bind mount a volume) in the iner ). in this way, the data can be persisted in the 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 password mypass when we started the above. Now we can log on to mysql to create some data to see if it will be saved.
shell>mysql -uadmin -pmypass -h127.0.0.1mysql>create databases test;
Exit mysql and restart Container. The operations we do will be retained. The following command can be used to start mysql every time.
docker run -d -p 127.0.0.1:3306:3306 -v /home/walter/softwares/tutum-docker-mysql/data:/var/lib/mysql tutum/mysql