时间:2021-07-01 10:21:17 帮助过:4人阅读
一、首先要安装docker
其他博文里有写过,这里步骤省略
二、建立镜像
1、下载官方镜像
docker pull mysql:5.7 # 拉取 mysql 5.7 docker pull mysql # 拉取最新版mysql镜像
2、检查是否拉取成功
docker images
3、新建挂载目录
mkdir wxl2020 mkdir -p ~/wlx2020/mysql/conf ~/wlx2020/mysql/data
4、启动容器
docker run -p 3309:3306 --name mysql2020 -v ~/wlx2020/mysql/conf:/etc/mysql/conf.d -v ~/wlx2020/mysql/logs:/logs -v ~/wlx2020/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
命令说明:
如果不需要做映射的话:
sudo docker run -p 3306:3306 --name mysql2020 -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7
5、检查容器是否正确运行
docker container ls
三、连接mysql
1、进入mysql2020容器操作mysql客户端
docker exec -it mysql bash
2、进入mysql应用,使用root用户操作mysql
mysql -uroot -p123456
看到mysql> 说明已经进入了mysql
进入mysql后所有指令结尾都要加;分号
3、使用Navicat连接mysql
Tips:
如果容器运行正常,但是无法访问到mysql,一般困难有一下几个原因
# 开放端口: $ systemctl status firewalld $ firewall-cmd --zone=public --add-port=3309/tcp -permanent $ firewall-cmd --reload # 关闭防火墙: $ systemctl stop firewalld
$ docker exec -it mysql bash $ mysql -uroot -p123456 mysql> grant all privileges on *.* to root@‘%‘ identified by "password";
原理
# mysql使用mysql数据库中的user表来管理权限,修改user表就可以修改权限(只有root账号可以修改) mysql> use mysql; Database changed mysql> select host,user,password from user; +--------------+------+-------------------------------------------+ | host | user | password | +--------------+------+-------------------------------------------+ | localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | | 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | +--------------+------+-------------------------------------------+ 2 rows in set (0.00 sec) mysql> grant all privileges on *.* to root@‘%‘ identified by "password"; Query OK, 0 rows affected (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.00 sec) mysql> select host,user,password from user; +--------------+------+-------------------------------------------+ | host | user | password | +--------------+------+-------------------------------------------+ | localhost | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | | 192.168.1.1 | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | | % | root | *A731AEBFB621E354CD41BAF207D884A609E81F5E | +--------------+------+-------------------------------------------+ 3 rows in set (0.00 sec)
参考链接:https://www.cnblogs.com/sablier/p/11605606.html
使用docker搭建MySQL服务
标签:原因 防火墙 status 远程 exec 官方 指令 主机名 html