Record the process of Docker setting up MySQL and mounting data. The process of setting up is referenced from Docker Hub

Series welcome to visit: www.itwxe.com/posts/9e76d.

Docker builds MySQL and mounts data

1, first install Docker, do not know how to install can see CentOS7 install Docker first experience.

2. Download the MySQL5.7 image.

Docker pull mysql: 5.7Copy the code

3. Create a container and mount data.

docker run -d --restart=always --name mysql \
-v /itwxe/dockerData/mysql/data:/var/lib/mysql \
-v /itwxe/dockerData/mysql/conf:/etc/mysql \
-v /itwxe/dockerData/mysql/log:/var/log/mysql \ -p 3306:3306 \ -e TZ=Asia/Shanghai \ -e MYSQL_ROOT_PASSWORD=123456 \ mysql:5.7 \ --character-set-server=utf8mb4  \ --collation-server=utf8mb4_general_ciCopy the code

Parameter description:

- v/itwxe/dockerData/mysql/data: / var/lib/mysql: the data folder mount to the host - v/itwxe/dockerData/mysql/conf: / etc/mysql: By attaching the configuration folder to the host, you can place a custom my.cnf file on the host, and the container will start with the custom configuration -v /itwxe/dockerData/mysql/log:/var/log/mysql: mount the log folder to the host -p 3306:3306: map port 3306 of the container to port 3306 of the host -e MYSQL_ROOT_PASSWORD=123456: Initialize the password of user 123456. --character-set-server= UTF8MB4: sets the character set. --collation-server= UTF8MB4_general_CI: sort modeCopy the code

4. Enter the MySQL container.

docker exec -it mysql /bin/bash
Copy the code

5. Log in to the MySQL database.

mysql -uroot -p123456
Copy the code

6. Check whether the character set is valid.

show variables like 'character_set_%';
Copy the code

2. Whether the mount takes effect

1. Upload the backup SQL to the host and copy the backup SQL to the container.

docker cp /itwxe/dockerData/sunny.sql mysql:/
Copy the code

2. Enter the mysql container and import SQL.

docker exec -it mysql /bin/bash

mysql -uroot -p123456

create database sunny;
use sunny;
source /sunny.sql;
Copy the code

3. After the data is imported successfully and can be queried, delete the container, run the command to create the container again, and check the data exists.

docker stop mysql && docker rm mysql
Copy the code

Run the preceding command to create the container again and check whether the data is normal. You can see that the data is properly mounted and queried. The data shown here is properly mounted and stored on the host.

Now that you’ve read this, like, comment, follow, or collect it!

Author: IT wang2 xiao3 er4 starting address: www.itwxe.com/posts/53489… Copyright notice: the content of the article is subject to the authorship-non-commercial-no deduction 4.0 international license, except for special notice, all original, reprint please give the author and the original link in the obvious position of the article page.