Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Containerization of MySQL and explanation of command parameters

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "the containerization mode of MySQL and the explanation of command parameters". In the daily operation, I believe that many people have doubts about the containerization mode of MySQL and the interpretation of command parameters. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "containerization mode of MySQL and command parameter interpretation". Next, please follow the editor to study!

MySQL containerization mode

MySQL is a stateful application. If it is containerized, its state needs to be extracted from the host, and its configuration files, log files and persistent data should be mounted to the host.

Each path in the container

There are several important paths in the official MySQL image pulled by the docker pull command:

Configuration file

The path to the configuration file of MySQL in the image is / etc/mysql, which contains my.cnf configuration and a conf.d folder, and some base setting in the my.cnf file. Our custom configuration should be written in the configuration file under the conf.d folder to overwrite base setting. There are two configuration files under the conf.d folder by default: docker.cnf and mysql.cnf. After mounting the configuration path to the host described later, you should copy these two user-defined configuration files to the host configuration path.

Persistent data

The persistent data of MySQL in the image is stored under / var/lib/mysql.

Command

$docker run\-name db\-e MYSQL_ROOT_PASSWORD=secret\-v / path/to/confdir:/etc/mysql/conf.d\-v / path/to/datadir:/var/lib/mysql\-d mysql:tag

Parameter interpretation

-- name container name

-e set the environment variable for the new container, and the MySQL in the container will use this to initialize the root user password

The-v and-v parameters mount the custom configuration file directory and persistent data directory of MySQL in the container to the host, respectively, with the host directory before the colon and the container directory after the colon.

-d start the container in the background

Tips

We can create a directory on the host to store container status data. The hierarchical structure is as follows:

Docker-container-data |-container1 | |-conf |-logs (under which the path can be subdivided) |-data |-container2 |-container3 |.

Disaster measures

The advantage of mounting the status data to the host is that if there is a problem with the container, you can start a new container and mount the container status path to the original state path on the host, thus resuming production.

You can directly mount the configuration and persistence data of the newly launched MySQL container to the host path of the previous container, and the new container can seamlessly recover the data (configuration, users, libraries, tables, records are all restored).

Matters needing attention

You cannot mount the status path of two running containers to the same host path. If the two containers write the state under the same host path at the same time, the status will not be recognized when the container application reads the state, resulting in a crash.

If the newly launched container wants to mount the host data path of the container before, the root user password environment variable set by the-e MYSQL_ROOT_PASSWORD=foo command parameter will be ignored, because the MySQL user information table is already contained in the mounted data path, and the root user password has been set before.

At this point, the study of "containerization of MySQL and interpretation of command parameters" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report