Technical work, should be praised and then see, form a habitCopy the code

Docker use tutorial related series of directories


scenario

Zhao Tiezhu, A citizen, worked as A development engineer in A company. One day, A website zhao Tiezhu was responsible for maintaining could not be accessed. After investigation, it was the mysql container of Docker that hung up. Thinking about what to order for lunch.

The mysql container was started successfully, and then I visited the website to test it, and all the data was lost. Let’s observe three seconds of silence for zhao Tieju

The analysis reason

Zhao Tiezhu is made what mistake, bring about data loss.

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

The solution

conclusion

Mount data from the container to the host

Docker: How to mount data from host to container

1. Create a configuration file

Create a configuration file location and a data mapping location

mkdir -p /mysql/config /mysql/data
Copy the code

Create edit configuration file

vi /mysql/config/my.conf
Copy the code

The contents of the my.conf configuration file are 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
Copy the code

2. Start the container

Take a look at the already running container and take mysql57 out of service

docker ps
docker stop mysql57
Copy the code

 

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 package - v = / mysql/data: / var/lib/mysql mysql: 5.7Copy the code

 

-d Background run container -p 3306:3306 Specify port mapping (host (host) port: container port) --restart=always startup --privileged=true Privileged in the container --name Specify a name for the container -e Set environment variables MYSQL_ROOT_PASSWORD = root initial password - v/mysql/config/my conf: / etc/my cof package mapping configuration file - v = / mysql/data: / var/lib/mysql mapping data directory mysql: 5.7 Image name and version numberCopy the code

3. Test the connection

4. Verify data persistence

1, create database, create table, insert data

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,' test_name 'varchar(32) NOT NULL, PRIMARY KEY (`uuid`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of test_table -- ---------------------------- INSERT INTO `test_table` VALUES (' aAAA ', 'test data ');Copy the code

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

Start dream_mysQL57_3307. 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 package - v = / mysql/data: / var/lib/mysql mysql: 5.7Copy the code

Close the current mysql container

docker stop dream_mysql57
Copy the code

Access the 3307 service

The data is still there

Mysql > delete mysql from mysql.mysql

Action is better than action. Let’s test it

Let’s get rid of all mysql containers

Now it’s gone

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 package - v = / mysql/data: / var/lib/mysql mysql: 5.7Copy the code

Let’s see if the container is activated before we visit

The container is started

Then access it through the client

 

The data is there. Happy that the boss won’t Sue me this time.