查看需要下载的mysql版本
目录结构及my.cnf
mysql5.7
conf
my.cnf
logs
data
my.cnf
[client]
# 设置mysql客户端连接服务端时默认字符集
default-character-set=utf8mb4
[mysql]
#设置mysql客户端默认字符集
default-character-set=utf8mb4
[mysqld]
sql-mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
#服务端使用的字符集默认为utf8mb4
character-set-server=utf8mb4
#允许最大连接数
max_connections=1000
#允许连接失败的次数
max_connect_errors=10
#创建新表时将使用的默认存储引擎
default-storage-engine=INNODB
拉取mysql5.7镜像
[root@VM-24-14-centos mysql]# docker pull mysql:5.7
5.7: Pulling from library/mysql
72a69066d2fe: Pull complete
93619dbc5b36: Pull complete
99da31dd6142: Pull complete
626033c43d70: Pull complete
37d5d7efb64e: Pull complete
ac563158d721: Pull complete
d2ba16033dad: Pull complete
0ceb82207cd7: Pull complete
37f2405cae96: Pull complete
e2482e017e53: Pull complete
70deed891d42: Pull complete
Digest: sha256:f2ad209efe9c67104167fc609cca6973c8422939491c9345270175a300419f94
Status: Downloaded newer image for mysql:5.7
docker.io/library/mysql:5.7
启动镜像
docker run --restart=always -p 3306:3306 --name my-mysql
-v /usr/local/soft/mysql/conf/my.cnf:/etc/mysql/my.cnf
-v /usr/local/soft/mysql/logs:/var/log/mysql
-v /usr/local/soft/mysql/data:/var/lib/mysql
-e MYSQL_ROOT_PASSWORD=admin123456 -d mysql:5.7
详解:
--restart=always 当docker重启时,容器也重启
--name 容器命名
-p 3306:3306 宿主机和容器端口映射
-v(此命令中有三个-v参数) 挂载数据等内容,避免容器重启造成的数据丢失
-e 设置环境变量(此处设置了mysql的密码)
-d 后台启动(容器不会因为shell的退出而停止运行)
进入容器
[root@VM-24-14-centos mysql]# docker exec -it 564d75aaa7f8 /bin/bash
root@564d75aaa7f8:/# mysql -uroot -padmin123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
mysql> show databases;