Use Docker to deploy MySQL (data persistence) and dockermysql

Source: Internet
Author: User
Tags docker run

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.