本文取自互联网以及自身实践,参开资料文末附上原文链接
01 拉取 Mysql 镜像
可以先查看 mysql 的相关镜像:
docker search mysql
这里官方的镜像进行下载:
docker pull mysql
之后 Docker 会自动下载 Mysql 镜像:
注意,若提示拉取失败就重复几次,总有一次会成功的,我在拉取这个时尝试了 5 次不止0.0
拉取成功后我们查看一下:
docker images
02 创建并启动一个 Mysql 容器
输入以下命令:
docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql
–name:给新创建的容器命名,此处命名为 mysqlserver
-e:配置信息,此处配置 mysql 的 root 用户的登陆密码
-p:端口映射,表示在这个容器中使用 3306 端口(第二个)映射到本机的端口号也为 3306 (第一个)
-d:成功启动容器后输出容器的完整 ID
最后一个 mysql 指的是 mysql 镜像名字
到这里我们查看容器运行状态:
docker ps
上图可以看到容器的简写ID,容器的源镜像,创建时间,状态,端口映射信息,容器名字等。
如果要设置 mysql 自动启动,加上 --restart=always
其中,restart 的参数有以下几个:
Flag Description
no 不自动重启容器. (默认 value)
on-failure 容器发生 error 而退出(容器退出状态不为 0 )重启容器
unless-stopped 在容器已经 stop 掉或 Docker stoped/restarted 的时候才重启容器
always 在容器已经 stop 掉或 Docker stoped/restarted 的时候才重启容器
03 测试连接 Mysql
1、进入 容器
docker exec -it mysqlserver /bin/bash
docker exec :在运行的容器中执行命令
语法
docker exec [OPTIONS] CONTAINER COMMAND [ARG…]
OPTIONS说明:
-d :分离模式: 在后台运行
-i :即使没有附加也保持STDIN 打开
-t :分配一个伪终端
进来伪终端之后,可以设置 Mysql 的一些东西。
mysql -h 127.0.0.1 -uroot -p
然后输入上文启动 docker 容器输入的密码:123456
进入 Mysql 终端,把 mysql 用户登录密码加密规则还原成 mysql_native_password
ALTER USER 'root'@'%' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; #修改加密规则 ,'password'改成你的密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password'; #更新一下用户的密码 ,'password'是你的密码
FLUSH PRIVILEGES; #刷新权限
2、这里使用 navicat 远程连接,连接 MySQL 前需要防火墙开放端口或者关闭防火墙。
开放端口:
firewall-cmd --add-port=3306/tcp
关闭防火墙:
systemctl stop firewalld
04 其他
1、可以启动多个MySQL服务,因为我们启动的是容器,容器可以有多个,只要容器名字映射段端口不一样就可以了,例如:
docker run --name mysqlserver2 -e MYSQL_ROOT_PASSWORD=123456 -p 3307:3306 -d mysql
下图就是把mysqlserver2的3306绑定到了本机上的3307,使用navicat访问的时候只要填写成3307即可访问。
2、查看所有容器(启动状态或者关闭状态)
docker ps -a
3、启动和关闭容器
启动命令:
docker start mysqlserver //通过指定容器名字sudo docker start 73f8811f669e //通过指定容器ID
关闭命令:
docker stop mysqlserver //通过指定容器名字
docker stop 73f8811f669e //通过指定容器ID
4、修改MySQL配置文件有两种方法:
一 是进入容器,修改容器里的MySQL的配置文件,然后重新启动容器,例如:
docker exec -it mysqlserver /usr/bin/bash
然后可以进入容器的命令行模式,接着修改 /etc/mysql/my.cnf 文件即可
二 是挂载主机的mysql配置文件,官方文档如下:
The MySQL startup configuration is specified in the file /etc/mysql/my.cnf, and that file in turn includes any files found in the /etc/mysql/conf.d directory that end with .cnf. Settings in files in this directory will augment and/or override settings in /etc/mysql/my.cnf. If you want to use a customized MySQL configuration, you can create your alternative configuration file in a directory on the host machine and then mount that directory location as /etc/mysql/conf.d inside the mysql container.
If /my/custom/config-file.cnf is the path and name of your custom configuration file, you can start your mysql container like this (note that only the directory path of the custom config file is used in this command):
docker run --name some-mysql -v /my/custom:/etc/mysql/conf.d -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql:tag
This will start a new container some-mysql where the MySQL instance uses the combined startup settings from /etc/mysql/my.cnf and /etc/mysql/conf.d/config-file.cnf, with settings from the latter taking precedence.
参考资料:
- https://www.cnblogs.com/pwc1996/p/5425234.html