MySQL 是世界上主流的开源数据库之一。
- 拉取 MySQL 8 镜像。
docker pull mysql:8.0.33
8.0.33: Pulling from library/mysql
46ef68baacb7: Pull complete
94c1114b2e9c: Pull complete
ff05e3f38802: Pull complete
41cc3fcd9912: Pull complete
07bbc8bdf52a: Pull complete
6d88f83726a9: Pull complete
cf5c7d5d33f7: Pull complete
9db3175a2a66: Pull complete
feaedeb27fa9: Pull complete
cf91e7784414: Pull complete
b1770db1c329: Pull complete
Digest: sha256:15f069202c46cf861ce429423ae3f8dfa6423306fbf399eaef36094ce30dd75c
Status: Downloaded newer image for mysql:8.0.33
docker.io/library/mysql:8.0.33
- 运行 MySQL 服务器容器。
docker run --network topeid-network --name mysql8-temp --hostname mysql8-temp -e MYSQL_ROOT_PASSWORD=secret -d mysql:8.0.33
e91b337be8590c159e7cc34b4feeab7cd6697abe395a800a195119bbf3636eec
- 查看 MySQL 容器。
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e91b337be859 mysql:8.0.33 "docker-entrypoint.s…" 53 seconds ago Up 51 seconds 3306/tcp, 33060/tcp mysql8-temp
- 访问容器并登录数据库。
$ docker exec -it mysql8-temp bash
bash-4.4# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, 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>
- 从另一个容器登录数据库。
docker run --network topeid-network -it --rm mysql:8.0.33 mysql -hmysql8-temp -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.33 MySQL Community Server - GPL
Copyright (c) 2000, 2023, 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日志。
docker logs mysql8-temp
2023-06-18 09:57:47+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
2023-06-18 09:57:47+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
2023-06-18 09:57:47+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.33-1.el8 started.
2023-06-18 09:57:48+00:00 [Note] [Entrypoint]: Initializing database files
...
2023-06-18T09:58:29.589330Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: '::' port: 33060, socket: /var/run/mysqld/mysqlx.sock
2023-06-18T09:58:29.589596Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.33' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
- 查看MySQL配置文件。
$ docker exec -it mysql8-temp bash
sh-4.4# ls -l /etc/ | grep my
-rw-r--r-- 1 root root 1317 Jun 16 00:38 my.cnf
drwxr-xr-x 2 root root 4096 Mar 16 20:35 my.cnf.d
drwxr-xr-x 3 root root 4096 Jun 16 00:38 mysql
sh-4.4# cat /etc/my.cnf
# 省略注释部分内容
[mysqld]
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
pid-file=/var/run/mysqld/mysqld.pid
[client]
socket=/var/run/mysqld/mysqld.sock
!includedir /etc/mysql/conf.d/
使用自定义的配置文件。假设配置文件位于 /your/path/custom.cnf.
docker run --network topeid-network --name mysql8-temp --hostname mysql8-temp -v /your/path:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=secret -d mysql:8.0.33
- 使用MySQL命令行参数。
docker run --network topeid-network --name mysql8-temp --hostname mysql8-temp -e MYSQL_ROOT_PASSWORD=secret -d mysql:8.0.33 --character-set-server=utf8mb4 --collation-server=utf8mb4_zh_0900_as_cs
注意:--character-set-server=utf8mb4 和 --collation-server=utf8mb4_zh_0900_as_cs 两个参数要放到命令的最后,因为这两个参数是传递给 MySQL 的,而不是传递给 Docker 的。
- 挂载配置目录,日志目录和数据目录。
在 macOS 或 Linux 系统中, 执行下列命令。
docker run --detach --name mysql8-temp \
--network topeid-network \
-v $PWD/data:/var/lib/mysql \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=secret \
-p 3306:3306 \
-e TZ="Asia/Shanghai" \
mysql:8.0.33 \
--character-set-server=utf8mb4 \
--collation-server=utf8mb4_zh_0900_as_cs
在 Windows 的命令行(cmd.exe)中,执行下列命令。
docker run --detach --name mysql8-temp ^
--network topeid-network ^
-v %CD%/data:/var/lib/mysql ^
-v %CD%/conf:/etc/mysql/conf.d ^
-v %CD%/logs:/var/log/mysql ^
-e MYSQL_ROOT_PASSWORD=secret ^
-p 3306:3306 ^
-e TZ="Asia/Shanghai" ^
mysql:8.0.33 ^
--character-set-server=utf8mb4 ^
--collation-server=utf8mb4_zh_0900_as_cs
参考文献
致谢
- 封面图片来自:unsplash.com