安装mysql
# 拉取镜像
docker pull mysql:8.0.35
# 查看所有镜像
docker images
创建挂载目录
# 宿主机上创建挂载目录
mkdir -p /usr/local/software/docker/mysql/log
mkdir -p /usr/local/software/docker/mysql/data
mkdir -p /usr/local/software/docker/mysql/conf
创建并启动容器
docker run -itd --name mysql8 -p 3306:3306 -v /usr/local/software/docker/mysql/conf:/etc/mysql/conf.d -v /usr/local/software/docker/mysql/data:/var/lib/mysql -v /usr/local/software/docker/mysql/log:/var/log/mysql --restart unless-stopped -e MYSQL_ROOT_PASSWORD=gusy1234 mysql:8.0.35
命令解释:
–name mysql | 启动容器的名字 |
-p 3306:3306 | 将容器的 3306 (后面那个) 端口映射到主机的 3306 (前面那个) 端口 |
–restart unless-stopped | 容器重启策略 |
-v /usr/local/software/docker/mysql/conf:/etc/mysql | 将配置文件夹挂载到主机,语法:-v 宿主机目录:容器目录 |
-v /usr/local/software/docker/mysql/data:/var/lib/mysql | 将mysql储存文件夹挂载到主机 |
-v /usr/local/software/docker/mysql/log:/var/log/mysql | 将日志文件夹挂载到主机 |
-e MYSQL_ROOT_PASSWORD=gusy1234 | 设置root用户的密码 |
mysql:8.0.35 | 启动哪个版本的 mysql (本地镜像的版本) |
查看容器是否启动
docker ps