当前可选择的版本以及 Dockerfile
的地址
-
8.0.32
,8.0
,8
,latest
,8.0.32-oracle
,8.0-oracle
,8-oracle
,oracle
-
8.0.32-debian
,8.0-debian
,8-debian
,debian
-
5.7.41
,5.7
,5
,5.7.41-oracle
,5.7-oracle
,5-oracle
-
5.7.41-debian
,5.7-debian
,5-debian
mysql镜像的使用
启动一个 mysql
服务的实例
Starting a MySQL instance is simple:
1 | docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag |
… some-mysql
是容器的名称, my-secret-pw
是root密码 tag
mysql的版本.
连接到mysql实例
用下面的方式连接刚刚启动的mysql其中网络要与之前mysql实例的网络为相同的网络空间 --rm
表示使用完成即可删除客户端容器
1 | docker run -it --network some-network --rm mysql mysql -hsome-mysql -uexample-user -p |
这个客户端可以连接任何MySQL服务器,如果我们只是需要连接mysql数据库没有客户端工具可以使用下面的方式来实现
1 | docker run -it --rm mysql mysql -hsome.mysql.host -usome-mysql-user -p |
MySQL的用法可以参考mysql的官方文档 MySQL documentation
… 也可以通过 docker stack deploy
或者 docker-compose
启动mysql服务
如stack.yml
:
1 | # Use root/example as user/password credentials |
可以通过 docker stack deploy -c stack.yml mysql
(或者 docker-compose -f stack.yml up
)启动服务 adminer是个前端程序等待容器全部都启动完成后可以访问http://swarm-ip:8080
, http://localhost:8080
, 或者 http://host-ip:8080
验证.
进入容器
通过 docker exec
可以进入容器
1 | docker exec -it some-mysql bash |
查看容器日志
1 | docker logs some-mysql |
自定义mysql的配置文件
容器中的默认配置文件为/etc/mysql/my.cnf
, 主要是通过 !includedir
载其他配置文件 /etc/mysql/conf.d
or /etc/mysql/mysql.conf.d
.
如果配置文件为 /my/custom/config-file.cnf
可以用下面的方式启动mysql并指定自己的配置文件
1 | docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag |
通过镜像变量来指定配置
有很多参数可以通过容器的启动参数来指定,比如:数据库的字符集
1 | docker run --name some-mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci |
如果想查看全部的参数列表可以通过下面的方式
1 | docker run -it --rm mysql:tag --verbose --help |
容器也可以指定很多环境变量
在容器启动的时候可以指定环境变量,但是如果启动的时候已经挂在了一个已存在的数据库路径,那么环境变量不会生效,比如root用户的密码
MYSQL_ROOT_PASSWORD
指定 root
用户的密码. 前面的例子中root密码设置成了 my-secret-pw
.
MYSQL_DATABASE
This variable is optional and allows you to specify the name of a database to be created on image startup. If a user/password was supplied (see below) then that user will be granted superuser access (corresponding to GRANT ALL
) to this database.
MYSQL_USER
, MYSQL_PASSWORD
These variables are optional, used in conjunction to create a new user and to set that user’s password. This user will be granted superuser permissions (see above) for the database specified by the MYSQL_DATABASE
variable. Both variables are required for a user to be created.
Do note that there is no need to use this mechanism to create the root superuser, that user gets created by default with the password specified by the MYSQL_ROOT_PASSWORD
variable.
MYSQL_ALLOW_EMPTY_PASSWORD
This is an optional variable. Set to a non-empty value, like yes
, to allow the container to be started with a blank password for the root user. NOTE: Setting this variable to yes
is not recommended unless you really know what you are doing, since this will leave your MySQL instance completely unprotected, allowing anyone to gain complete superuser access.
MYSQL_RANDOM_ROOT_PASSWORD
This is an optional variable. Set to a non-empty value, like yes
, to generate a random initial password for the root user (using pwgen
). The generated root password will be printed to stdout (GENERATED ROOT PASSWORD: .....
).
MYSQL_ONETIME_PASSWORD
Sets root (not the user specified in MYSQL_USER
!) user as expired once init is complete, forcing a password change on first login. Any non-empty value will activate this setting. NOTE: This feature is supported on MySQL 5.6+ only. Using this option on MySQL 5.5 will throw an appropriate error during initialization.
MYSQL_INITDB_SKIP_TZINFO
By default, the entrypoint script automatically loads the timezone data needed for the CONVERT_TZ()
function. If it is not needed, any non-empty value disables timezone loading.
Docker Secrets
As an alternative to passing sensitive information via environment variables, _FILE
may be appended to the previously listed environment variables, causing the initialization script to load the values for those variables from files present in the container. In particular, this can be used to load passwords from Docker secrets stored in /run/secrets/<secret_name>
files. For example:
1 | docker run --name some-mysql -e MYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql-root -d mysql:tag |
Currently, this is only supported for MYSQL_ROOT_PASSWORD
, MYSQL_ROOT_HOST
, MYSQL_DATABASE
, MYSQL_USER
, and MYSQL_PASSWORD
.
Initializing a fresh instance
When a container is started for the first time, a new database with the specified name will be created and initialized with the provided configuration variables. Furthermore, it will execute files with extensions .sh
, .sql
and .sql.gz
that are found in /docker-entrypoint-initdb.d
. Files will be executed in alphabetical order. You can easily populate your mysql
services by mounting a SQL dump into that directory and provide custom images with contributed data. SQL files will be imported by default to the database specified by the MYSQL_DATABASE
variable.
数据存储
指定数据存储路径
- 创建一个路径在本地如:
/my/own/datadir
. - 启动mysql容器实例是挂在目录
1
docker run --name some-mysql -v /my/own/datadir:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
指定用户运行
可以通过 –user选项指定容器内运行的用户
1 | mkdir data |
创建数据库备份
1 | docker exec some-mysql sh -c 'exec mysqldump --all-databases -uroot -p"$MYSQL_ROOT_PASSWORD"' > /some/path/on/your/host/all-databases.sql |
数据恢复
通过 docker exec
加 -i
选项恢复数据
1 | docker exec -i some-mysql sh -c 'exec mysql -uroot -p"$MYSQL_ROOT_PASSWORD"' < /some/path/on/your/host/all-databases.sql |