为什么在使用 Java (Netbeans) 连接到 MySQL 时会收到此错误“通信?链接?失败?最后?数据包?已发送...”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10567064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why I get this error 'Communications?link?failure The?last?packet?sent...' when connecting to MySQL with Java (Netbeans)?
提问by Patryk
I am trying to use MySQL database (that is installed on virtual machine (Debian) on my pc and has IP address 192.168.1.5
) from Java with use of NetBeans.
我正在尝试192.168.1.5
使用 NetBeans 从 Java 中使用 MySQL 数据库(安装在我电脑上的虚拟机(Debian)上并具有 IP 地址)。
I have configured the connection as follows :
我已将连接配置如下:
Driver Name MySQL(Connector/J Driver)
Host 192.168.1.5
Database test
Username root
Password *
JDBC URL jdbc:mysql://192.168.1.5:3306/test
and then I get the following error :
然后我收到以下错误:
cannot establish?a?connection?to?jdbc:mysql://192.168.1.5:3306/test?using?
com.mysql.jdbc.Driver?(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.)
My mysql.user
table looks like this ( I know % root
is not very secure but this is just to simplify things for the moment):
我的mysql.user
桌子看起来像这样(我知道% root
这不是很安全,但这只是为了暂时简化事情):
+------------+------------------+
| host | user |
+------------+------------------+
| % | root |
| 127.0.0.1 | root |
| ::1 | root |
| debVirtual | |
| localhost | |
| localhost | debian-sys-maint |
| localhost | phpmyadmin |
| localhost | root |
+------------+------------------+
What can I do to allow this connection ?
我该怎么做才能允许这种连接?
回答by Patryk
The problem was with mysql configuration file on the server /etc/mysql/my.cnf
问题出在服务器上的 mysql 配置文件上 /etc/mysql/my.cnf
the line : bind-address
should point to your server's IP like in this example
该行:bind-address
应该指向您服务器的 IP,如本例所示
bind-address = 192.168.1.5
bind-address = 192.168.1.5
and not
并不是
bind-address = 127.0.0.1
bind-address = 127.0.0.1
to allow remote access.
允许远程访问。
回答by user1391476
Can you post your entire code?
你能发布你的整个代码吗?
That error comes when the database youre trying to reach does not exist. Check your network setting in your VM software. What type of VM software are you using (VirtualBox, VM ware...)
当您尝试访问的数据库不存在时,就会出现该错误。检查 VM 软件中的网络设置。您使用的是什么类型的 VM 软件(VirtualBox、VM ware...)
回答by Ka?ss Bouali
This is either the wrong folder, or I have a different version. I am running Lubuntu and the file is not my.cnf (because that file does not have a bind-address). the bind-address is situated in a file in /etc/mysql/mysql.conf.d/
whose name is: mysqld.cnf
. You cannot modify it by the file manager so you have to go through the following command lines:
这要么是错误的文件夹,要么是我的版本不同。我正在运行 Lubuntu,但该文件不是 my.cnf(因为该文件没有绑定地址)。绑定地址位于/etc/mysql/mysql.conf.d/
名称为:的文件中mysqld.cnf
。您无法通过文件管理器修改它,因此您必须通过以下命令行:
sudo su
***enter password***
cd /etc/mysql/mysql.conf.d
nano mysqld.cnf
then go to bind-address 127.0.0.1 and change it to the IPv4 that you can find in
然后转到绑定地址 127.0.0.1 并将其更改为您可以在其中找到的 IPv4
ifconfig
I hope I helped.
我希望我有所帮助。
回答by Halefom
Perfect solution:
完美解决方案:
- Start wamp server before netbeans
- Then open netbeans
- 在 netbeans 之前启动 wamp 服务器
- 然后打开netbeans
It worked for me. When I close the wamp server, it shows me this error:
它对我有用。当我关闭 wamp 服务器时,它显示了这个错误: