文章来源:https://www.cnblogs.com/hello-tl/p/9238298.html
1.首先安装docker
参照一下网址安装docker
docker:安装 https://www.cnblogs.com/hello-tl/p/8901132.html
参照安装一个mysql
docker:安装mysql https://www.cnblogs.com/hello-tl/p/9234429.html
2.创建文件
# mkdir /data # mkdir /data/mysql # mkdir /data/mysql/data5.6 # mkdir /data/mysql/data5.7 # mkdir /data/mysql/data8.0
3.添加镜像
# docker pull mysql:5.6 # docker pull mysql:5.7 # docker pull mysql:8.0
4.启动镜像
# docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql5.6 --restart always -v /data/mysql/data5.6:/var/lib/mysql -p 3306:3306 mysql:5.6 # docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql5.7 --restart always -v /data/mysql/data5.7:/var/lib/mysql -p 3307:3306 mysql:5.7 # docker run -d -e MYSQL_ROOT_PASSWORD=123456 --name mysql8.0 --restart always -v /data/mysql/data8.0:/var/lib/mysql -p 3308:3306 mysql:8.0
5.打开端口
//开启防火墙 # systemctl start firewalld //开启3306端口 # firewall-cmd --zone=public --add-port=3306/tcp --permanent //开启3307端口 # firewall-cmd --zone=public --add-port=3307/tcp --permanent //开启3308端口 # firewall-cmd --zone=public --add-port=3308/tcp --permanent //重启防火墙 # systemctl restart firewalld.service
6.本地连接测试
7.如果mysql是 8 版本以上应该会出一下错误
连接Docker启动的mysql出现:ERROR 2059 (HY000): Authentication plugin 'caching_sha2_password' cannot be loaded: ÕÒ²»µ½Ö¸¶¨µÄÄ£¿é¡£
解决方案
1.进入mysql容器
docker exec -it mysql /bin/bash
2.进入mysql
mysql -uroot -p123456
3.修改密码
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';