选择版本
![](https://img-blog.csdnimg.cn/img_convert/40cf1c0057e0f05e9b2f63d0af99f69a.png)
拉取镜像
docker pull mysql:8.0.30 #安装8.x版本
docker pull mysql:5.7.39 #安装的5.7.x版本
创建容器并启动
docker run -d -p 3306:3306 \
-v /data/mysql/8.0.30/conf:/etc/mysql/conf.d \
-v /data/mysql/8.0.30/data:/var/lib/mysql \
-v /data/mysql/8.0.30/log:/var/log/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
--name mysql mysql:8.0.30
配置端口映射
-p 3306:3306 --name mysql
将容器的3306端口映射到主机的3306端口
配置mysql数据卷挂载
1.-v /data/mysql/8.0.30/log:/var/log/mysql(日志文件挂载)
将容器中的日志文件夹/var/log/mysql挂载到主机对应的/data/mysql/8.0.30/log文件夹中
2.-v /data/mysql/8.0.30/data:/var/lib/mysql(数据文件挂载)
将容器中的数据文件夹/var/lib/mysql挂载到主机对应的/data/mysql/8.0.30/data文件夹中
3.-v /data/mysql/8.0.30/conf:/etc/mysql(配置文件挂载)
将容器的配置文件夹/etc/mysql挂载到主机对应的/data/mysql/8.0.30/conf文件夹中
注(这里所提的主机指的是当前的linux主机)
设置初始化root用户的密码为123456
-e MYSQL_ROOT_PASSWORD=123456
指定镜像资源
mysql:8.0.30:指定用这个镜像来创建运行实例
指定后台运行
-d:以后台方式运行实例
登录问题
![](https://img-blog.csdnimg.cn/img_convert/b6ea6d74b9abb1d00f43da4580f486d4.png)
MySQL 8.0.4 开始,MySQL 默认身份验证插件从 mysql_native_password 改为 caching_sha2_password ,但是客户端不支持caching_sha2_password这需要我们更改为mysql_native_password;但是也可以选择升级客户端,支持caching_sha2_password验证插件。
[root@agljc log]# docker exec -it c2fb4dc32f9d /bin/bash
bash-4.4# mysql
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
bash-4.4# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 8.0.30 MySQL Community Server - GPL
Copyright (c) 2000, 2022, 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>
![](https://img-blog.csdnimg.cn/img_convert/6f2bf5e7201acef4965cc78b3614d182.png)
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
![](https://img-blog.csdnimg.cn/img_convert/b8d799b3d18a79fbbecd54a1e78748d7.png)
连接成功
![](https://img-blog.csdnimg.cn/img_convert/f90a2f7d3389de95283f400f7109a315.png)