Java com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:无法创建到数据库服务器的连接

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

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server

javamysqljdbc

提问by Alexey Bugerya

I'm new in Databases just started to learn them. I have MySQL Server 8.0., Workbench 8.0, Java connector 5.1.31, Java 1.8 itself. Followed multiple guides for newbies how to start with.

我是数据库新手,刚开始学习它们。我有 MySQL Server 8.0.、Workbench 8.0、Java 连接器 5.1.31、Java 1.8 本身。遵循多个新手指南如何开始。

So there is the case. I have database on localhost and successfully connect to it with workbench and via windows prompt. But after I execute code in java:

所以有这种情况。我在本地主机上有数据库,并通过工作台和 Windows 提示成功连接到它。但是在我用java执行代码之后:

        Driver sqlDriver = new FabricMySQLDriver();
        DriverManager.registerDriver(sqlDriver);
        Connection sqlConnection = DriverManager.getConnection("jdbc:mysql://localhost:3306/?user=root", "root", "root");

I get exception com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

我得到例外 com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.

Why it could happen? I've tried to reinstall MySQL, Cleaned OS variables, looked everywhere and couldn't find solution. Would be glad to find it here.

为什么会发生?我试图重新安装 MySQL,清理操作系统变量,到处寻找,但找不到解决方案。很高兴在这里找到它。

UPD:

更新:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
    at com.mysql.jdbc.Util.getInstance(Util.java:383)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1023)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:997)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:983)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:928)
    at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2407)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2328)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:408)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at com.company.Main.main(Main.java:11)
Caused by: java.lang.NullPointerException
    at com.mysql.jdbc.ConnectionImpl.getServerCharacterEncoding(ConnectionImpl.java:3309)
    at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1985)
    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1911)
    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1288)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2508)
    at com.mysql.jdbc.ConnectionImpl.connectWithRetries(ConnectionImpl.java:2346)
    ... 13 more

回答by Mark Rotteveel

The problem is the compatibility of older versions of MySQL Connector/J with MySQL 8. You need to upgrade to either MySQL Connector/J 5.1.46 or - better - 8.0.11 (or a higher version).

问题是旧版本的 MySQL Connector/J 与 MySQL 8 的兼容性。您需要升级到 MySQL Connector/J 5.1.46 或 - 更好 - 8.0.11(或更高版本)。

回答by Ramesh Kumar

In my case, I was using maven dependency to use JDBC connector. I just changed the version of dependency. This worked for me

就我而言,我使用 maven 依赖来使用 JDBC 连接器。我只是更改了依赖项的版本。这对我有用

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>