我正在使用 PyCharm 连接到我在本地主机端口 3306 上运行的本地 MySQL 数据库。这是 PyCharm 在“数据源和驱动程序”窗口中显示的我的 JDBC url:
jdbc:mysql://localhost:3306/mydb
当我尝试连接或单击“测试连接”时,我在 IntelliJ 中收到此错误:
Error: Connection to MySQL failed.
Connection to Local MySQL failed.
[08S01] Communications link failure.
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
我可以很好地使用命令行客户端连接到 mysql,只是 PyCharm 无法正常工作。我做错了什么?
最佳答案
确保您的 MySQL 没有监听 IPv6 地址。由于某些原因,IntelliJ/PyCharm 在连接到在 IPv6 地址上运行的 MySQL 时出现问题。
要查看您的 MySQL 服务器正在监听的地址,请键入:
$ ss -ntl|grep 3306
LISTEN 0 80 [::1]:3306 [::]:*
[::1]:3306
部分表示它正在监听本地主机上的 IPv6 地址。
要更改为 IPv4,请打开 /etc/mysql/my.cnf
并在 [mysqld]
中添加/设置 bind-address
参数> 部分到 127.0.0.1
而不是 localhost
:
...
[mysqld]
...
port = 3306
bind-address = 127.0.0.1
...
然后重新启动 mysqld.service
,您将能够通过 IntelliJ/PyCharm 进行连接。
关于mysql - 智能/PyCharm : Connection to localhost MySQL failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48497143/