Erma Win
Erma Win

Reputation: 1

Connecting to Cloud SQL using Compute Engine via JDBC

I cannot connect to my database thru Compute Engine.

Things I have done so far:

  1. Setup my Compute Engine
  2. Connect my Compute Engine to my Cloud SQL
  3. Install and run Cloud Proxy on my Compute Engine
  4. Included the JDBC Jar file on my libs folder
  5. Created the JDBC Connection string

"jdbc:mysql:///?cloudSqlInstance=&socketFactory=com.google.cloud.sql.mysql.SocketFactory;user=&password=";

  1. Added dependency on my Gradle file

-implementation fileTree(dir: 'libs', include: ['*.jar'])
-implementation 'com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.1.0'

Here is the stack trace:

W/System.err: java.sql.SQLNonTransientConnectionException: Cannot connect to MySQL server on localhost:3,306. W/System.err: Make sure that there is a MySQL server running on the machine/port you are trying to connect to and that the machine this software is running on is able to connect to this host/port (i.e. not firewalled). Also make sure that the server has not been started with the --skip-networking flag. at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:110) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89) at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63) W/System.err: at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73) at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:470) at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246) W/System.err: at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:197) at java.sql.DriverManager.getConnection(DriverManager.java:569) at java.sql.DriverManager.getConnection(DriverManager.java:237) W/System.err: at com.dedase.dedaseconstruction.ui.login.LoginActivity$GetPatientID.doInBackground(LoginActivity.java:150) at com.dedase.dedaseconstruction.ui.login.LoginActivity$GetPatientID.doInBackground(LoginActivity.java:136) at android.os.AsyncTask$2.call(AsyncTask.java:333) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:245) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636) W/System.err: at java.lang.Thread.run(Thread.java:764) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'com.mysql.cj.protocol.SocketConnection com.mysql.cj.protocol.a.NativeProtocol.getSocketConnection()' on a null object reference at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:980) at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) W/System.err: at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:456) ... 12 more I/Choreographer: Skipped 58 frames! The application may be doing too much work on its main thread. I/zygote: Do full code cache collection, code=123KB, data=111KB After code cache collection, code=117KB, data=78KB

Upvotes: 0

Views: 474

Answers (1)

yuranos
yuranos

Reputation: 9715

This line localhost:3,306. looks really suspicious. Are you sure you don't have a typo somewhere?
You probably came across this repo, and it's not very accurate. I don't have a java client at hand to test it, but I do use Cloud SQL with a different stack and the point of using Proxy is that your app connects to the DB via it as if the DB itself is running locally. Your connection string should not have anything apart from creds and localhost:3306.

Upvotes: 0

Related Questions