com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Asked Answered
T

1

1

I have a google app engine connected project with which I am trying to connect to a google cloud sql database.I upload my project to google and via it I try to connect to the db.

my connection URL is as follows->

Class.forName("com.mysql.jdbc.GoogleDriver");
String password="";
    url = "jdbc:google:mysql://my-project-id:my-instance-name/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";

    Connection conn = DriverManager.getConnection(url);

it is at the point where I attempt to get a connection with the url that I get an exception

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 then added the following string to my url-> &autoReconnect=true&failOverReadOnly=false&maxReconnects=10

then received a different exception-->

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

I have tried pinging the ip address of the db instance and it works fine.

Do I need to give any explicit permissions in my cloud sql instance for it to be able to connect?

Any suggestions are welcome.

The strange thing is that I am able to connect to the db using eclipse by going to project->google->app engine->usegoogle cloud sql instance->configure.I use the same user name (root) and password ("") here as in the url in the code but for some reason it refuses to connect through the code. I have also enabled the

<use-google-connector-j>true</use-google-connector-j>  

tag in my appengine-web.xml

I changed the root password from "" to some value and I am able to connect via mysql client and I can access the database, but on giving the same password in code it refuses to connect.

Any suggestions will be appreciated. Thanks, Laura

Townley answered 19/11, 2013 at 7:30 Comment(5)
is db service is running ?Resupine
I looked in to the summary tab under cloud sql (on clicking my instance id)and it says the status is running.Townley
under the Authorized IP Addresses: I have "None" ,do I need to mention any IP addresses to connect? Can that be an issue?Townley
If you are connecting using your instance IP, which is the recommended way of connecting from your LOCAL computer, yes, you should whitelist your IP address to allow incoming connections. If you are connecting from App Engine (not from your local devserver, from Google production servers) then you need to authorize the AppEngine application.Alethaalethea
Also please take a look at #20054362Alethaalethea
W
0

Instance Id by default contains the project Id. So don't explicitly add project Id again.

Project ID: sampleapp

Instance Id: sampleapp:instance1 (This is how google cloud sql creates by default)

Connections string:

Correct:

jdbc:google:mysql://sampleapp:instance1/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";

In correct

jdbc:google:mysql://sampleapp:sampleapp:instance1/dbname?user=root&password="+password+"useUnicode=true&characterEncoding=UTF-8";
Workman answered 19/11, 2013 at 18:14 Comment(3)
Thanks a lot for the answer ! I changed the url as suggested and it worked! .Townley
According to https://mcmap.net/q/1052517/-app-engine-java-servlet-does-not-connect-to-cloud-sql, one shouldn't use a password.Pedigo
No problem @LauraStoneWorkman

© 2022 - 2024 — McMap. All rights reserved.