1.下载MysqlServer Docker Image
docker pull mysql/mysql-server:tag
tag为镜像标签,可以使5.5,5.6,5.7,8.0或latest,可以忽略;忽略则为lastest
2.运行MysqlServer Docker Image
docker run --name=mysql8 -d mysql/mysql-server:8.0
查看mysqlserver容器名称
docker ps
3.查看运行日志
docker logs mysql8
查看第一次运行Mysql生成的随机密码
docker logs mysql8 2>&1 | grep GENERATED
4.通过容器内客户端连接MysqlServer
docker exec -it mysql8 mysql -uroot -p
5.访问容器Shell
docker exec -it mysql8 bash
6.MysqlServer配置
docker run --name mysql8 -d mysql/mysql-server:tag --character-set-server=utf8mb4 --collation-server=utf8mb4_col
指定默认字符集为utf8mb4
查看MysqlServer默认数据存放路径
docker inspect mysql8
“Mounts”: [
{
“Type”: “volume”,
“Name”: “4f2d463cfc4bdd4baebcb098c97d7da3337195ed2c6572bc0b89f7e845d27652”,
“Source”: “/var/lib/docker/volumes/4f2d463cfc4bdd4baebcb098c97d7da3337195ed2c6572bc0b89f7e845d27652/_data”,
“Destination”: “/var/lib/mysql”,
“Driver”: “local”,
“Mode”: “”,
“RW”: true,
“Propagation”: “”
}
],
Source为host机器中存放数据的目录
指定配置文件目录和数据存放目录
docker run --name=mysql8 \
--mount type=bind,src=/path-on-host-machine/my.cnf,dst=/etc/my.cnf \
--mount type=bind,src=/path-on-host-machine/datadir,dst=/var/lib/mysql \
-d mysql/mysql-server:tag
指定配置文件路径:/path-on-host-machine/my.cnf
指定数据存放目录:/path-on-host-machine/datadir