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
另一种映射本地文件启动的方式(黑马程序员docker教程)
docker run -id -p 3306:3306 --name=mysql -v $PWD/conf:/etc/mysql/conf.d -v $PWD/logs:/logs -v $PWD/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:latest
2 .在虚拟机外面通过客户端访问docker安装的mysql ,连接失败
(1)进入镜像中的mysql(ti 后面的字符串是mysql镜像启动的容器ID或者用容器名字)
docker exec -ti 2cbb0f246353 /bin/bash
docker exec -it mysql /bin/bash
(2)登录mysql
mysql -u root -p
mysql -u root -p123456 密码明文登陆
(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 -- 直接用这个ip访问好像不通,待解决,用127.0.0.1就可以
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>