I have a mysql server running locally to which I am able to connect using

mysql -h localhost -u root

It does not require a password. But when I try to connect with the same attributes (jdbcurl = "jdbc:mysql://localhost:3306/mysql", user = root) via JDBC, I get this weird exception

he last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
    at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
    at com.mysql.cj.NativeSession.connect(NativeSession.java:144)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)
    ... 33 more
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
    at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
    at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
    at java.base/java.net.Socket.connect(Socket.java:591)
    at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
    at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
    ... 36 more

What could be the issue here?


Tried out the suggestion given in the comments to try to connect to mysql database via mysql as being done in java.

mysql -h localhost -u root mysql

This works and I get connected to the mysql db.

  • You didn't connect with your mysql.exe connection to a database, but in java you told the connection that you want to connect to the database mysql. instead of mysql use your own database and try tihs also in the command.
    – nbk
    Apr 24, 2020 at 17:36
  • @nbk tried out your suggestion. I have updated the question with an EDIT
    – Ashwin
    Apr 27, 2020 at 1:58
  • Please add the connection code with the connection string init
    – nbk
    Apr 27, 2020 at 7:13
  • you might want to check the answers here as well stackoverflow.com/questions/6865538/… May 3, 2020 at 4:19

2 Answers 2


Case 1 - JDBC connects as [email protected] but privilege is not granted:

It is possible that your mysql CLI is connecting via Unix domain socket (as root@localhost) while your Java application attempts to connect via TCP (as [email protected]) but privilege is not granted.

To verify, run status command in the mysql client:

mysql -h localhost -u root -e 'status'

The Connection field should show if you are connecting to Localhost via UNIX socket or via TCP/IP

If you are connecting via UNIX socket, you may try forcing the client to use TCP instead:

mysql -h localhost -u root --protocol TCP -e 'status'

If it fails, you may check if you have the user allowed from the host

SELECT * FROM mysql.user WHERE Host = ''\G

An empty result set indicates that you don't have that user [email protected] yet. You may create it with:


You may then retry the TCP and Java application.

Case 2 - localhost translated to IPv6

If you have no problem connecting via TCP as described above, then another possibility is that the localhost string is translated by your Java application to the IPv6 ::1 while your MySQL server isn't configured to listen to the IPv6 interface.

You may try using in your jdbcurl, e.g.: jdbc:mysql://

If for some reasons you cannot change jdbcurl, you may try launching your Java application with the -Djava.net.preferIPv4Stack=true option, e.g.:

java -Djava.net.preferIPv4Stack=true -jar myapp.jar

Which MySQL Version you are using ?

You won't be able to use root user I guess.

Create new application user and use that user for application.

CREATE USER appuser@'%' identified by 'PASSWORD';
Grant SELECT on *.* to appuser@'%';
flush privileges;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.