The company's mysql docker container is down and the data is lost. The boss wants to sue me. .

Docker use tutorial related series catalog


table of Contents

Scenes

Analyze the reasons

solution

in conclusion

1. Create a configuration file

2. Start the container

3. Test the connection

4. Verify data persistence

1. Create a database, create a table, insert data

2. Start a new mysql container and close the current mysql container

3. Some students may ask, if the current container is deleted, then start a new mysql container, the data is still there


Scenes

Citizen Zhao Tiezhu works as a development engineer in Company A. One day, a website maintained by Zhao Tiezhu could not be accessed. After investigation, it was found that the mysql container of docker was hung up. This is simple. Zhao Tiezhu took a cup of coffee without rushing. Started the mysql container. I thought about what to order for takeaway at noon.

Seeing that the mysql container started successfully, and then I visited the website to test it, and the data was gone. . Let us mourn for the citizen Zhao Tiezhu for three seconds

Analyze the reasons

What mistake did Zhao Tiezhu make that caused the loss of data?

After investigation, it was found that Zhao Tiezhu did not do the data persistence of the mysql container in order to be lazy.

solution

in conclusion

Mount the data from the container to the host

For students who don’t understand docker's data volume mounting, please see here: How docker mounts data from the host to the container (1)

1. Create a configuration file

Create configuration file storage location and data mapping location

mkdir -p /mysql/config /mysql/data

Create and edit configuration files

vi /mysql/config/my.conf

 The content of my.conf configuration file is as follows

 

[mysqld]
user=mysql
character-set-server=utf8
default_authentication_plugin=mysql_native_password

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

2. Start the container

First check the containers that are already running, and stop the mysql57 container from serving

docker ps
docker stop mysql57

 0

docker run -d -p 3306:3306 --restart always --privileged=true --name dream_mysql57 -e MYSQL_ROOT_PASSWORD=root -v /mysql/config/my.conf:/etc/my.cof -v=/mysql/data:/var/lib/mysql mysql:5.7

 0

-d 	后台运行容器
-p 3306:3306 	指定端口映射(主机(宿主)端口:容器端口)
--restart=always 	开机启动
--privileged=true 	提升容器内权限
--name 	为容器指定一个名称
-e  	设置环境变量
MYSQL_ROOT_PASSWORD=root 	初始密码
-v /mysql/config/my.conf:/etc/my.cof 映射配置文件
-v=/mysql/data:/var/lib/mysql 映射数据目录
mysql:5.7 	镜像名称和版本号

3. Test the connection

0

4. Verify data persistence

1. Create a database, create a table, insert data

0

Enter the test database and execute the following sql script

DROP TABLE IF EXISTS `test_table`;
CREATE TABLE `test_table` (
  `uuid` varchar(32) NOT NULL,
  `test_name` varchar(32) NOT NULL COMMENT '姓名',
  PRIMARY KEY (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of test_table
-- ----------------------------
INSERT INTO `test_table` VALUES ('aaaa', '测试数据');

0

2. Start a new mysql container and close the current mysql container

Start the dream_mysql57_3307 container, and the external mapping port is 3307

docker run -d -p 3307:3306 --restart always --privileged=true --name dream_mysql57_3307 -e MYSQL_ROOT_PASSWORD=root -v /mysql/config/my.conf:/etc/my.cof -v=/mysql/data:/var/lib/mysql mysql:5.7

Close the current mysql container

docker stop dream_mysql57

0

Access 3307 service 

0

0

The data is still there

3. Some students may ask, if the current container is deleted, then start a new mysql container, the data is still there

Action is not as good as heart, let’s verify

First kill all the mysql containers

0

No more now

0 Then recreate the mysql container

docker run -d -p 3306:3306 --restart always --privileged=true --name dream_mysql57_3306 -e MYSQL_ROOT_PASSWORD=root -v /mysql/config/my.conf:/etc/my.cof -v=/mysql/data:/var/lib/mysql mysql:5.7

Don't rush to visit, first check if the container is started

The container is started

0

Then access through the client

0 

0

The data is also there. This time the boss will not sue me, happy. .

Guess you like

Origin blog.csdn.net/shi_hong_fei_hei/article/details/114766203