1.在Mac中安装 docker 的mysql 镜像。
docker pull mysql/mysql-server docker run --name mysql01 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 -d mysql/mysql-server
2 .通过客户端通过客户端访问docker安装的mysql ,连接失败
(1)进入镜像mysql(ti 后面的字符串是mysql镜像ID)
docker exec -ti 2cbb0f246353 /bin/bash
(2)登录mysql
mysql -u root -p
(3)修改root 可通过任何客户端连接
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';
3、更新mysql中root用户的连接host
use mysql; update user set host = '%' where user = 'root' and host='localhost'; grant all privileges on *.* to 'root'@'%' with grant option; 或者 GRANT ALL ON *.* TO 'root'@'%'; flush privileges;
4.查看mysql容器ip
docker inspect --format '{
{ .NetworkSettings.IPAddress }}' <container-ID> docker inspect --format '{
{ .NetworkSettings.IPAddress }}' <容器名称>
注:在这里找到的ip是docker的内网ip
5.连接docker中的mysql
可直接使用127.0.0.1连接
konsy@Konsy-MacBook-Pro ~ % mysql -uroot -p -h 127.0.0.1 Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 40 Server version: 8.0.27 MySQL Community Server - GPL
Copyright (c) 2000, 2021, 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>