Installation and deployment environment
- Ubuntu 18.04.3 LTS
- Docker 19.03.2
- MySQL latest(8.0.17)
Download image
# docker从仓库中拉取最新版的mysql镜像,如果没加标签的话,默认获取最新的版本
Docker pull mysql
Mounting data volumes and configuration files
In principle Docker container it is short, if the container is damaged or deleted, any data or configuration will be lost. Because we need to transfer data files and MySQL configuration file to the specified path in the Docker's host.
MySQL deployments within each person, may not match the file path. We can create a test MySQL container, and then find out according to the specific file path location, we re-create the MySQL container in line with our needs, the command is as follows:
docker run --name mysqltest \ -p 3307:3306 -e MYSQL_ROOT_PASSWORD=root \ -d mysql
Docker into the container
docker exec -it mysqltest bash
Determine within Docker MySQL-related files path
According to the official website Description : If you want to mount the MySQL configuration file, we have to exist in the configuration file on a physical machine.
# Find within Docker, MySQL configuration file my.cnf location of MySQL --help | grep my.cnf # shown below, meaning that their order of priority path, the path would be in the following years: the Order of preference, my.cnf, $ MYSQL_TCP_PORT , /etc/my.cnf /etc/mysql/my.cnf ~ / .my.cnf path # configuration file is not necessarily the same, the location is described in some blog /etc/my.cnf. And on my Ubuntu system, the actual location of presence in /etc/mysql/my.cnf
Find the data file locations
For operating on the container Docker Inspect the JSON output command having a Mount
key, which provide information about the value of the volume data directory:
docker inspect mysqltest
... "Mounts": [ { "Type": "volume", "Name": "4f2d463cfc4bdd4baebcb098c97d7da3337195ed2c6572bc0b89f7e845d27652", "Source": "/var/lib/docker/volumes/4f2d463cfc4bdd4baebcb098c97d7da3337195ed2c6572bc0b89f7e845d27652/_data", "Destination": "/var/lib/mysql", "Driver": "local", "Mode": "", "RW": true, "Propagation": "" } ], ...
Output display source folder: / var / lib / docker / volumes, expressed installed /var/lib/mysql
server data directory within the container.
Create a local path and mount Docker in the data
Next, we need to on a physical machine, to create the specified mount path to a good data and configuration files.
mkdir -p /home/docker/mysql/conf && mkdir -p /home/docker/mysql/datadir
After creating local mount data path, and then we test container copy MySQL configuration file to the path. The future need to change the configuration, modification can be mounted directly on the path of the configuration file.
docker cp mysqltest:/etc/mysql/my.cnf /home/docker/mysql/conf
Create a container and start the MySQL
Solve the problem of the configuration file, we can create our MySQL container according to the needs and mount the data.
docker run --name mysql1 \ -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root \ --mount type=bind,src=/home/docker/mysql/conf/my.cnf,dst=/etc/mysql/my.cnf \ --mount type=bind,src=/home/docker/mysql/datadir,dst=/var/lib/mysql \ --restart=on-failure:3 \ -d mysql
- --name : specify a name for the container
- -p : Specifies port mapping format: a host (host) port: the port of the container
- -e : username = "xxx", set the environment variable
- --restart = on-failure: 3 cycles restart case refers to a container in the future appear abnormal exit (exit code of non-0) three times:
- -mount : bind mounts
- -d : background container and return the container id
Many people write online blog mount MySQL data volume method, using
--volume
, but to build MySQL documentation, we recommend that you use the latest MySQL official Docker--mount
, because studies have shown that it is easier to use. Therefore, we According to the official website of the recommended approach, also used here--mount
to mount.
In view of the container operation
docker ps
You can see themysql1
container is running, including a test vessel before we create.