|
2 | 2 | Status](https://travis-ci.org/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory.svg?branch=master)](https://travis-ci.org/GoogleCloudPlatform/cloud-sql-jdbc-socket-factory) |
3 | 3 | ## Cloud SQL Socket Factory for JDBC drivers |
4 | 4 |
|
5 | | -The Cloud SQL Socket Factory is a library for the MySQL/Postgres JDBC drivers |
6 | | -that allows a user with the appropriate permissions to connect to a Cloud SQL |
7 | | -database without having to deal with IP whitelisting or SSL certificates |
8 | | -manually. |
| 5 | +The Cloud SQL Socket Factory is a library for the MySQL/Postgres JDBC drivers that allows a user |
| 6 | +with the appropriate permissions to connect to a Cloud SQL database without having to deal with IP |
| 7 | +whitelisting or SSL certificates manually. |
9 | 8 |
|
10 | 9 | ## Instructions |
11 | 10 |
|
12 | | -The library is [available in Maven Central](http://search.maven.org/#artifactdetails%7Ccom.google.cloud.sql%7Cmysql-socket-factory%7C1.0.4%7Cjar). |
| 11 | +This library is available |
13 | 12 |
|
14 | | -Add a dependency using your favorite build tool. Maven and Gradle examples are shown below. |
| 13 | +[Maven Central](http://search.maven.org/#artifactdetails%7Ccom.google.cloud.sql%7Cmysql-socket-factory%7C1.0.4%7Cjar). |
15 | 14 |
|
16 | 15 |
|
17 | | -### MySQL |
| 16 | +### Authentication |
18 | 17 |
|
19 | | -**Note**: If you wish to use the 6.x (development) version of the MySQL driver, use the artifact id |
20 | | -'mysql-socket-factory-connector-j-6'. |
| 18 | +This library uses the [Application Default Credentials]( |
| 19 | +https://developers.google.com/identity/protocols/application-default-credentials) to authenticate |
| 20 | +the connection to the Cloud SQL server. For more details, see the previously mentioned link. |
21 | 21 |
|
22 | | -#### Adding dependency (Maven) |
| 22 | +To activate credentials locally, use the following [gcloud](https://cloud.google.com/sdk/gcloud/) |
| 23 | +command: |
| 24 | +```bash |
| 25 | +gcloud auth application-default login |
| 26 | +``` |
| 27 | + |
| 28 | +### Add library as a dependency |
| 29 | + |
| 30 | +#### MySQL |
| 31 | + |
| 32 | +**Note**: Use the correct version to match your JDBC driver: |
| 33 | + |
| 34 | +| JDBC Driver Version | Cloud SQL Socket Factory Version | |
| 35 | +| -------------------------- | ---------------------------------------- | |
| 36 | +| mysql-connector-java:8.x | mysql-socket-factory-connector-j8:1.0.10 | |
| 37 | +| mysql-connector-java:6.x | mysql-socket-factory-connector-j6:1.0.10 | |
| 38 | +| mysql-connector-java:5.1.x | mysql-socket-factory:1.0.10 | |
23 | 39 |
|
| 40 | + |
| 41 | +##### Maven |
| 42 | +Include the following in the project's `pom.xml`: |
24 | 43 | ```maven-pom |
25 | 44 | <dependency> |
26 | 45 | <groupId>com.google.cloud.sql</groupId> |
27 | | - <artifactId>mysql-socket-factory</artifactId> |
28 | | - <version>1.0.9</version> |
| 46 | + <artifactId>mysql-socket-factory-connector-j8</artifactId> |
| 47 | + <version>1.0.10</version> |
29 | 48 | </dependency> |
30 | 49 | ``` |
31 | 50 |
|
32 | | -#### Adding dependency (Gradle) |
33 | | - |
| 51 | +#### Gradle |
| 52 | +Include the following the project's `gradle.build` |
34 | 53 | ```gradle |
35 | | -compile 'com.google.cloud.sql:mysql-socket-factory:1.0.9' |
| 54 | +compile 'com.google.cloud.sql:mysql-socket-factory:1.0.10' |
36 | 55 | ``` |
37 | 56 |
|
38 | | -#### Using |
39 | | - |
40 | | -When specifying the JDBC connection URL, add two additional parameters: |
41 | | - |
42 | | -| Property | Value | |
43 | | -| ---------------- | ------------- | |
44 | | -| socketFactory | com.google.cloud.sql.mysql.SocketFactory | |
45 | | -| cloudSqlInstance | The instance connection name (which is found on the instance details page in Google Developers Console) | |
46 | | - |
47 | | -For example, if the instance connection name is `foo:bar:baz`, the JDBC URL |
48 | | -would be |
49 | | -`jdbc:mysql://google/mydb?socketFactory=com.google.cloud.sql.mysql.SocketFactory&cloudSqlInstance=foo:bar:baz` |
50 | | - |
51 | | -A tool is available in `examples/getting-started` that can help generate the JDBC URL and verify that connectivity can be established. |
52 | | - |
53 | | -### PostgreSQL |
54 | | - |
55 | | -#### Adding dependency (Maven) |
| 57 | +#### PostgreSQL |
56 | 58 |
|
| 59 | +##### Maven |
| 60 | +Include the following in the project's `pom.xml`: |
57 | 61 | ```maven-pom |
58 | 62 | <dependency> |
59 | 63 | <groupId>com.google.cloud.sql</groupId> |
60 | | - <artifactId>postgres-socket-factory</artifactId> |
61 | | - <version>1.0.9</version> |
| 64 | + <artifactId>mysql-socket-factory</artifactId> |
| 65 | + <version>1.0.1o</version> |
62 | 66 | </dependency> |
63 | 67 | ``` |
64 | 68 |
|
65 | | -#### Adding dependency (Gradle) |
66 | | - |
| 69 | +#### Gradle |
| 70 | +Include the following the project's `gradle.build` |
67 | 71 | ```gradle |
68 | | -compile 'com.google.cloud.sql:postgres-socket-factory:1.0.9' |
| 72 | +compile 'com.google.cloud.sql:mysql-socket-factory:1.0.10' |
69 | 73 | ``` |
70 | 74 |
|
71 | | -#### Using |
72 | 75 |
|
73 | | -When specifying the JDBC connection URL, add two additional parameters: |
| 76 | +#### Creating the JDBC URL |
| 77 | + |
| 78 | +##### MySQL |
| 79 | + |
| 80 | +Base JDBC url: `jdbc:mysql://google/<DATABASE_NAME>` |
| 81 | + |
| 82 | +When specifying the JDBC connection URL, add the additional parameters: |
74 | 83 |
|
75 | 84 | | Property | Value | |
76 | 85 | | ---------------- | ------------- | |
77 | 86 | | socketFactory | com.google.cloud.sql.postgres.SocketFactory | |
78 | | -| socketFactoryArg | The instance connection name (which is found on the instance details page in Google Developers Console) | |
| 87 | +| socketFactoryArg | The instance connection name (found on the instance details) | |
| 88 | +| useSSL | False | |
| 89 | +| user | MySQL username | |
| 90 | +| password | MySQL user's password | |
| 91 | + |
| 92 | +The full JDBC url should look like this: |
| 93 | +``` |
| 94 | +jdbc:mysql://google/<DATABASE_NAME>?cloudSqlInstance=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.mysql.SocketFactory&useSSL=false&user=<MYSQL_USER_NAME>&password=<MYSQL_USER_PASSWORD> |
| 95 | +``` |
79 | 96 |
|
80 | | -For example, if the instance connection name is `foo:bar:baz`, the JDBC URL |
81 | | -would be |
82 | | -`jdbc:postgresql://google/mydb?socketFactory=com.google.cloud.sql.postgres.SocketFactory&socketFactoryArg=foo:bar:baz` |
| 97 | +##### Postgres |
83 | 98 |
|
84 | | -A tool is available in `examples/getting-started` that can help generate the JDBC URL and verify that connectivity can be established. |
| 99 | +Base JDBC url: `jdbc:mysql://google/<DATABASE_NAME>` |
85 | 100 |
|
86 | | -## Credentials |
| 101 | +When specifying the JDBC connection URL, add the additional parameters: |
| 102 | + |
| 103 | +| Property | Value | |
| 104 | +| ---------------- | ------------- | |
| 105 | +| socketFactory | com.google.cloud.sql.postgres.SocketFactory | |
| 106 | +| socketFactoryArg | The instance connection name (which is found on the instance details page in Google Developers Console) | |
| 107 | +| user | Postgres username | |
| 108 | +| password | Postgres user's password | |
| 109 | + |
| 110 | +The full JDBC url should look like this: |
| 111 | +``` |
| 112 | +jdbc:postgresql://google/<DATABASE_NAME>?useSSL=false&socketFactoryArg=<INSTANCE_CONNECTION_NAME>&socketFactory=com.google.cloud.sql.postgres.SocketFactory&user=<POSTGRESQL_USER_NAME>&password=<POSTGRESQL_USER_PASSWORD> |
| 113 | +``` |
87 | 114 |
|
88 | | -The library needs to obtain credentials in order to retrieve SSL certificates that are used to connect to the instance. |
89 | | -[Application Default Credentials](https://developers.google.com/identity/protocols/application-default-credentials) are used for this purpose. |
| 115 | +## Connecting via Unix Sockets |
90 | 116 |
|
91 | | -On Google Compute Engine and Google App Engine, the VM/application service account is used. |
| 117 | +The library will automatically detect when it is running on GAE Standard, and will connect via the |
| 118 | + provided unix socket for reduced latency. |
92 | 119 |
|
93 | | -For local development, application default credentials written by gcloud are used, if present. |
94 | | -You must run `gcloud auth application-default login` once for the credentials to become available to the library. |
| 120 | +To force the library to connect to a unix socket (created by the Cloud SQL proxy) when running |
| 121 | +outside of GAE-Standard, set the environment variable `CLOUD_SQL_FORCE_UNIX_SOCKET` to any value. |
0 commit comments