How to Create a Read Replica in Cloud SQL on GCP
To create a read replica in
Cloud SQL, use the gcloud sql instances create command with the --master-instance-name flag pointing to the primary instance. This sets up a read-only copy that replicates data from the primary database automatically.Syntax
Use the gcloud sql instances create command with these key parts:
INSTANCE_NAME: The name for your new read replica.--master-instance-name=PRIMARY_INSTANCE: The name of the primary Cloud SQL instance to replicate.--region=REGION: The region where the replica will be created.
This command creates a read-only replica that syncs data from the primary instance.
bash
gcloud sql instances create INSTANCE_NAME \ --master-instance-name=PRIMARY_INSTANCE \ --region=REGION
Example
This example creates a read replica named my-read-replica in the us-central1 region, replicating from the primary instance my-primary-instance.
bash
gcloud sql instances create my-read-replica \ --master-instance-name=my-primary-instance \ --region=us-central1
Output
Created [https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/my-read-replica].
Common Pitfalls
Common mistakes when creating read replicas include:
- Not specifying the
--master-instance-name, which causes the replica creation to fail. - Choosing a region different from the primary without considering latency and cost.
- Trying to create a read replica for unsupported database versions or instance types.
- Not enabling binary logging on the primary instance, which is required for replication.
Always check that the primary instance has binary logging enabled and that the replica is compatible.
bash
## Wrong: Missing master-instance-name gcloud sql instances create my-read-replica --region=us-central1 ## Right: Include master-instance-name gcloud sql instances create my-read-replica --master-instance-name=my-primary-instance --region=us-central1
Quick Reference
Tips for creating Cloud SQL read replicas:
- Use
gcloud sql instances createwith--master-instance-name. - Ensure the primary instance has binary logging enabled.
- Choose the replica region wisely for performance and cost.
- Read replicas are read-only and help scale read traffic.
Key Takeaways
Use the gcloud command with --master-instance-name to create a read replica.
The primary instance must have binary logging enabled for replication.
Read replicas are read-only copies that improve read scalability.
Choose the replica region carefully to balance latency and cost.
Always verify compatibility between primary and replica instance types.