Table of contents
3. Stop the container and create a shell script file to run the container
4. Restart the container through the script
6. Subsequent operation and maintenance restart MYSQL container command
Today, I will share with you the installation of mysql8.0.27 under the Centos8 system. Please refer to the following steps to install mysql correctly using Docker.
1. Pull the mirror
#拉取最新的mysql版本
docker pull mysql
#查看mysql镜像
docker images mysql
output:
2. First start the container
Start the container to create the directory to be mounted by the host, and then copy the directory corresponding to the container to the host.
#运行mysql容器
docker run --name mysql8 -d -p 3306:3306 -e MYSQL_ROOT_PASSWORD=root mysql:8.0.27
#查看mysql日志,如果没有报错信息,然后进行下面的操作
docker logs mysql8
#进入mysql容器内部
docker exec -it mysql8 /bin/bash
#查看mysql的配置文件my.cnf
cat /etc/mysql/my.cnf
#退出容器
exit
#当前服务器创建挂在mysql配置文件
mkdir -p /usr/local/mysql8/conf /usr/local/mysql8/data
#文件夹授权
chmod -R 755 /usr/local/mysql8
#将容器中的my.cnf文件拷贝到服务器
docker cp 容器名:容器中my.cnf的路径/要拷贝到服务器的相应路径
docker cp mysql8:/etc/mysql/my.cnf /usr/local/mysql8/conf
The content of the modified my.cnf file is as follows:
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
character_set_server = utf8mb4
collation_server = utf8mb4_bin
3. Stop the container and create a shell script file to run the container
#停止容器
docker stop mysql8
#删除容器
docker rm mysql8
#进入服务器创建的mysql目录
cd /usr/local/mysql8
#创建mysql运行脚本
vi mysqlRun.sh
The content of the mysqlRun.sh script is as follows:
Set MYSQL configuration information, mount directory, open port
#!/bin/sh
docker run \
-p 3306:3306 \
--name mysql8 \
--privileged=true \
--restart unless-stopped \
-v /home/mysql8/conf/my.cnf:/etc/mysql/my.cnf \
-v /home/mysql8/logs:/logs \
-v /home/mysql8/data:/var/lib/mysql \
-v /etc/localtime:/etc/localtime \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8.0.27
4. Restart the container through the script
#执行脚本启动mysql容器
sh mysql8-docker-run.sh
#查看MySQL日志是否有报错
docker logs mysql8
5. Verify the effect
If no error is reported in the log, you can use the mysql client to test whether the link is ok.
To ensure that the default 3306 port of mysql can be accessed from the external network.
Verify that the mysql data is linked successfully
Create test database, create user table
The server MySQL data is shown in the figure:
#docker容器查看是否有对应的数据文件
docker exec -it mysql8 /bin/bash #进入容器内部
cd cd /var/lib/mysql # 进入docker的MySQL目录
ls #查看是否有test
cd test #进入test目录
ls #查看是否有 user.ibd 文件
6. Subsequent operation and maintenance restart MYSQL container command
#停止容器
docker stop <容器Id>
docker stop 572219d3a0ca
#重新启动
docker start <容器Id>
docker start 572219d3a0ca