30

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.

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:

        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.

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.

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
5
  • have you looked at stackoverflow.com/questions/2297356/… Commented May 4, 2018 at 15:11
  • Please post the full exception stacktrace, it provides more information to determine the underlying cause. In any case, MySQL Connector/J is pretty old, you might want to try a newer version like 5.1.46 or 8.0.11. Also be aware that you likely shouldn't be using the FabricMySQLDriver, let alone register it yourself. Commented May 4, 2018 at 15:32
  • @MarkRotteveel Thank you. Tryed connector version 5.1.46 and it works. Updated head message with full stacktrace I had before Commented May 5, 2018 at 5:34
  • @MarkRotteveel And why I shouldn't use FabricMySQLDriver? Is it wrong or just redundant? Commented May 5, 2018 at 6:13
  • The Fabric driver is for Highly Available MySQL deployments, which I don't think you're using. On top of that, it requires a specific JDBC URL (fabric would require jdbc:mysql:fabric: as the prefix), so right now you're just using the automatically loaded normal driver. Commented May 5, 2018 at 7:15

2 Answers 2

69

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).

Sign up to request clarification or add additional context in comments.

2 Comments

yes it was 5.1.36 and resolved the issue updating to 5.1.49! Thanks!
@DaniloBreda You may want to consider upgrading to version 8.x instead.
31

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

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

Add the below version and you will be fine:

<mysql.driver.version>8.0.20</mysql.driver.version>

1 Comment

This was my case.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.