简体   繁体   中英

Connecting to Google Cloud SQL with Java

I've been trying for days and have gotten nowhere. I am trying to connect to my MySQL database through a JavaFX program I'm building, without requiring me to whitelist every IP that attempts to connect. The GCP support team has replied to me once but completely misinterpreted the issue (gave examples of logs that only occurred after I whitelisted my own IP to test the other aspects of my program).

I found instructions at https://cloud.google.com/sql/docs/mysql/connect-external-app#java , and pasted the following code into my main method (substituting the appropriate values for databaseName, instanceConnectionName, username, and password):

String jdbcUrl = String.format(
"jdbc:mysql://google/%s?cloudSqlInstance=%s"
+ "&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false",
databaseName,
instanceConnectionName);

Connection connection = DriverManager.getConnection(jdbcUrl, username, 
password);

I then enabled the Cloud API and, to the best of my knowledge, installed and authenticated the Cloud SDK, as directed. Yet despite all of that, I still cannot connect to the instance without a whitelisted IP address, even though the documentation says this is the workaround for that. Does anyone see an issue with how I'm attempting to connect or know how to make this work?

There are some more detailed instructions in the README for the repo of the project.

Some potential tripping points:

  1. Use Application Default Credentials to provide credentials to the factory.

  2. Make sure you have the Cloud SQL API enabled to your project (and if you are using a service account, make sure to have the Cloud SQL Client role added to it).

  3. Add the library as a dependency in your POM or gradlefile.

  4. Make sure your firewall allows out on port 3307 to your Cloud SQL instance.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM