本文基于MySQL5.7.42进行说明
在 Docker 镜像仓库查找 MySQL 镜像
拉取MySQL镜像
常规命令(不适用),提示no matching manifest for linux/arm64/v8 in the manifest list entries
docker pull mysql:5.7.42
到DockerHub查看MySQL5.7.42的镜像,可以看到确实没有linux/arm64/v8版本的镜像
因此我们要另辟蹊径,加入指定架构的参数进行拉取
docker pull --platform linux/amd64 mysql:5.7.42
--platform linux/amd64:Docker拉取镜像时指定拉取所选架构的版本
查看镜像
在应用端的镜像界面和启动镜像的时候,Docker会检测到该镜像支持的架构与电脑的CPU架构不一致,忽略即可(M1/M2芯片也是支持linux/amd64版本的)
启动镜像并查看MySQL具体信息
# 启动镜像并进入容器
docker run -it mysql:5.7.42 /bin/bash
# 查看MySQL版本
mysql --version
# 查找 Docker 内,MySQL 配置文件 my.cnf 的位置
mysql --help | grep my.cnf
查看MySQL配置文件my.cnf内容(如下)
bash-4.2# cat my.cnf
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
skip-host-cache
skip-name-resolve
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
secure-file-priv=/var/lib/mysql-files
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
#log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
!includedir /etc/mysql/mysql.conf.d/
默认配置并未指定字符集,此时默认字符集为latin1,可能会导致一些中文字符乱码,因此我们可以修改配置文件,添加默认字符集(见下节)
运行镜像并配置持久化
准备
1. 宿主机创建数据存储目录(可以是其他位置,/Users/leo笔者Mac上的用户目录,等价于~目录,读者自行调整目录)
mkdir /Users/leo/docker/mysql
mkdir /Users/leo/docker/mysql/conf
mkdir /Users/leo/docker/mysql/data
mkdir /Users/leo/docker/mysql/logs
mkdir /Users/leo/docker/mysql/mysql-files
2. 新建my.cnf配置文件并放到/Users/leo/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/leo/docker/mysql/conf:/etc/mysql/conf.d \
-v /Users/leo/docker/mysql/logs:/var/log/mysql \
-v /Users/leo/docker/mysql/data:/var/lib/mysql \
-v /Users/leo/docker/mysql/mysql-files:/var/lib/mysql-files \
-d mysql:5.7.42
参数说明:
- docker run:运行Docker镜像
- –restart=always:当docker重启时,该容器也会重新启动
- -p:映射端口号,宿主机端口映射到容器内部端口(即访问宿主机的3306端口即可访问到容器的3306端口)
- –name mysql:设置容器名称为mysql
- -e MYSQL_ROOT_PASSWORD="123456":指定环境变量(初始化
root
用户的密码为123456
) - -v /Users/leo/docker/mysql/conf:/etc/mysql/conf.d:将宿主机的my.cnf文件映射到Docker容器中
- -v /Users/leo/docker/mysql/logs:/var/log/mysql:将容器的/var/log/mysql目录挂载到宿主机的/Users/leo/docker/mysql/logs目录
- -v /Users/leo/docker/mysql/data:/var/lib/mysql:同上
- -v /Users/leo/docker/mysql/mysql-files:/var/lib/mysql-files:同上
- -d mysql:5.7.42:选择运行的Docker镜像并指定Tag(不指定的话默认是latest)
测试
# 进入名为mysql的容器
docker exec -ti mysql bash
# 命令行连接MySQL
mysql -uroot -p'jY%kng8cc&'
# 查看字符集
show variables like 'char%';