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

HTML Language Encyclopedia

123WORDPRESS.COM--HTML超文本标记语言速查手册<!-- --> !D...

5 MySQL GUI tools recommended to help you with database management

There are many database management tools for MySQ...

Detailed explanation of software configuration using docker-compose in linux

Preface This article will share some docker-compo...

Introduction to HTML basic controls_PowerNode Java Academy

The <input> tag The <input> tag is us...

Solve the MySQL login 1045 problem under centos

Since the entire application needs to be deployed...

VMware virtualization kvm installation and deployment tutorial summary

Virtualization 1. Environment Centos7.3 Disable s...

Simply learn various SQL joins

The SQL JOIN clause is used to join rows from two...

Four ways to modify the default CSS style of element-ui components in Vue

Table of contents Preface 1. Use global unified o...

Steps for IDEA to integrate Docker to achieve remote deployment

1. Enable remote access to the docker server Log ...

Mysql5.7.14 Linux version password forgotten perfect solution

In the /etc/my.conf file, add the following line ...

JS Decorator Pattern and TypeScript Decorators

Table of contents Introduction to the Decorator P...

Detailed explanation of CocosCreator Huarongdao digital puzzle

Table of contents Preface text 1. Panel 2. Huaron...

Detailed explanation of putting common nginx commands into shell scripts

1. Create a folder to store nginx shell scripts /...

About the layout method of content overflow in table

What is content overflow? In fact, when there is ...