Docker quick installation of Mysql

Configure Alibaba Cloud Image Acceleration

The Docker image source is abroad, and the domestic access speed is very slow. To obtain a higher download speed, you need to configure a domestic image source.

Enter the Alibaba Cloud image acceleration configuration page cr.console.aliyun.com/cn-hangzhou ... (login required) to obtain the acceleration address under your account

/etc/docker/daemon.jsonUse the accelerator by modifying the daemon configuration file

The following are the configuration commands for CentOs and Ubuntu systems, please replace the mirror acceleration address (unavailable in the code) with your own address 罒ω罒

 
 
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://kwli5l3lxi5a0mx.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

To view the image acceleration configuration of other systems, please refer to the Alibaba Cloud image acceleration document

Docker install Mysql

Go to hub.docker.com/ to find the image that needs to be installed, and select the appropriate version

For example, searching for mysql shows hub.docker.com/_/mysql?tab…

Just copy the command directly

 
 
$ docker pull mysql:8

After pulling the image successfully, you can view the locally pulled image

 
 
$ docker images

Quick installation generally does not need to mount the configuration. If you need to mount the configuration file, you need to check the configuration file path and file name of the current version of Mysql.

Create a test container instance and start it

 
 
$ docker run -p 3306:3306 --name mysqltest -e MYSQL_ROOT_PASSWORD=root -d mysql:8

Parameter Description:

 
 
- p 3306:3306 : 将容器的 3306 端口映射到主机的 3306 端口
- e MYSQL_ROOT_PASSWORD=root : 设置 mysql 登录密码
- d 后台运行容器,并返回容器 id
mysql:8 我运行的镜像名,也可替换成镜像 id

Enter the Mysql container

 
 
$ docker exec -it mysqltest bash

Determine the relative path of MySQL files in Docker

 
 
# 查找Docker内,MySQL配置文件my.cnf的位置
mysql --help | grep my.cnf

# 若找不到 my.cnf,使用 whereis 命令查找相关配置路径
whereis mysql

Create a local path and mount the data in Docker

Execute first exitto exit the mysql container

Create a local folder, you can plan the path yourself

 
 
$ mkdir -p /root/docker/mysql/conf

Copy the MySQL configuration file in the test container to this path. If you need to change the configuration in the future, you can modify it directly on the configuration file of the mount path  

 
 
$ docker cp mysqltest:/etc/mysql/my.cnf /root/docker/mysql/conf

Delete the test container, create a new docker container and start

 
 
$ docker run -p 3306:3306 --name mysql \
-e MYSQL_ROOT_PASSWORD=root \
-v /root/docker/mysql/conf/my.cnf:/etc/mysql/my.cnf \
-d mysql:8 

Parameter Description:

 
 
- p 3306:3306 : 将容器的 3306 端口映射到主机的 3306 端口
- v /dockerData/mysql/conf:/etc/mysql : 将配置文件挂载到主机
- e MYSQL_ROOT_PASSWORD=root : 设置 mysql 登录密码
- d 后台运行容器,并返回容器 id
mysql:8 我运行的镜像名,也可替换成镜像 id

Check whether the startup is successful

We can use docker psthe command to see if the target container is running.

You can wait a while before running this command, as it takes a while for the Docker container to start.

The container will appear running during startup, but will stop when an error is encountered.

When we find that the container is not running and we need to troubleshoot, we can check the startup log of the container to find the error message.

 
 
# 其中 id 需要替换为容器启动后返回的容器 id
$ docke logs id

Fix it by looking at the error message and doing a web search.

Connect to Mysql database

We can connect to the Mysql server running on Docker through the database connection software on other computers.

For example, we can connect to the database through Navicat, and ensure that your Mysql container starts normally before connecting.

Error: Navicat does not support caching_sha_password encryption method

After Mysql 8 is installed, using Navicat to connect may report the error "navicat does not support the caching_sha_password encryption method". Solve it in the following way:

into the container

 
 
$ docker exec -it mysql bash

Login to Mysql

 
 
 $ mysql -uroot -p

View and select database

 
 
$ show databases;
$ use mysql

Modify the encryption method and exit Mysql and Mysql container

 
 
$ select host,user,plugin from user;
$ alter user 'root'@'%' identified with mysql_native_password by 'root';
$ exit;
$ exit;

Modify the configuration file in the container

If the relevant configuration file is not mounted when the container is created, you need to modify the configuration file inside the container.

Enter the container command:

 
 
$ docker exec -it 容器名/容器id bash

The Docker container does not have vim installed by default, it needs to be installed manually, just execute the following command according to the system.

Ubuntu system

 
 
$ apt-get update
$ apt install vim 

CentOS system

 
 
$ yum -y install vim*

Copy files between host and container

In practice, we often need to copy files between the host and the container.

Copy from host to container

 
 
$ docker cp 宿主机本地路径 容器名字/ID:容器路径

$ docker cp /root/123.cnf mysql:/etc/mysql

Copy from container to host

 
 
$ docker cp 容器名字/ID:容器路径 宿主机本地路径

$ docker cp mysql:/etc/mysql/my.cnf /root

If this article is helpful to you, please like and share~

Guess you like

Origin blog.csdn.net/2302_76827504/article/details/130631472