windows 和 mac 版可以直接到官网下载 docker desktop
linux 的安装方法可以参考 https://www.cnblogs.com/myzony/p/9071210.html
可以在shell中输入以下命令检查是否成功安装: sudo docker version
1 2 |
docker pull mysql:5.7 # 拉取 mysql 5.7 docker pull mysql # 拉取最新版mysql镜像 |
1 |
sudo docker images |
1 |
sudo docker run -p 3306:3306 --name mysql -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.7 |
mysql
1 2 3 4 5 6 |
sudo docker run -p 3306:3306 --name mysql \ -v /usr/local/docker/mysql/conf:/etc/mysql \ -v /usr/local/docker/mysql/logs:/var/log/mysql \ -v /usr/local/docker/mysql/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123456 \ -d mysql:5.7 |
1 |
docker container ls |
1 2 |
sudo docker exec -it mysql bash mysql -uroot -p123456 |
我们在创建容器的时候已经将容器的3306端口和主机的3306端口映射到一起,所以我们应该访问:
1 2 3 4 |
host: 127.0.0.1 port: 3306 user: root password: 123456 |
1 2 3 4 5 6 |
# 开放端口: $ systemctl status firewalld $ firewall-cmd --zone=public --add-port=3306/tcp -permanent $ firewall-cmd --reload # 关闭防火墙: $ sudo systemctl stop firewalld |
1 2 3 |
$ sudo docker exec -it mysql bash $ mysql -uroot -p123456 mysql> grant all privileges on *.* to root@'%' identified by "password"; |
原理:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# 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://blog.csdn.net/jor_ivy/article/details/81323199
https://www.52pojie.cn/thread-727433-1-1.html
from:https://www.cnblogs.com/sablier/p/11605606.html