Docker deploys mysql detailed steps and pitfalls

1. Pull the image

sudo docker pull mysql

If you do not specify a version number, the latest version is pulled by default. An example of specifying a version number is as follows

sudo docker pull mysql:5.7

You can go to the official website of dockerHub https://registry.hub.docker.com/ to find the required image, no need to log in, just search directly

 After finding mysql, select the Tags page, find the required version, and copy the pull command

2. Create an instance

sudo docker run -d -p 3306:3306
 -v /usr/local/mysql/conf:/etc/mysql/conf.d
 -v /usr/local/mysql/data:/var/lib/mysql
 -e MYSQL_ROOT_PASSWORD=123456
 --name  mysql mysql:latest

-p 3306:3306
mapping port
-v /usr/local/mysql/conf:/etc/mysql/conf.d
mount directory, before the colon (:) is the host directory, after that is the docker internal directory
-e MYSQL_ROOT_PASSWORD=123456
Set the root user password
--name mysql
specify the container name
mysql:latest
specify the image and version

3. Check whether the deployment is successful

docker ps

Seeing the running instance indicates that the deployment is successful

4. Adjust the time zone

When docker deploys mysql, the time zone will be incorrect. After adjusting the time zone, the problem will be solved.

docker cp /usr/share/zoneinfo/Asia/Shanghai 47799179873e:/etc/localtime

 47799179873e is the container ID, replace it with your container ID, you can view the container ID with docker ps

5. Slow connection establishment

mySql will perform DNS resolution on connected clients, resulting in a slow connection. DNS resolution can be disabled through configuration.

Modify the /etc/mysql/conf.d/mysqld.cnf file in the container, add skip-name-resolve in the last line , restart the container after modification, and the problem is solved.

 

Modification method:
(1) Enter the docker container and modify it directly with the vi command. If the vi command is not installed in the docker, you can use method 2 or 3
to enter the container

docker exec -it 容器ID /bin/bash

modify file

vi /etc/mysql/conf.d/mysqld.cnf


(2) Copy the files in the container to the local, modify, and then copy the modified files back to the docker container
Copy the files in the docker to the local, $PWD represents the current directory

docker cp 容器ID:/etc/mysql/conf.d/mysqld.cnf $PWD

Modify local files

vi mysqld.cnf

Copy the modified file to the inside of docker

docker cp $PWD/mysqld.cnf 容器ID:/etc/mysql/conf.d/mysqld.cnf


(3) Directly modify the local mount file
to view the mount directory

docker inspect 容器ID | grep Binds -A 4

My mount directory is as follows, directly modify

So far, all the modification methods have been introduced, restart the container, and the problem is solved.

6. When the Navicat client connects to mySql, it is very slow to query again after a long period of inactivity

I installed the latest version of mysql. During the test, I found that the Navicat client will be disconnected if there is no operation for 10 minutes, and the reconnection is very slow after another operation. According to the information, the new version of mysql will regularly clean up the connections that have not been operated for a long time to ensure performance.

Many netizens provide solutions to modify wait_timeout, and the test is invalid.

solution:

Edit Connection > Advanced > Tick Keep Alive Interval

Navicat will send a heartbeat to the server according to the configured time to keep the connection. The pro-test is effective and the problem is solved.

 

Guess you like

Origin blog.csdn.net/secretdaixin/article/details/128116127