docker Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

更新日期: 2023-10-07 阅读次数: 848 字数: 145 分类: docker

通过宿主机的 mysql client 连接 docker 容器中的 mysql server 时报错:

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

解决方法

获取 mysql 容器名词:

$ docker container ls

将获取到名词替换掉下方命令中的 container_name

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' container_name

172.18.0.5

再用获取到的 IP 连接即可:

$ mysql -h 172.18.0.5 -P 3306 --protocol=tcp -u root -p
Enter password:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.34 MySQL Community Server - GPL

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

参考

https://www.baeldung.com/docker-cant-connect-local-mysql

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式