您可以設定 Cloud SQL 執行個體,讓應用程式使用貴機構管理的自訂 DNS 名稱連線。如要使用自訂 DNS 名稱連線至 Cloud SQL 執行個體,而非使用 IP 位址,請設定自訂主體替代名稱 (SAN)。建立或更新執行個體時,您可以設定自訂 SAN。
將自訂 DNS 名稱做為自訂 SAN 設定新增至執行個體時,Cloud SQL 會將自訂 DNS 名稱插入執行個體伺服器憑證的 SAN 欄位。這項自訂功能可讓您安全地使用自訂 DNS 名稱進行主機名稱驗證。您可以在自訂 SAN 設定中,新增最多三個自訂 DNS 名稱 (以半形逗號分隔)。基於安全考量,只有在使用 CUSTOMER_MANAGED_CAS_CA 將執行個體設定為伺服器 CA 模式時,才能使用自訂 SAN 設定。
resource "google_sql_database_instance" "default" {
name = "sqlserver-instance"
region = "asia-northeast1"
database_version = "SQLSERVER_2022_STANDARD"
root_password = "INSERT-PASSWORD-HERE"
settings {
tier = "db-custom-2-7680"
ip_configuration {
# The following server CA mode lets the instance use customer-managed CAS CA to issue server certificates.
# https://cloud.google.com/sql/docs/sqlserver/admin-api/rest/v1beta4/instances#ipconfiguration
server_ca_mode = "CUSTOMER_MANAGED_CAS_CA"
server_ca_pool = google_privateca_ca_pool.default.id
custom_subject_alternative_names = ["customSan.test.com"]
}
}
}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Set up a custom DNS name for a Cloud SQL instance\n\n\u003cbr /\u003e\n\n[MySQL](/sql/docs/mysql/custom-dns-name \"View this page for the MySQL database engine\") \\| [PostgreSQL](/sql/docs/postgres/custom-dns-name \"View this page for the PostgreSQL database engine\") \\| SQL Server\n\n\u003cbr /\u003e\n\nThis page describes how to set up a custom domain name system\n(DNS) name for your Cloud SQL instance.\n\nOverview\n--------\n\nYou can configure a Cloud SQL instance so that your applications can\nconnect using a custom DNS name managed by your organization.\nIf you want to use a custom DNS name to connect to a Cloud SQL\ninstance instead of using an IP address, then configure a custom\nsubject alternative name (SAN). You can configure a custom SAN\nwhen you create or update an instance.\n\nWhen you add a custom DNS name as a custom SAN configuration to your instance,\nCloud SQL inserts the custom DNS name\nto the SAN field of the server certificate of the instance. This customization\nlets you use the custom DNS name with [hostname validation](/sql/docs/sqlserver/configure-ssl-verification#server-identity-verification) securely.\nYou can add a comma-separated list of up to three custom DNS names to the\ncustom SAN configuration. For security reasons, you can use a custom SAN configuration\nonly for instances that you\n[configure with `CUSTOMER_MANAGED_CAS_CA`](/sql/docs/sqlserver/customer-managed-ca)\nas the server CA mode.\n\nAfter you configure the custom DNS name for your instance, you can use that\ncustom DNS name to connect your database clients or applications, including\n[Cloud SQL Language Connectors](/sql/docs/sqlserver/language-connectors) and\nthe [Cloud SQL Auth Proxy](/sql/docs/sqlserver/sql-proxy).\n\n### Workflow\n\nTo set up a custom DNS name for an instance, do the following:\n\n1. Create an instance configured with customer-managed CA.\n2. Add custom SAN values to the instance. The custom SAN values are inserted into the SAN field of the server certificate of the instance.\n3. Determine the IP address of the instance.\n4. Create custom DNS records for your instance.\n5. Connect to the instance using the custom DNS name.\n\nBefore you begin\n----------------\n\nBefore you set up a custom DNS name for your instance, make sure you have\nthe [required roles and permissions](/sql/docs/sqlserver/customer-managed-ca#required-roles).\n\nIf you are creating a new Cloud SQL instance, then to use custom SAN,\nyour instance must also use a\n[customer-managed certificate authority (CA)](/sql/docs/sqlserver/customer-managed-ca) for its\nserver CA mode.\n\nCreate an instance with custom SAN values\n-----------------------------------------\n\nTo create an instance with custom SAN values, use the following [gcloud sql instances create](/sdk/gcloud/reference/sql/instances/create) command: \n\n### gcloud\n\n```bash\ngcloud sql instances create \"\u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e\" \\\n --database-version=DATABASE_VERSION \\\n --project=PROJECT_ID \\\n --region=REGION \\\n --server-ca-mode=CUSTOMER_MANAGED_CAS_CA \\\n --server-ca-pool=projects/PROJECT_ID_CAS/locations/REGION/caPools/CA_POOL_ID\n --custom-subject-alternative-names=CUSTOM_DNS_NAME\n```\n\nMake the following replacements:\n\n- \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e with the name of the Cloud SQL instance that you want to create.\n- \u003cvar translate=\"no\"\u003eDATABASE_VERSION\u003c/var\u003e with the [`enum` of the version](/sql/docs/sqlserver/admin-api/rest/v1/SqlDatabaseVersion) of the Cloud SQL instance that you want to create.\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with the ID of the project where you plan to create your Cloud SQL instances.\n- \u003cvar translate=\"no\"\u003ePROJECT_ID_CAS\u003c/var\u003e with the ID of the project where you created your \u003cvar translate=\"no\"\u003eCA_POOL_ID\u003c/var\u003e. This project might be the same or different from where you want to create your Cloud SQL instance.\n- \u003cvar translate=\"no\"\u003eREGION\u003c/var\u003e with the region where you created the CA pool. You must create your instance in the same region as the CA pool.\n- \u003cvar translate=\"no\"\u003eCA_POOL_ID\u003c/var\u003e with the ID of the CA pool that you created.\n- \u003cvar translate=\"no\"\u003eCUSTOM_DNS_NAME\u003c/var\u003e with up to three custom DNS names, comma-separated with no spaces in between the values. For example, `develop.example.com,test.example.com,production.example.com`.\n\nAdd or update custom SAN values for an instance\n-----------------------------------------------\n\nTo add or update custom SAN values for an existing instance,\ndo the following: \n\n### gcloud\n\n```bash\ngcloud sql instances patch INSTANCE_NAME \\\n --custom-subject-alternative-names=CUSTOM_DNS_NAME\n```\nCaution: If you're updating an instance that already has custom SAN values, then specify the existing custom SAN values or the existing values will be replaced.\n\nMake the following replacements:\n\n- \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e with the name of the Cloud SQL instance that you want to update.\n- \u003cvar translate=\"no\"\u003eCUSTOM_DNS_NAME\u003c/var\u003e with up to three custom DNS names, comma-separated with no spaces in between the values. For example, `develop.example.com,new-test.example.com,production.example.com`.\n\nClear all custom SAN values from an instance\n--------------------------------------------\n\nTo clear all custom SAN values from an instance,\ndo the following: \n\n### gcloud\n\n```bash\ngcloud sql instances patch INSTANCE_NAME \\\n --clear-custom-subject-alternative-names\n```\n\nReplace \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e with the name of the Cloud SQL\ninstance that you want to update.\n\nCreate custom DNS records\n-------------------------\n\nBefore you can connect to the instance using the custom DNS name with\nyour clients and applications, set up the mapping between the DNS name\nand the IP address. This mapping\nis known as DNS resolution. If you are connecting privately, then\n[create the DNS record in a private DNS zone](/dns/docs/set-up-dns-records-domain-name)\nin the corresponding Virtual Private Cloud (VPC)\nnetwork.\n\nTo create custom DNS records for your instance, do the following:\n\n1. Retrieve the IP address for your instance. Run the following command:\n\n ```bash\n gcloud sql instances describe INSTANCE_NAME \\\n --project=PROJECT_ID\n ```\n\n Make the following replacements:\n - \u003cvar translate=\"no\"\u003eINSTANCE_NAME\u003c/var\u003e: the name of the Cloud SQL instance\n - \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the ID or [project number](/resource-manager/docs/creating-managing-projects#identifying_projects) of the Google Cloud project that contains the instance\n2. In the response, look for the\n `ipAddresses:` field.\n\n For example: \n\n ipAddresses:\n - ipAddress: 192.0.2.4\n type: PRIVATE\n\n You can retrieve the private IP address for an instance configured for\n [private services access](/sql/docs/sqlserver/configure-private-services-access), [Private Service Connect](/sql/docs/sqlserver/configure-private-service-connect), or [both](/sql/docs/sqlserver/configure-private-services-access-and-private-service-connect).\n | **Warning**: For security reasons, we don't recommend setting up custom DNS records for the public IP address of an instance.\n3. Add two DNS records to a DNS zone used by your application.\n You can use a DNS server that you manage yourself or\n the [Cloud DNS private zone](/dns/docs/zones#create-private-zone) used by your application.\n\n - Create an `A` record for the IP address of the instance\n - Create a `TXT` record containing the instance connection name.\n\n Standard database clients use the `A` record to look up the IP address\n to make the connection. Cloud SQL Language Connectors and the Cloud SQL Auth Proxy\n use the `TXT` record to look up the instance connection name.\n\n For example, the connection string for your instance is `my-project:region:my-instance`,\n but you want to connect using the DNS name\n `prod-db.mycompany.example.com`.\n If the IP address of the instance on your company network is `192.0.2.4`,\n then create the following DNS records: \n\n Record type: A\n Name: prod-db.mycompany.example.com\n Value: 192.0.2.4\n TTL: 600\n\n Record type: TXT\n Name: prod-db.mycompany.example.com\n Value: my-project:region:my-instance\n TTL: 600\n\nConnect to an instance using a custom DNS name\n----------------------------------------------\n\nWhen you connect to the Cloud SQL for SQL Server instance, configure the custom DNS name as the hostname. Then enable server identity verification verification by specifying the `-N` flag for `sqlcmd` or by selecting the **Encrypt Connection/Encryption** option of SSMS.\n\n\u003cbr /\u003e\n\nOther SQL Server drivers have similar flags or configurations.\n\n\nIf you're connecting to a private Cloud SQL instance\nfrom an external network, then you must configure your Cloud SQL instance\nand its Virtual Private Cloud (VPC) network to permit external\nconnections and to let Cloud DNS communicate with on-premises\nsystems. For more information about this configuration, see [Connect from an external source](/sql/docs/mysql/configure-private-ip#vpn) and [Configure DNS for on-premises systems](/vpc/docs/configure-private-google-access-hybrid#config-dns-onpremises).\n\nLimitations\n-----------\n\n- You can't add more than three DNS names as custom SAN values to the server certificate of a Cloud SQL instance.\n- The maximum length of a DNS name that you can add to the certificate as a custom SAN value for an instance is 253 characters.\n- The DNS names that you add to the certificate for an instance can't have wildcard characters (`*`) or trailing dots. For example, `test.example.com.` isn't allowed.\n- The DNS names must be valid, as specified by [RFC 1034](https://datatracker.ietf.org/doc/html/rfc1034).\n- You can specify custom SAN values for `CUSTOMER_MANAGED_CAS_CA` instances only.\n\nWhat's next\n-----------\n\n- [Manage](/sql/docs/sqlserver/manage-ssl-instance) SSL/TLS certificates on your Cloud SQL instance.\n- Learn more about [how encryption is handled in Google Cloud](/security/encryption-in-transit)."]]