Java com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2121829/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:46:30  来源:igfitidea点击:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:Communications link failure

javamysqljdbc

提问by Nick Heiner

My program that connects to a MySQL database was working fine. Then, without changing any code used to set up the connection, I get this exception:

我连接到 MySQL 数据库的程序运行良好。然后,在不更改用于设置连接的任何代码的情况下,我收到此异常:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 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.

What happened?

发生了什么?

The code used to get the connection:

用于获取连接的代码:

private static Connection getDBConnection() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException {
    String username = "user";
    String password = "pass";
    String url = "jdbc:mysql://www.domain.com:3306/dbName?connectTimeout=3000";

    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection(url, username, password);
    return conn;
}

回答by BalusC

This is a wrapped exception and not really interesting. It is the root causeof the exception which actually tells us something about the root cause. Please look a bit further in the stacktrace. The chance is big that you'll then face a SQLException: Connection refusedor SQLException: Connection timed out.

这是一个包装的异常,并不是很有趣。异常的根本原因实际上告诉我们一些关于根本原因的信息。请在堆栈跟踪中进一步查看。然后您将面临 aSQLException: Connection refused或的可能性很大SQLException: Connection timed out

If this is true in your case as well, then all the possible causes are:

如果您的情况也是如此,那么所有可能的原因是:

  1. IP address or hostname in JDBC URL is wrong.
  2. Hostname in JDBC URL is not recognized by local DNS server.
  3. Port number is missing or wrong in JDBC URL.
  4. DB server is down.
  5. DB server doesn't accept TCP/IP connections.
  6. Something in between Java and DB is blocking connections, e.g. a firewall or proxy.
  1. JDBC URL 中的 IP 地址或主机名错误。
  2. 本地 DNS 服务器无法识别 JDBC URL 中的主机名。
  3. JDBC URL 中的端口号丢失或错误。
  4. 数据库服务器已关闭。
  5. 数据库服务器不接受 TCP/IP 连接。
  6. Java 和 DB 之间的某些东西正在阻止连接,例如防火墙或代理。

To solve the one or the either, follow the following advices:

要解决一个或两个,请遵循以下建议:

  1. Verify and test them with ping.
  2. Refresh DNS or use IP address in JDBC URL instead.
  3. Verify it based on my.cnfof MySQL DB.
  4. Start it.
  5. Verify if mysqld is started without the --skip-networkingoption.
  6. Disable firewall and/or configure firewall/proxy to allow/forward the port.
  1. 验证并测试它们ping
  2. 刷新 DNS 或改用 JDBC URL 中的 IP 地址。
  3. 基于my.cnfMySQL DB 进行验证。
  4. 启动它。
  5. 验证是否在没有该--skip-networking选项的情况下启动了 mysqld 。
  6. 禁用防火墙和/或配置防火墙/代理以允许/转发端口。

By the way (and unrelated to the actual problem), you don't necessarily need to load the JDBC driver on everygetConnection()call. Just only once during startup is enough.

顺便说一句(与实际问题无关),您不一定需要在每次getConnection()调用时加载 JDBC 驱动程序。在启动期间只需一次就足够了。

回答by MBCook

We have a piece of software (webapp with Tomcat) using Apache commons connection pooling, and worked great for years. In the last month I had to update the libraries due to an old bug we were encountering. The bug had been fixed in a recent version.

我们有一个使用 Apache 公共连接池的软件(带有 Tomcat 的 webapp),并且多年来运行良好。在上个月,由于我们遇到了一个旧错误,我不得不更新库。该错误已在最近的版本中修复。

Shortly after deploying this, we started getting exactly these messages. Out of the thousands of connections we'd get a day, a handful (under 10, usually) would get this error message. There was no real pattern, except they would sometimes cluster in little groups of 2 to 5.

部署后不久,我们就开始收到这些消息。在我们每天获得的数千个连接中,少数(通常少于 10 个)会收到此错误消息。没有真正的模式,除了它们有时会聚集成 2 到 5 个小群。

I changed the options to on the pool to validate the connection every time one is taken from or put back in the pool (if one is found bad, a new one is generated instead) and the problem went away.

我将选项更改为 on the pool 以在每次从池中取出或放回池时验证连接(如果发现一个坏的,则会生成一个新的),问题就消失了。

Have you updated your MySQL jar lately? It seems like there may be a new setting that didn't used to be there in our (admittedly very old) jar.

你最近更新了你的 MySQL jar 吗?似乎在我们的(不可否认非常旧的)罐子里可能有一个新的设置。

I agree with BalusC to try some other options on your config, such as those you're passing to MySQL (in addition to the connection timeout).

我同意 BalusC 在您的配置中尝试一些其他选项,例如您传递给 MySQL 的那些选项(除了连接超时)。

If this failure is transient like mine was, instead of permanent, then you could use a simple try/catch and a loop to keep trying until things succeed or use a connection pool to handle that detail for you.

如果这个失败像我的一样是暂时的,而不是永久的,那么你可以使用一个简单的 try/catch 和一个循环来继续尝试直到事情成功或使用连接池来为你处理那个细节。

Other random idea: I don't know what happens why you try to use a closed connection (which exception you get). Could you be accidentally closing the connection somewhere?

其他随机想法:我不知道为什么您尝试使用关闭的连接(您得到哪个异常)会发生什么。你会不会在某个地方不小心关闭了连接?

回答by Pascal Thivent

As BalusC mentioned, it would be very useful to post the fullstacktrace (always post a full stacktrace, it is useless and frustrating to have only the first lines of a stacktrace).

正如 BalusC 所提到的,发布完整的堆栈跟踪非常有用(总是发布完整的堆栈跟踪,只有堆栈跟踪的第一行是无用的和令人沮丧的)。

Anyway, you mentioned that your code was workingfine and that this problem started suddenly to occur without any code change so I'm wondering if this could be related to you other question Problem with not closing db connection while debugging?Actually, if this problem started while debugging, then I think it is (you ran out of connections). In that case, restart you database server (and follow the suggestions of the other question to avoid this situation).

无论如何,您提到您的代码运行良好,并且这个问题在没有任何代码更改的情况下突然发生,所以我想知道这是否与您有关其他问题调试时不关闭数据库连接的问题?实际上,如果这个问题是在调试时开始的,那么我认为是(你的连接用完了)。在这种情况下,请重新启动数据库服务器(并按照其他问题的建议避免这种情况)。

回答by Jorge Omar Vázquez

I've been having this issue also for about 8-9 days. Here's some background: I'm developing a simple Java application that runs in bash.

我也遇到这个问题大约 8-9 天了。这是一些背景知识:我正在开发一个在 bash 中运行的简单 Java 应用程序。

Details:

细节:

  • Spring 2.5.6
  • Hibernate3.2.3.ga
  • With maven. (The base of the project is from mkyong.com , the spring tutorial without anotations )
  • MySQL version:
  • 春天 2.5.6
  • Hibernate3.2.3.ga
  • 与行家。(项目基础来自mkyong.com,spring教程,无注释)
  • MySQL版本:
[jvazquez@archbox ~]$ mysql --version
mysql  Ver 14.14 Distrib 5.5.9, for Linux (i686) using readline 5.1
Linux archbox 2.6.37-ARCH #1 SMP PREEMPT Fri Feb 18 16:58:42 UTC 2011 i686 Intel(R) Core(TM)2 Quad CPU Q8200 @ 2.33GHz GenuineIntel GNU/Linux

The application works fine in Arch Linux, Mac OS X 10.6, and FreeBSD 7.2. When I moved the jar file to another arch linux in a different host, using the same mysql, a similar my.cnf, and the similar kernel version, the connection died and obtained the same error as the original poster:

该应用程序在 Arch Linux、Mac OS X 10.6 和 FreeBSD 7.2 中运行良好。当我将jar文件移动到不同主机的另一个arch linux时,使用相同的mysql,类似的my.cnf,以及类似的内核版本,连接死亡并获得与原始海报相同的错误:

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

I tried every possible combination for this that I found on so and the forums (http://forums.mysql.com/read.php?39,180347,180347#msg-180347for example, which is closed now and I can't post .. ), specifically:

我为此尝试了在 so 和论坛上找到的所有可能的组合(例如http://forums.mysql.com/read.php?39,180347,180347#msg-180347,现在已关闭,我可以t post .. ),特别是:

  • Triple check that I wasn't using skip networking. (verified with ps aux and the my.cnf)
  • Tried enable log_warnings=1 in the my.cnf but obviously, I wasn't hitting the server so I didn't saw anything while using the app
  • SHOW ENGINE innodb STATUS didn't show anything at all; during the tests I could connect via shell, and php also connected to the mysql server
  • /etc/hosts has localhost 127.0.0.1
  • Tried the jdbc properties using localhost and 127.0.0.1 with no results
  • Tried adding c3p0 and changed the max_wait
  • Max connections in the my.cnf was changed to 900 , 2000 and still nothing my.cnf
  • Added wait_timeout = 60 my.cnf
  • Added net_wait_timeout = 360 my.cnf
  • Added the destroy-method="close" spring.xml
  • 三重检查我没有使用跳过网络。(用 ps aux 和 my.cnf 验证)
  • 尝试在 my.cnf 中启用 log_warnings=1 但显然,我没有访问服务器,所以在使用该应用程序时我什么也没看到
  • SHOW ENGINE innodb STATUS 根本没有显示任何东西;在测试期间我可以通过shell连接,php也连接到mysql服务器
  • /etc/hosts 有 localhost 127.0.0.1
  • 使用 localhost 和 127.0.0.1 尝试了 jdbc 属性,但没有结果
  • 尝试添加 c3p0 并更改 max_wait
  • my.cnf 中的最大连接数更改为 900 、 2000 并且仍然没有my.cnf
  • 添加 wait_timeout = 60 my.cnf
  • 添加 net_wait_timeout = 360 my.cnf
  • 添加了 destroy-method="close" spring.xml

As it was pointed out (if you look up for the same exception , you will find several so threads about the issue Reproduce com.mysql.jdbc.exceptions.jdbc4.CommunicationsException with a setup of Spring, hibernate and C3P0for example ).

正如它所指出的(如果你查找相同的异常,你会发现几个关于这个问题的线程Reproduce com.mysql.jdbc.exceptions.jdbc4.CommunicationsException,例如设置Spring、hibernate和C3P0)。

  1. If you are using tomcat, please check the security exception (again, it is on SO, you will find it )
  2. Check that you can resolve that url that you are using
  3. Try adding c3p0.
  4. Verify that there isn't a firewall rejecting your connections
  5. Finally , if you are using GNU/Linux ( ARch linux for example and you indeed obtain this exception ) Try MySQL Forums :: JDBC and Java :: EOFException: Can not read response from server. Expected to read 4 bytes, read 0 bytes before connection was unexpectedly lost
  1. 如果您使用的是tomcat,请检查安全异常(再次,它在SO上,您会找到它)
  2. 检查您是否可以解析您正在使用的网址
  3. 尝试添加 c3p0。
  4. 确认没有防火墙拒绝您的连接
  5. 最后,如果您使用的是 GNU/Linux(例如 ARch linux 并且您确实获得了此异常),请尝试 MySQL 论坛 :: JDBC 和 Java :: EOFException:无法从服务器读取响应。预期读取4个字节,连接意外丢失前读取0个字节

If the link get's removed, just add mysqld:ALL to /etc/hosts.allow

如果链接被删除,只需将 mysqld:ALL 添加到 /etc/hosts.allow

I know that is a bit extense, but it may help anybody using GNU/Linux and having this exception and this thread seemed the best place to post my research.

我知道这有点牵强,但它可能对任何使用 GNU/Linux 并有此异常的人有所帮助,这个线程似乎是发布我的研究的最佳场所。

Hope it helps

希望能帮助到你

回答by sujit sathe

check your wait timeout set on the DB server. Some times it defaults to 10 seconds. This looses the connection in 10 seconds.

检查您在数据库服务器上设置的等待超时。有时它默认为 10 秒。这会在 10 秒内断开连接。

mysql> show global variables like '%time%' ;

update it make it something like 28800

更新它使它像 28800

mysql> SET GLOBAL wait_timeout = 28800;

回答by PorridgeBear

Ensure skip-networking is commented out in my.cnf/my.ini

确保在 my.cnf/my.ini 中注释掉跳过网络

回答by Alexander Bering

In my case, the local loopback interface wasn't started, so "localhost" couldn't be resolved. You can check this by running "ifconfig" and you should see an interface called "lo". If it is not up, you can activate it by running "ifup lo" or "ifconfig lo up".

就我而言,本地环回接口未启动,因此无法解析“localhost”。您可以通过运行“ifconfig”来检查这一点,您应该会看到一个名为“lo”的界面。如果它没有启动,您可以通过运行“ifup lo”或“ifconfig lo up”来激活它。

回答by mstrthealias

In my case, the mysql.com downloaded Connector/J 5.1.29 .jar had this errorwhereas the 5.1.29 .jar downloaded from the MvnRepository did not.

在我的例子中,mysql.com 下载的 Connector/J 5.1.29 .jar 有这个错误,从 MvnRepository 下载5.1.29 .jar 没有

This happened when building a Google appengine application in Android Studio (gradle, Windows x64), communicating to a Linux MySQL server on the local network/local VM.

这发生在在 Android Studio(gradle,Windows x64)中构建 Google appengine 应用程序,与本地网络/本地 VM 上的 Linux MySQL 服务器通信时。

回答by LiuJian

I encountered same problem. I am using spring & dbcp & mysql 5.5But If I change localhostto 192.168.1.110then everything works. What make things more weird is mysql -h localhostjust works fine.

我遇到了同样的问题。我正在使用 spring & dbcp & mysql 5.5但是如果我更改localhost192.168.1.110那么一切正常。让事情变得更奇怪的是mysql -h localhost工作正常。

update: Finally found a solution. Changing bindaddress to localhostor 127.0.0.1in my.confwill fix the problem.

更新:终于找到了解决方案。将 bindaddress 更改为localhost127.0.0.1inmy.conf将解决该问题。

回答by 21stking

I see you are connecting to a remote host. Now the question is what type of a network are you using to connect to the internet?

我看到您正在连接到远程主机。现在的问题是您使用什么类型的网络连接到 Internet?

WINDOWS

视窗

If it's a mobile broadband devicethen get your machines IP address and add it to your hosting server so that your host server can allow connections coming from your machine.[your host might have turned this off due to security reasons]. Note that every time you use a different network device your IP changes.

如果它是移动宽带设备,则获取您的机器 IP 地址并将其添加到您的托管服务器,以便您的主机服务器可以允许来自您的机器的连接。[您的主机可能由于安全原因已关闭此功能]。请注意,每次使用不同的网络设备时,您的 IP 都会发生变化。

If you are using a LAN then set a static IP address on your machine then add it to your host.

如果您使用的是 LAN,则在您的机器上设置一个静态 IP 地址,然后将其添加到您的主机。

I hope this helps!! :)

我希望这有帮助!! :)