@@ -563,6 +563,123 @@ Properties connProps = new Properties();
563
563
connProps. setProperty(" cloudSqlRefreshStrategy" , " lazy" );
564
564
```
565
565
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
+
566
683
## Configuration Reference
567
684
568
685
- See [ Configuration Reference] ( configuration.md )
0 commit comments