0

This is the code I'm using to connect to the db:

Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://x:3306/y","username","password");

Where x is IPv4 address from Instance details and y is the project name.

I have added 0.0.0.0/0to Access Control > Authorized Networks.

I've read that this worked for some people, but I'm getting this error:

Exception in thread "main" 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

I tried the solutions from these answers Answer 1 and Answer 2 but it still didn't work.

EDIT: This is the whole stack trace:

    Exception in thread "main" 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.
        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:404)
        at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:988)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:341)
        at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2251)
        at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
        at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
        at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
        at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
        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:404)
        at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
        at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
        at java.sql.DriverManager.getConnection(DriverManager.java:664)
        at java.sql.DriverManager.getConnection(DriverManager.java:247)
        at test.App.main(App.java:33)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)
    Caused by: java.net.ConnectException: Connection timed out: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
        at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
        at java.net.Socket.connect(Socket.java:589)
        at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:211)
        at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:300)
        ... 20 more

EDIT: Fixed it. Forgot to save changes when I was whitelisting 0.0.0.0/0

Community
  • 1
  • 1
  • Can you connect to the db via mysql console? – Janez Kuhar Apr 20 '17 at 22:41
  • using the google cloud shell, yes. If you're not referring to this, then what mysql console? @JanezKuhar – Teo Vecerdi Apr 20 '17 at 22:46
  • 1
    where are you running this code? GCE? app engine? somewhere else? also please always include the full exception, not the first line. there's usually more information int the stack trace – Vadim Apr 20 '17 at 22:49
  • Can you answer my first question? Where are you running the app? – Vadim Apr 20 '17 at 22:58
  • @Vadim I don't know what you mean by that. It's the first time working with Google Cloud. I'm running the app from IntelliJ IDEA on Windows x64 – Teo Vecerdi Apr 20 '17 at 23:01
  • If you have whitelisted 0.0.0.0/0 and are still getting a connection timeout, then it's possible you're behind a firewall that is blocking outbound connections on port 3306. Is that possible? BTW whitelisting 0.0.0.0/0 is usually a bad idea from a security perspective – Vadim Apr 20 '17 at 23:04
  • @Vadim port 3306 works fine when using a localhost database or other free online services – Teo Vecerdi Apr 20 '17 at 23:09
  • @Vadim sorry for wasting your time. I forgot to click save changes when I was whitelisting 0.0.0.0/0 – Teo Vecerdi Apr 20 '17 at 23:17
  • you should consider using https://github.com/GoogleCloudPlatform/cloud-sql-mysql-socket-factory to connect to your instance. it's much more secure than whitelisting 0.0.0.0/0 – Vadim Apr 20 '17 at 23:18
  • @Vadim when I use the example provided there I get this error `Unable to obtain credentials to communicate with the Cloud SQL API` – Teo Vecerdi Apr 20 '17 at 23:30
  • Check out the Credentials section in the README: https://github.com/GoogleCloudPlatform/cloud-sql-mysql-socket-factory#credentials, you need to run `gcloud auth application-default login` once to generate credentials for the library to use – Vadim Apr 20 '17 at 23:32
  • @Vadim and if I give the app to, let's say, some friends, would it work or would they need to generate credentials too? – Teo Vecerdi Apr 20 '17 at 23:37
  • If they are running the program directly, they would also need to generate credentials once. – Vadim Apr 20 '17 at 23:38
  • @Vadim if I include the json file with the app will it work? – Teo Vecerdi Apr 20 '17 at 23:39
  • hypothetically yes, but giving that file out to other people is almost equivalent to giving the password to your account so i would not recommend it. if you trust your friends, you can create a service account with limited privileges and distribute those credentials. typically, people will create an API layer that runs in the cloud that takes care of accessing the database so that the client application never accesses the DB directly. – Vadim Apr 21 '17 at 00:05

0 Answers0