1.docker hub 镜像仓库官网

仓库官网地址:https://hub.docker.com/search?q=mysql&type=image

2.拉取镜像

docker pull mysql:8.0.22

3.docker启动mysql容器

1)数据卷挂载

mkdir -p /usr/local/docker/mysql/data
mkdir -p /usr/local/docker/mysql/mysql-files
mkdir -p /usr/local/docker/mysql/cnf
mkdir -p /usr/local/docker/mysql/log

2)编写my.cnf文件

#Apply this config only on the master.
[client]
port = 3306
default-character-set = utf8mb4

[mysql]
port = 3306
default-character-set = utf8mb4

[mysqld]
# bind-address = 0.0.0.0
# port = 3306

max_connections=10000

character-set-server = utf8mb4
collation-server = utf8mb4_unicode_ci

# 设置时区和字符集
# default-time-zone='+8:00'
character-set-client-handshake=FALSE
init_connect='SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci'

gtid-mode=ON
enforce-gtid-consistency = ON

3)启动mysql容器,使用docker run

docker run  \
--restart=always \
--name mysql \
-v /usr/local/docker/mysql/cnf:/etc/mysql \
-v /usr/local/docker/mysql/data:/var/lib/mysql \
-v /usr/local/docker/mysql/log:/var/log \
-v /usr/local/docker/mysql/mysql-files:/var/lib/mysql-files \
-p 3306:3306 \
-e MYSQL_ROOT_PASSWORD=123456 \
-d mysql:8.0.22

–restart=always #容器在docker启动的时候,也会跟着启动

–name mysql #给容器取名

-v /usr/local/docker/mysql/data:/var/lib/mysql #挂载目录,就是把容器的目录挂载到外面

-p 3306:3306 #端口映射

-e MYSQL_ROOT_PASSWORD=root #mysql初始密码

-d #后台运行


4)查看启动是否成功

docker ps -a

docker exec -it mysql bash