Java MySQLNonTransientConnectionException: 无法创建到数据库服务器的连接。尝试重新连接 3 次。放弃

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

MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up

javamysqldatabase-connection

提问by Diet Cookiez

I'm having trouble connecting to my database. I think there is a setting somewhere that I don't know about and needs to be changed. I have some really basic code here:

我在连接到我的数据库时遇到问题。我认为某处有一个我不知道并且需要更改的设置。我这里有一些非常基本的代码:

public static void main(String[] args)
{
    try
    {
        Connection con = DriverManager.getConnection("jdbc:mysql://IP:3306/TABLENAME?autoReconnect=true","USERNAME", "PASSWORD");
        con.close();
    }

    catch(Exception e)
    {
        e.printStackTrace();
    }
}

Obviously with the login info. And it causes this error:

显然是登录信息。它会导致此错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

Caused by: 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.

Caused by: java.net.ConnectException: Connection refused

I've looked through about 10 different posts about the same problem and tried their solutions and still couldn't get it to work.

我已经浏览了大约 10 篇关于同一问题的不同帖子,并尝试了他们的解决方案,但仍然无法解决问题。

回答by Lucky

This problem occurs when the MySQL service is not running and you're trying to connect to the mysql database.

当 MySQL 服务未运行并且您尝试连接到 mysql 数据库时,会出现此问题。

To resolve this in Windows,

要在 Windows 中解决此问题,

  • Start >> type 'services' and open it.

  • Find the MySQL service. Probably with name MySQLxx. xx being the version.
    eg. MySQL56, MySQL57 etc

  • Start the service and now try to connection to database.

  • 开始 >> 输入“服务”并打开它。

  • 找到 MySQL 服务。可能名称为 MySQLxx。xx 是版本。
    例如。MySQL56、MySQL57 等

  • 启动服务,现在尝试连接到数据库。

回答by Rahul Bachhav

You have to use database name instead of table name

您必须使用数据库名称而不是表名称

回答by Hemant Thorat

Increase Mysql max connection,

增加Mysql最大连接数,

SET GLOBAL max_connections = 1500;  

If still getting an error then queries might be taking too long to respond.

如果仍然出现错误,则查询可能需要很长时间才能响应。

In that case increase attempt while creating jdbc connection,

在这种情况下,在创建 jdbc 连接时增加尝试,

&maxReconnects=XX