Configuring public IP connectivity

This page describes how to configure public IP connectivity for a Cloud SQL instance.

Introduction

You can configure your Cloud SQL instance to have a public IPv4 address, and to accept connections from specific IP addresses or a range of addresses by adding authorized addresses to your instance.

For help with connecting an administration client to your instance over an IP connection, see Connecting sqlcmd Client using IP addresses.

If you configure your instance to accept connections using its public IP address, also configure it to use SSL to keep your data secure. For more information, see Configure SSL for Instances.

To configure your instance with an IP address that is not exposed to the public internet, see Configuring Private IP Connectivity.

Enabling public IP and adding an authorized address or address range

To enable public IP and add an authorized address:

When you enable public IP for your instance, it is configured with a public, static IPv4 address.

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  2. Click the instance name to open its Instance details page.
  3. Select the Connections tab.
  4. Select the Public IP checkbox.
  5. Click Add network.
  6. In the Network field, enter the IP address or address range you want to allow connections from.

    Use CIDR notation.

  7. Optionally, enter a name for this entry.
  8. Click Done.
  9. Click Save to update the instance.

gcloud

  1. If you haven't already, add an IPv4 address to the instance:
    gcloud sql instances patch [INSTANCE_NAME] --assign-ip
    
  2. Show all existing authorized addresses by describing the instance:
    gcloud sql instances describe [INSTANCE_NAME]
    

    Look for authorizedNetwork entries under ipConfiguration, and note any authorized addresses you want to keep.

  3. Update the authorized network list, including all addresses you want included.
    gcloud sql instances patch [INSTANCE_NAME] --authorized-networks=[IP_ADDR1],[IP_ADDR2]...
    

    Use CIDR notation.

  4. Confirm your changes:
    gcloud sql instances describe [INSTANCE_NAME]
    

REST v1beta4

  1. Show all existing authorized addresses by describing the instance:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • machine-type The instance machine type
    • zone The instance zone

    HTTP method and URL:

    GET  https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id?fields=settings

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  2. Update the instance, including all addresses you want set on the instance:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • network_range_1 An authorized ip address or range
    • network_range_2 Another authorized ip address or range

    HTTP method and URL:

    PATCH https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

    Request JSON body:

    {
      "settings":
      {
        "ipConfiguration":
        {
          "authorizedNetworks":
            [{"value": "network_range_1"}, {"value": network_range_2"}]
        }
      }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

    Use CIDR notation.

  3. Confirm your changes:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • ip-address1: The CIDR form of the first IP address
    • ip-address-name1: The name of the first IP address
    • ip-address2: The CIDR form of the second IP address
    • ip-address-name2: The name of the second IP address
    • machine-type The instance machine type
    • zone The instance zone

    HTTP method and URL:

    GET  https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id?fields=settings

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

Removing an authorized address or address range

To remove an authorized address:

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  2. Click the instance name to open its Instance details page.
  3. Select the Connections tab.
  4. Click the delete icon Delete. for the address you want to delete.
  5. Click Save to update the instance.

gcloud

  1. Show all existing authorized addresses by describing the instance:
    gcloud sql instances describe [INSTANCE_NAME]
    

    Look for authorizedNetwork entries under ipConfiguration, and note any authorized addresses you want to keep.

  2. Update the authorized network list, dropping off any addresses you want to remove.
    gcloud sql instances patch [INSTANCE_NAME] --authorized-networks=[IP_ADDR1],[IP_ADDR2]...
    
  3. Confirm your changes:
    gcloud sql instances describe [INSTANCE_NAME]
    

REST v1beta4

  1. Show all existing authorized addresses by describing the instance:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • ip-address1: The CIDR form of the first IP address
    • ip-address-name1: The name of the first IP address
    • ip-address2: The CIDR form of the second IP address
    • ip-address-name2: The name of the second IP address
    • machine-type The instance machine type
    • zone The instance zone

    HTTP method and URL:

    GET  https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id?fields=settings

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  2. Update the instance, by including all the addresses you want to keep and dropping off any addresses you want to remove:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • network_range_1 The authorized IP address or network range to remove

    HTTP method and URL:

    PATCH https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

    Request JSON body:

    {
      "settings":
      {
        "ipConfiguration":
        {
          "authorizedNetworks":
            [{"value": "network_range_1"}]
        }
      }
    }
    
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  3. Confirm your changes:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • ip-address: The CIDR form of the IP address
    • ip-address-name: The name of the IP address
    • machine-type The instance machine type
    • zone The instance zone

    HTTP method and URL:

    GET  https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id?fields=settings

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

Configuring an instance to refuse all public IP connections

To configure an instance to refuse all public IP connections:

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  2. Click the instance name to open its Instance details page.
  3. Select the Connections tab.
  4. Click the delete icon Delete. for all authorized addresses.
  5. Click Save to update the instance.

gcloud

  1. Clear the authorized address list:
    gcloud sql instances patch [INSTANCE_NAME] --clear-authorized-networks
    
  2. Confirm your changes:
    gcloud sql instances describe [INSTANCE_NAME]
    

REST v1beta4

  1. Show all existing authorized addresses by describing the instance:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • ip-address1: The CIDR form of the first IP address
    • ip-address-name1: The name of the first IP address
    • ip-address2: The CIDR form of the second IP address
    • ip-address-name2: The name of the second IP address
    • machine-type The instance machine type
    • zone The instance zone

    HTTP method and URL:

    GET  https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id?fields=settings

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  2. Update the instance with an empty address list:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID

    HTTP method and URL:

    PATCH https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

    Request JSON body:

    {
      "settings":
      {
        "ipConfiguration":
        {
          "authorizedNetworks": []
        }
      }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  3. Confirm your changes:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • machine-type The instance machine type
    • zone The instance zone

    HTTP method and URL:

    GET  https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id?fields=settings

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

Disabling public IP

You can disable public IP, but only if your instance is also configured to use Private IP. To enable private IP, see Configuring an existing instance to use private IP.

To disable public IP:

Console

  1. Go to the Cloud SQL Instances page in the Google Cloud Console.

    Go to the Cloud SQL Instances page

  2. Click the instance name to open its Instance details page.
  3. Select the Connections tab.
  4. Deselect the Public IP checkbox.
  5. Click Save to update the instance.

gcloud

  1. Update the instance:
    gcloud sql instances patch [INSTANCE_NAME] --no-assign-ip
    
  2. Confirm your changes:
    gcloud sql instances describe [INSTANCE_NAME]
    

REST v1beta4

  1. Show all existing authorized addresses by describing the instance:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • ip-address1: The CIDR form of the first IP address
    • ip-address-name1: The name of the first IP address
    • ip-address2: The CIDR form of the second IP address
    • ip-address-name2: The name of the second IP address
    • machine-type The instance machine type
    • zone The instance zone

    HTTP method and URL:

    GET  https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id?fields=settings

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  2. Update the instance:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID

    HTTP method and URL:

    PATCH https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id

    Request JSON body:

    {
      "settings":
      {
        "ipConfiguration": {"ipv4Enabled": false}
      }
    }
    

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

  3. Confirm your changes:

    Before using any of the request data below, make the following replacements:

    • project-id: The project ID
    • instance-id: The instance ID
    • machine-type The instance machine type
    • zone The instance zone

    HTTP method and URL:

    GET  https://www.googleapis.com/sql/v1beta4/projects/project-id/instances/instance-id?fields=settings

    To send your request, expand one of these options:

    You should receive a JSON response similar to the following:

Troubleshooting

Click the links in the table for details:

For this problem... The issue might be... Try this...
Unauthorized to connect errors. There can be many root causes. See these things to try.
Network association failed. Service Networking API is not enabled in the project. Enable the Service Networking API in the project.
Remaining connection slots are reserved. The maximum number of connections has been reached. Increase the max_connections flag.
Set Service Networking service account as servicenetworking.serviceAgent role on consumer project. Networking permissions on the service account are missing or incorrect. Disable and re-enable the Service Networking API.
error x509: certificate is not valid for any names, but wanted to match project-name:db-name. Known issue: The Cloud SQL Proxy Dialer is not compatible with Go 1.15 at this time. Until fixed, see this discussion on GitHub, which includes a workaround.

Aborted connection

You see the error message Got an error reading communication packets , or Aborted connection xxx to db: DB_NAME.

The issue might be

  • Networking instability.
  • No response to TCP keep-alive commands (either the client or the server is not responsive, possibly overloaded).
  • The database engine connection lifetime was exceeded and the server ends the connection.

Things to try

Applications must tolerate network failures and follow best practices such as connection pooling and retrying. Most connection poolers catch these errors where possible. Otherwise the application must either retry or fail gracefully.

For connection retry, we recommend the following methods:

  1. Exponential backoff. Increase the time interval between each retry, exponentially.
  2. Add randomized backoff also.
Combining these methods helps reduce throttling.


Unauthorized to connect

You see the error message Unauthorized to connect.

The issue might be

There can be many causes, as authorization occurs at many levels.

  • At the database level, the database user must exist and its password must match.
  • At the project level, the user may lack the correct IAM permissions.
  • At the Cloud SQL level, the root cause can depend on how you connect to your instance. If you are connecting directly to an instance through the public IP, the connection's source IP must be in the authorized network of the instance.

    Private IP connectivity is allowed by default, except when you are connecting from a non-RFC 1918 address. Non-RFC 1918 client addresses must be configured as authorized networks.

    Cloud SQL doesn't learn Non-RFC 1918 subnet routes from your VPC by default. You need to update the network peering to Cloud SQL to export any Non-RFC 1918 routes. For example:

    gcloud compute networks peerings update cloudsql-sqlserver-googleapis-com --network=NETWORK --export-subnet-routes-with-public-ip --project=PROJECT
    

    If you are connecting through the Cloud SQL Proxy, ensure that the IAM permissions are set up correctly.

  • At the network level, if the Cloud SQL instance is using public IP, the connection's source IP must be in an authorized network.

Things to try

  • Check the username and password.
  • Check the user's IAM roles and permissions.
  • If using public IP, make sure the source is in the authorized networks.

Network association failed

You see the error message Error: Network association failed due to the following error: set Service Networking service account as servicenetworking.serviceAgent role on consumer project.

The issue might be

The Service Networking API is not enabled in the project.

Things to try

Enable the Service Networking API in your project. If you see this error when you are trying to assign a private IP address to a Cloud SQL instance, and you are using a shared VPC, you also need to enable the Service Networking API for the host project.


Remaining connection slots are reserved

You see the error message FATAL: remaining connection slots are reserved for non-replication superuser connections.

The issue might be

The maximum number of connections has been reached.

Things to try

Edit the max_connections flag value. Increasing max_connections above a certain value can result in losing SLA support.


Set Service Networking service account as servicenetworking.serviceAgent role on consumer project

You see the error message set Service Networking service account as servicenetworking.serviceAgent role on consumer project..

The issue might be

User or service account permissions are not correct. This can happen during automated setup scripts, such as a Terraform configuration script.

Things to try

To repair service permissions, disable the Service Networking API , wait five minutes and then re-enable it.

You can also try using these gcloud commands to assign the role to the project

gcloud beta services identity create --service=servicenetworking.googleapis.com --project=project-id
gcloud projects add-iam-policy-binding project-id --member="service-account-prefix@service-networking.iam.gserviceaccount.com" --role="roles/servicenetworking.serviceAgent"

Error x509: certificate is not valid for any names

You see the error message error x509: certificate is not valid for any names, but wanted to match project-name:db-name

The issue might be ...

Known issue: The Cloud SQL Proxy Dialer is not compatible with Go 1.15 at this time.

Things to try

Until the bug is fixed, see this discussion on GitHub, which includes a workaround.


What's next