This page describes how to manage read replicas. These operations include disabling and enabling replication, promoting a replica, configuring parallel replication, and checking the replication status.
For more information about how replication works, see Replication in Cloud SQL .
This page applies to replicas of a Cloud SQL instance. To set up a Cloud SQL instance to act as a publisher to an external subscriber, see Configure external replicas .
Disable replication
By default, a replica starts with replication enabled. However, you can disable replication, for example, to debug or analyze the state of an instance. When you are ready, you explicitly re-enable replication. Disabling or re-enabling replication doesn't restart the replica instance.
Disabling replication does not stop the replica instance; it becomes a read-only instance that is no longer replicating from its primary instance. You continue to be charged for the instance. On the disabled replica, you can re-enable replication, delete the replica, or promote the replica to a stand-alone instance.
When you disable the replication for an extended period of time, your disk storage requirements might increase. For example, your instance might accumulate transactional logs to let you resume replication when you re-enable replication. To avoid increasing disk storage requirements, instead of disabling replication for an extended period of time, consider promoting the replica or creating a clone of the primary instance.
To disable replication:
Console
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select a replica instance by clicking its name.
- Click Disable replication in the button bar.
- Click OK .
gcloud
gcloud sql instances patch REPLICA_NAME \ --no-enable-database-replication
REST v1
To execute this cURL command at a command line prompt, you acquire an access token by using the gcloud auth print-access-token command. You can also use the APIs Explorer on the Instances:patch page to send the REST API request.
Before using any of the request data, make the following replacements:
- project-id : The project ID
- replica-name : The name of the replica instance
HTTP method and URL:
PATCH https://sqladmin.googleapis.com/v1/projects/ project-id /instances/ replica-name
Request JSON body:
{ "settings": { "databaseReplicationEnabled": "False" } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
REST v1beta4
To execute this cURL command at a command line prompt, you acquire an access token by using the gcloud auth print-access-token command. You can also use the APIs Explorer on the Instances:patch page to send the REST API request.
Before using any of the request data, make the following replacements:
- project-id : The project ID
- replica-name : The name of the replica instance
HTTP method and URL:
PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/ project-id /instances/ replica-name
Request JSON body:
{ "settings": { "databaseReplicationEnabled": "False" } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
Enable replication
If a replica has not been replicating for a long time, it will take longer for it to catch up to the primary instance. In this case, delete the replica and create a new one.
To enable replication:
Console
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select a replica instance by clicking its name.
- Click Enable replication .
- Click Ok .
gcloud
gcloud sql instances patch REPLICA_NAME \ --enable-database-replication
REST v1
To execute this cURL command at a command line prompt, you acquire an access token by using the gcloud auth print-access-token command. You can also use the APIs Explorer on the Instances:patch page to send the REST API request.
Before using any of the request data, make the following replacements:
- project-id : The project ID
- replica-name : The name of the replica instance
HTTP method and URL:
PATCH https://sqladmin.googleapis.com/v1/projects/ project-id /instances/ replica-name
Request JSON body:
{ "settings": { "databaseReplicationEnabled": "True" } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
REST v1beta4
To execute this cURL command at a command line prompt, you acquire an access token by using the gcloud auth print-access-token command. You can also use the APIs Explorer on the Instances:patch page to send the REST API request.
Before using any of the request data, make the following replacements:
- project-id : The project ID
- replica-name : The name of the replica instance
HTTP method and URL:
PATCH https://sqladmin.googleapis.com/sql/v1beta4/projects/ project-id /instances/ replica-name
Request JSON body:
{ "settings": { "databaseReplicationEnabled": "True" } }
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
Promote a replica
Promoting a read replica stops replication and converts the instance to a standalone Cloud SQL primary instance with read and write capabilities.
When promoted, read replicas are automatically configured with backups, but they aren't automatically configured as high availability (HA) instances. You can enable high availability after promoting the replica just as you would for any non-replica instance. Configuring a read replica for high availability is done the same way as for a primary instance. Learn more about configuring the instance for high availability .
Before promoting a read replica, if the primary is still available and serving clients, you should do the following:
- Stop all writes to the primary instance.
- Check the replication status of the replica. One option for doing so is the Always On Availability Group dashboard in SQL Server Management Studio (SSMS).
- Verify that the replica is replicating, and then check
the replication lag, for example as reported by the
seconds_behind_master
metric.
Otherwise, a newly promoted instance may be missing some transactions that were committed to the primary instance.
To promote a replica to a standalone instance:
Console
-
In the Google Cloud console, go to the Cloud SQL Instances page.
- Select a replica instance by clicking its name.
- Click Promote replica .
- Click Ok .
gcloud
gcloud sql instances promote-replica REPLICA_NAME
REST v1
To execute this cURL command at a command line prompt, you acquire an access token by using the gcloud auth print-access-token command. You can also use the APIs Explorer on the Instances:promoteReplica page to send the REST API request.
Before using any of the request data, make the following replacements:
- project-id : The project ID
- replica-name : The name of the replica instance
HTTP method and URL:
POST https://sqladmin.googleapis.com/v1/projects/ project-id /instances/ replica-name /promoteReplica
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
REST v1beta4
To execute this cURL command at a command line prompt, you acquire an access token by using the gcloud auth print-access-token command. You can also use the APIs Explorer on the Instances:promoteReplica page to send the REST API request.
Before using any of the request data, make the following replacements:
- project-id : The project ID
- replica-name : The name of the replica instance
HTTP method and URL:
POST https://sqladmin.googleapis.com/sql/v1beta4/projects/ project-id /instances/ replica-name /promoteReplica
To send your request, expand one of these options:
You should receive a JSON response similar to the following:
Confirm that the promoted instance is configured correctly. In particular, consider configuring the instance for high availability if needed.
Check replication status
Currently, you should use T-SQL queries or SSMS to monitor replication status. For more information, see the following:Troubleshoot
First, check that the value of the max_connections
flag is
greater than or equal to the value on the primary.
If the max_connections
flag is set appropriately, inspect the logs
in
Cloud Logging to find the actual error.
If the error is: set Service Networking service account as
servicenetworking.serviceAgent role on consumer project
, then disable
and re-enable the Service
Networking API
. This action creates the service account necessary
to continue with the process.
Restart the replica instance to reclaim the temporary memory space.
Edit the instance
to enable automatic storage increase
.
- Slow queries on the replica. Find and fix them.
- Queries like
DELETE ... WHERE field < 50000000
cause replication lag with row-based replication since a huge number of updates are piled up on the replica.
Some possible solutions include:
- Edit the instance to increase the size of the replica.
- Reduce the load on the database.
- Send read traffic to the read replica.
- Index the tables.
- Identify and fix slow write queries.
- Recreate the replica.
Recreate the replica after stopping all running queries.
What's next
- Learn how to create a read replica .
- Learn more about requirements and best practices for replication .