在 Docker 镜像仓库查找 MySQL 镜像
DockerHub镜像仓库-mysql镜像 是 https://hub.docker.com/_/mysql/tags
拉取MySQL镜像
加入指定架构的参数进行拉取
docker pull --platform linux/amd64 mysql:8.0.39-debian
查看镜像
aaa@MacBook-Air air-master % docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest 6725a7dc7a44 2 months ago 196MB
mysql 8.0.39-debian f5217f5fd0ec 2 months ago 814MB
可以看到MySQL镜像的TAG是8.0.39-debian
启动镜像并查看MySQL具体信息
# 启动镜像并进入容器
docker run -it mysql:8.0.39-debian /bin/bash
# 查看MySQL版本
mysql --version
# 查找 Docker 内,MySQL 配置文件 my.cnf 的位置
mysql --help | grep my.cnf
查看my.cnf的内容
root@612a1d134a52:/# mysql --help | grep my.cnf
order of preference, my.cnf, $MYSQL_TCP_PORT,
/etc/my.cnf /etc/mysql/my.cnf ~/.my.cnf
root@612a1d134a52:/# cat /etc/mysql/my.cnf
[mysqld]
pid-file = /var/run/mysqld/mysqld.pid
socket = /var/run/mysqld/mysqld.sock
datadir = /var/lib/mysql
secure-file-priv= NULL
# Custom config should go here
!includedir /etc/mysql/conf.d/
运行镜像并配置持久化
1, 宿主机创建数据存储目录(可以是其他位置,/Users/tiger笔者Mac上的用户目录,等价于~目录,读者自行调整目录)
mkdir /Users/tiger/docker/mysql
mkdir /Users/tiger/docker/mysql/conf
mkdir /Users/tiger/docker/mysql/data
mkdir /Users/tiger/docker/mysql/logs
mkdir /Users/tiger/docker/mysql/mysql-files
2, 新建my.cnf配置文件并放到/Users/tiger/docker/mysql/conf目录下 my.cnf配置如下:
[client]
default_character_set=utf8
[mysql]
default_character_set=utf8
[mysqld]
character_set_server=utf8
运行容器
docker run --restart=always -p 3306:3306 --name mysql \
-e MYSQL_ROOT_PASSWORD="123456" \
-v /Users/tiger/docker/mysql/conf:/etc/mysql/conf.d \
-v /Users/tiger/docker/mysql/logs:/var/log/mysql \
-v /Users/tiger/docker/mysql/data:/var/lib/mysql \
-v /Users/tiger/docker/mysql/mysql-files:/var/lib/mysql-files \
-d mysql:8.0.39-debian
参数说明:
docker run:运行Docker镜像
–restart=always:当docker重启时,该容器也会重新启动
-p:映射端口号,宿主机端口映射到容器内部端口(即访问宿主机的3306端口即可访问到容器的3306端口)
–name mysql:设置容器名称为mysql
-e MYSQL_ROOT_PASSWORD="123456":指定环境变量(初始化 root 用户的密码为 123456)
-v /Users/tiger/docker/mysql/conf:/etc/mysql/conf.d:将宿主机的my.cnf文件映射到Docker容器中
-v /Users/tiger/docker/mysql/logs:/var/log/mysql:将容器的/var/log/mysql目录挂载到宿主机的/Users/tiger/docker/mysql/logs目录
-v /Users/tiger/docker/mysql/data:/var/lib/mysql:同上
-v /Users/tiger/docker/mysql/mysql-files:/var/lib/mysql-files:同上
-d mysql:8.0.39-debian:选择运行的Docker镜像并指定Tag(不指定的话默认是latest)
测试
# 进入名为mysql的容器
docker exec -ti mysql bash
# 命令行连接MySQL
mysql -uroot -p123456
# 查看字符集
show variables like 'char%';
查看字符集
mysql> show variables like 'char%';
+--------------------------+--------------------------------+
| Variable_name | Value |
+--------------------------+--------------------------------+
| character_set_client | utf8mb3 |
| character_set_connection | utf8mb3 |
| character_set_database | utf8mb3 |
| character_set_filesystem | binary |
| character_set_results | utf8mb3 |
| character_set_server | utf8mb3 |
| character_set_system | utf8mb3 |
| character_sets_dir | /usr/share/mysql-8.0/charsets/ |
+--------------------------+--------------------------------+
8 rows in set (0.05 sec)
连接 MYSQL
如果报错 “Public Key Retrieval is not allowed”,修改JDBC Driver的URL为
String database = "jdbc:mysql://localhost:3306/mysql?allowPublicKeyRetrieval=true&useSSL=false";