Docker data volume technology and MySQL actual combat

1. Data volume technology

Before understanding what data volume technology is, I have always had a problem. Containers and data seem to coexist and die, that is, delete the container and the data in it will disappear. The data volume technology solves this problem. Summarize in one sentence: container persistence and synchronization operations and data sharing between containers.

1. Data volume application

command:

docker run -v  主机目录:容器目录

Screenshot:
Insert picture description here

Insert picture description here
In this way, the /home directory of the container is mounted under /home/ceshi of the host

Use the docker inspect 容器idcommand to view the detailed information of the container.
Insert picture description here
Note: After mounting, the data of the container and the host are synchronized, that is, changing the host /home/ceshi container /home directory will also change, and vice versa.

2. Anonymous mount and named mount

Anonymous mount

docker run -v 容器路径

docker volume ls # 查看所有卷

Screenshot: As
Insert picture description here
you can see, its name is a long string of characters, indicating an anonymous mount.

Named mount

docker run -v 卷名:容器路径

Screenshot: As
Insert picture description here
you can see, its name is our name, which is named mount.

Take a look at the named mounted volume

docker volume inspect 挂载的卷名

Screenshot:
Insert picture description here
We can see that its mounting location is under /var/lib/docker/volumes/juming-nginx/_data
normal circumstances, we will mount the appliance name

Summary of mounting methods:

  1. -v container path (anonymous mount)
  2. -v volume name: path in the container (named mount)
  3. -v /host path: path in the container (specified path to mount)

Expansion:

#通过  -v 容器内路径:ro rw 改变文件的读写权限
#如
-v  卷名:容器内路径:ro
-v  卷名:容器内路径:rw

2. MySQL combat

1. Pull the mirror

docker pull mysql:5.7

Screenshot:
Insert picture description here

2. Start in the background

command

docker run --name mysql02 -p 8004:3306 -v /home/mysql/conf:/etc/mysql/conf.d  -e MYSQL_ROOT_PASSWORD=xxx -v /home/mysql/data:/var/lib/mysql -d  mysql:5.7

Explanation:

  • -v means to use data volume technology to mount the configuration and data of the database to the host, so that even if the container is deleted, the data is still there
  • -d means to start in the background
  • -e MYSQL_ROOT_PASSWORD=xxx represents the password of the database
  • --Name represents the container name

Connection test:
Insert picture description here

Problems encountered: ERROR 1130: Host '192.168.1.3' is not allowed to connect to this MySQL server 1.
Solution:

#进入容器
 docker exec -it mysql02 /bin/bash

#启动mysql,如果直接回车可以进去就先进去,然后修改密码;如果回车进不去就输入密码MYSQL_ROOT_PASSWORD=xxx中的xxx
mysql -u root -p

#进去后,授权
use mysql;
update user set host = '%' where user = 'root';
flush privileges;

Guess you like

Origin blog.csdn.net/weixin_43520670/article/details/113561910