Skip to content

Commit 1860b76

Browse files
authoredJan 23, 2025··
docs: Add docs for connector configuration using DNS Names, Part of #2043. (#2102)
Adds documentation to describe how to use the hostname for JDBC and R2DBC.
1 parent c0df76e commit 1860b76

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed
 

‎docs/jdbc.md

+117
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,123 @@ Properties connProps = new Properties();
563563
connProps.setProperty("cloudSqlRefreshStrategy", "lazy");
564564
```
565565

566+
567+
568+
### Using DNS domain names to identify instances
569+
570+
The connector can be configured to use DNS to look up an instance. This would
571+
allow you to configure your application to connect to a database instance, and
572+
centrally configure which instance in your DNS zone.
573+
574+
#### Configure your DNS Records
575+
576+
Add a DNS TXT record for the Cloud SQL instance to a **private** DNS server
577+
or a private Google Cloud DNS Zone used by your application.
578+
579+
**Note:** You are strongly discouraged from adding DNS records for your
580+
Cloud SQL instances to a public DNS server. This would allow anyone on the
581+
internet to discover the Cloud SQL instance name.
582+
583+
For example: suppose you wanted to use the domain name
584+
`prod-db.mycompany.example.com` to connect to your database instance
585+
`my-project:region:my-instance`. You would create the following DNS record:
586+
587+
- Record type: `TXT`
588+
- Name: `prod-db.mycompany.example.com` – This is the domain name used by the application
589+
- Value: `my-project:region:my-instance` – This is the instance name
590+
591+
592+
### Creating the JDBC URL
593+
594+
When specifying the JDBC connection URL, add the additional parameters:
595+
596+
| Property | Value |
597+
|------------------|-------------------------------------------------------------------|
598+
| socketFactory | <SOCKET_FACTORY_CLASS> |
599+
| user | Database username |
600+
| password | Database user's password |
601+
602+
Replace <SOCKET_FACTORY_CLASS> with the class name specific to your database.
603+
604+
#### MySQL
605+
606+
HOST: The domain name configured in your DNS TXT record.
607+
608+
Base JDBC URL: `jdbc:mysql://<HOST>/<DATABASE_NAME>`
609+
610+
SOCKET_FACTORY_CLASS: `com.google.cloud.sql.mysql.SocketFactory`
611+
612+
The full JDBC URL should look like this:
613+
614+
```java
615+
String jdbcUrl = "jdbc:mysql://<HOST>/<DATABASE_NAME>?"
616+
+ "&socketFactory=com.google.cloud.sql.mysql.SocketFactory"
617+
+ "&user=<MYSQL_USER_NAME>"
618+
+ "&password=<MYSQL_USER_PASSWORD>";
619+
```
620+
621+
#### Maria DB
622+
623+
HOST: The domain name configured in your DNS TXT record.
624+
625+
Base JDBC URL: `jdbc:mariadb://<HOST>/<DATABASE_NAME>`
626+
627+
SOCKET_FACTORY_CLASS: `com.google.cloud.sql.mariadb.SocketFactory`
628+
629+
**Note:** You can use `mysql` as the scheme if you set `permitMysqlScheme` on
630+
the URL.
631+
Please refer to the
632+
MariaDB [documentation](https://mariadb.com/kb/en/about-mariadb-connector-j/#jdbcmysql-scheme-compatibility).
633+
634+
The full JDBC URL should look like this:
635+
636+
```java
637+
String jdbcUrl = "jdbc:mariadb://<HOST>/<DATABASE_NAME>?"
638+
+ "&socketFactory=com.google.cloud.sql.mariadb.SocketFactory"
639+
+ "&user=<MYSQL_USER_NAME>"
640+
+ "&password=<MYSQL_USER_PASSWORD>";
641+
```
642+
643+
#### Postgres
644+
645+
Base JDBC URL: `jdbc:postgresql://<HOST>/<DATABASE_NAME>`
646+
647+
SOCKET_FACTORY_CLASS: `com.google.cloud.sql.postgres.SocketFactory`
648+
649+
When specifying the JDBC connection URL, add the additional parameters:
650+
651+
The full JDBC URL should look like this:
652+
653+
```java
654+
String jdbcUrl = "jdbc:postgresql://<HOST>/<DATABASE_NAME>?"
655+
+ "&socketFactory=com.google.cloud.sql.postgres.SocketFactory"
656+
+ "&user=<POSTGRESQL_USER_NAME>"
657+
+ "&password=<POSTGRESQL_USER_PASSWORD>";
658+
```
659+
660+
#### SQL Server
661+
662+
Base JDBC URL: `jdbc:sqlserver://localhost;databaseName=<DATABASE_NAME>`
663+
664+
SOCKET_FACTORY_CLASS: `com.google.cloud.sql.sqlserver.SocketFactory`
665+
666+
The full JDBC URL should look like this:
667+
668+
```java
669+
String jdbcUrl = "jdbc:sqlserver://localhost;"
670+
+ "databaseName=<DATABASE_NAME>;"
671+
+ "socketFactoryClass=com.google.cloud.sql.sqlserver.SocketFactory;"
672+
+ "socketFactoryConstructorArg=<HOST>;"
673+
+ "user=<USER_NAME>;"
674+
+ "password=<PASSWORD>";
675+
```
676+
677+
**Note:** The host portion of the JDBC URL is currently unused, and has no
678+
effect on the connection process. The SocketFactory will get your instances IP
679+
address based on the provided `socketFactoryConstructorArg` arg.
680+
681+
682+
566683
## Configuration Reference
567684

568685
- See [Configuration Reference](configuration.md)

‎docs/r2dbc.md

+84
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,90 @@ ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
362362
.build;
363363
```
364364

365+
### Using DNS domain names to identify instances
366+
367+
The connector can be configured to use DNS to look up an instance. This would
368+
allow you to configure your application to connect to a database instance, and
369+
centrally configure which instance in your DNS zone.
370+
371+
#### Configure your DNS Records
372+
373+
Add a DNS TXT record for the Cloud SQL instance to a **private** DNS server
374+
or a private Google Cloud DNS Zone used by your application.
375+
376+
**Note:** You are strongly discouraged from adding DNS records for your
377+
Cloud SQL instances to a public DNS server. This would allow anyone on the
378+
internet to discover the Cloud SQL instance name.
379+
380+
For example: suppose you wanted to use the domain name
381+
`prod-db.mycompany.example.com` to connect to your database instance
382+
`my-project:region:my-instance`. You would create the following DNS record:
383+
384+
- Record type: `TXT`
385+
- Name: `prod-db.mycompany.example.com` – This is the domain name used by the application
386+
- Value: `my-project:region:my-instance` – This is the instance name
387+
388+
#### Creating the R2DBC URL
389+
390+
Add the following connection properties:
391+
392+
| Property | Value |
393+
|---------------|----------------------------------|
394+
| DATABASE_NAME | The name of the database |
395+
| HOST | The domain name for the instance |
396+
| DB_USER | Database username |
397+
| DB_PASS | Database user's password |
398+
399+
R2DBC URL template:
400+
401+
#### MySQL
402+
403+
```
404+
r2dbc:gcp:mysql://<DB_USER>:<DB_PASS>@<HOST>/<DATABASE_NAME>
405+
```
406+
407+
#### MariaDB
408+
409+
```
410+
r2dbc:gcp:mariadb://<DB_USER>:<DB_PASS>@<HOST>/<DATABASE_NAME>
411+
```
412+
413+
#### Postgres
414+
415+
```
416+
r2dbc:gcp:postgres://<DB_USER>:<DB_PASS>@<HOST>/<DATABASE_NAME>
417+
```
418+
419+
##### SQL Server
420+
421+
```
422+
r2dbc:gcp:mssql://<DB_USER>:<DB_PASS>@<HOST>/<DATABASE_NAME>
423+
```
424+
425+
#### Example
426+
427+
```java
428+
// Set up URL parameters
429+
ConnectionFactoryOptions options = ConnectionFactoryOptions.builder()
430+
.option(DRIVER,"gcp")
431+
.option(PROTOCOL,"mysql") // OR "postgres" or "mssql" or "mariadb"
432+
.option(PASSWORD,"<DB_PASSWORD>")
433+
.option(USER,"<DB_USER>")
434+
.option(DATABASE,"<DATABASE_NAME}")
435+
.option(HOST,"<HOST>")
436+
.build();
437+
438+
ConnectionFactory connectionFactory = ConnectionFactories.get(options);
439+
ConnectionPoolConfiguration configuration = ConnectionPoolConfiguration
440+
.builder(connectionFactory)
441+
.build();
442+
443+
ConnectionPool connectionPool = new ConnectionPool(configuration);
444+
```
445+
446+
447+
448+
365449
## Configuration Reference
366450

367451
- See [Configuration Reference](configuration.md)

0 commit comments

Comments
 (0)
Please sign in to comment.