Detailed explanation of deploying MySQL using Docker (data persistence)

Detailed explanation of deploying MySQL using Docker (data persistence)

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 server, saving the time of rewriting the Dockerfile.

First we run tutum-docker-mysql.

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

If you don't have the tutum/mysql image locally, Docker will download its image first. This step may take some time. After the execution is completed, we check that it should look like this

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

We log in to mysql through the password in the log

mysql -uadmin -pi6k5USp9Km9G -h127.0.0.1

Theoretically, we have successfully logged into MySQL at this point. You can create a database, a table, and then exit. But when the container is stopped and restarted, your data will be lost. How can you really save your data?

The solution is to 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 local mountable path and restart tutum-docker-mysql. We specify /home/walter/softwares/tutum-docker-mysql/data to be mounted to the /var/lib/mysql directory in the Container (-v Bind mount a volume). In this way, we can persist the data in the directory of the host.

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

When we started it above, we specified the password to create it as mypass. Now let's log in to MySQL and create some data to 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 performed will be retained. Each time we start 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 full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Docker mounts MySQL to achieve data persistence

<<:  Nodejs implements intranet penetration service

>>:  MySQL 8.0.12 decompression version installation graphic tutorial under Windows 10

Recommend

JavaScript to dynamically load and delete tables

This article shares the specific code of JavaScri...

Basic operations on invisible columns in MySQL 8.0

Table of contents 01 Create invisible columns 02 ...

How to display the border when td is empty

Previously, I summarized how to use CSS to achieve...

Detailed explanation of Windows time server configuration method

Recently, I found that the company's server t...

Process parsing of reserved word instructions in Dockerfile

Table of contents 1. What is Dockerfile? 2. Analy...

In-depth analysis of JDBC and MySQL temporary tablespace

background Temporary tablespaces are used to mana...

Practical method of deleting files from Linux command line

rm Command The rm command is a command that most ...

How to load the camera in HTML

Effect diagram: Overall effect: Video loading: Ph...

Solve the problem of MySQL using not in to include null values

Notice! ! ! select * from user where uid not in (...

Detailed explanation of the benefits of PNG in various network image formats

BMP is an image file format that is independent o...

CentOS6.9+Mysql5.7.18 source code installation detailed tutorial

CentOS6.9+Mysql5.7.18 source code installation, t...

Vue+element ui realizes anchor positioning

This article example shares the specific code of ...

Detailed explanation of the use and precautions of crontab under Linux

Crontab is a command used to set up periodic exec...