Git Product home page Git Product logo

cloud-sql-jdbc-socket-factory's Introduction

Cloud SQL Connector for Java

Build Status

The Cloud SQL Connector for Java is a library for the MySQL/Postgres JDBC and R2DBC drivers that allows a user with the appropriate permissions to connect to a Cloud SQL database without having to deal with IP whitelisting or SSL certificates manually.

Examples

For examples of this library being used in the context of an application, check out the sample applications located here.

Authentication

This library uses the Application Default Credentials to authenticate the connection to the Cloud SQL server. For more details, see the previously mentioned link.

To activate credentials locally, use the following gcloud command:

gcloud auth application-default login

Instructions for JDBC

Add library as a dependency

MySQL

Note: Use your JDBC driver version to figure out which SocketFactory you should use. If you are unsure, it is recommended to use the latest version of mysql-connector-java:8.x.

JDBC Driver Version Cloud SQL Socket Factory Version
mysql-connector-java:8.x mysql-socket-factory-connector-j-8:1.0.16
mysql-connector-java:6.x mysql-socket-factory-connector-j-6:1.0.16
mysql-connector-java:5.1.x mysql-socket-factory:1.0.16
Maven

Include the following in the project's pom.xml:

<dependency>
    <groupId>com.google.cloud.sql</groupId>
    <artifactId>mysql-socket-factory-connector-j-8</artifactId>
    <version>1.1.0</version>
</dependency>
Gradle

Include the following the project's build.gradle

compile 'com.google.cloud.sql:mysql-socket-factory-connector-j-8:1.1.0'

PostgreSQL

Maven

Include the following in the project's pom.xml:

<dependency>
    <groupId>com.google.cloud.sql</groupId>
    <artifactId>postgres-socket-factory</artifactId>
    <version>1.1.0</version>
</dependency>

Gradle

Include the following the project's gradle.build

compile 'com.google.cloud.sql:postgres-socket-factory:1.1.0'

*Note: Also include the JDBC Driver for MySQL, org.postgresql:postgresql:<LATEST-VERSION>

Creating the JDBC URL

MySQL

Base JDBC URL: jdbc:mysql:///<DATABASE_NAME>

When specifying the JDBC connection URL, add the additional parameters:

Property Value
socketFactory com.google.cloud.sql.mysql.SocketFactory
cloudSqlInstance The instance connection name (found on the instance details page)
user MySQL username
password MySQL user's password

The full JDBC URL should look like this:

jdbc:mysql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD>

Note: The host portion of the JDBC URL is currently unused, and has no effect on the connection process. The SocketFactory will get your instances IP address base on the provided cloudSqlInstance arg.

Postgres

Base JDBC URL: jdbc:postgresql:///<DATABASE_NAME>

When specifying the JDBC connection URL, add the additional parameters:

Property Value
socketFactory com.google.cloud.sql.postgres.SocketFactory
cloudSqlInstance The instance connection name (found on the instance details page)
user Postgres username
password Postgres user's password

The full JDBC URL should look like this:

jdbc:postgresql:///<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<POSTGRESQL_USER_NAME>&password=<POSTGRESQL_USER_PASSWORD>

Note: The host portion of the JDBC URL is currently unused, and has no effect on the connection process. The SocketFactory will get your instances IP address base on the provided cloudSqlInstance arg.


Instructions for R2DBC

Add library as a dependency

MySQL

Maven

Include the following in the project's pom.xml:

    <dependency>
      <groupId>com.google.cloud.sql</groupId>
      <artifactId>cloud-sql-connector-r2dbc-mysql</artifactId>
      <version>1.1.0</version>
    </dependency>
Gradle

Include the following the project's build.gradle

compile 'com.google.cloud.sql:cloud-sql-connector-r2dbc-mysql:1.1.0'

*Note: Also include the R2DBC Driver for MySQL, dev.miku:r2dbc-mysql:<LATEST-VERSION>

PostgreSQL

Maven

Include the following in the project's pom.xml:

    <dependency>
      <groupId>com.google.cloud.sql</groupId>
      <artifactId>cloud-sql-connector-r2dbc-postgres</artifactId>
      <version>1.1.0</version>
    </dependency>
Gradle

Include the following the project's build.gradle

compile 'com.google.cloud.sql:cloud-sql-connector-r2dbc-postgres:1.1.0'

*Note: Also include the R2DBC Driver for Postgres, io.r2dbc:r2dbc-postgresql:<LATEST-VERSION>

Creating the R2DBC URL

MySQL

R2DBC URL template: r2dbc:gcp:mysql//<DB_USER>:<DB_PASS>@<CLOUD_SQL_CONNECTION_NAME>/<DATABASE_NAME>

Add the following parameters:

Property Value
DATABASE_NAME The name of the database to connect to
CLOUD_SQL_CONNECTION_NAME The instance connection name (found on the instance details page)
DB_USER MySQL username
DB_PASS MySQL user's password
Postgres

R2DBC URL template: r2dbc:gcp:postgres//<DB_USER>:<DB_PASS>@<CLOUD_SQL_CONNECTION_NAME>/<DATABASE_NAME>

Add the following parameters:

Property Value
DATABASE_NAME The name of the database to connect to
CLOUD_SQL_CONNECTION_NAME The instance connection name (found on the instance details page)
DB_USER Postgres username
DB_PASS Postgres user's password

Building the Drivers

To build a fat JAR containing the JDBC driver with the bundles Socket Factory dependencies you can issue the following Maven command from the location containing the project pom.xml:

mvn -P jar-with-dependencies clean package -DskipTests

This will create a target sub-folder within each of the module directories. Within these target directories you'll find the JDBC driver files.

Example:

mysql-socket-factory-connector-j-8–1.0.16-jar-with-dependencies.jar
postgres-socket-factory-1.0.16-jar-with-dependencies.jar

Additional Information

Specifying IP Types

The ipTypes argument can be used to specify a comma delimited list of preferred IP types for connecting to a Cloud SQL instance. The argument ipTypes=PRIVATE will force the SocketFactory to connect with an instance's associated private IP. Default value is PUBLIC,PRIVATE.

Firewall configuration

The Cloud SQL proxy establishes connections to Cloud SQL instances using port 3307. Applications that are protected by a firewall may need to be configured to allow outgoing connections on TCP port 3307. A connection blocked by a firewall typically results in an error stating connection failure (e.g. com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure).

Connect with IntelliJ

In order to connect IntelliJ to your Cloud SQL instance, you will need to add this library as a jar with dependencies in "Additional Files" section on the driver settings page. Prebuilt fat jars can be found on the Releases page for this purpose.

Connection via Unix Sockets

To connect using a Unix domain socket (such as the one created by the Cloud SQL proxy), you can use the unixSocketPath property to specify a path to a local file instead of connecting directly over TCP.

Example using MySQL:

jdbc:mysql:///<DATABASE_NAME>?unixSocketPath=</PATH/TO/UNIX/SOCKET>&cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD>

Example using PostgreSQL:

jdbc:postgresql:///<DATABASE_NAME>?unixSocketPath=</PATH/TO/UNIX/SOCKET>&cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<POSTGRESQL_USER_NAME>&password=<POSTGRESQL_USER_PASSWORD>

cloud-sql-jdbc-socket-factory's People

Contributors

ansidev avatar anuraaga avatar bdmogal avatar bhtucker avatar dmitry-s avatar joaoandremartins avatar justjoheinz avatar kirbyquerby avatar kurtisvg avatar lesv avatar marshallpierce avatar meltsufin avatar piaxc avatar release-please[bot] avatar renovate-bot avatar sergei-ivanov avatar shubha-rajan avatar t1ngj13 avatar vadimberezniker avatar vhajdari avatar yhrn avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.