Configure external replicas Stay organized with collections Save and categorize content based on your preferences.
For more information about replication, seeAbout replication in Cloud SQL.
Set up the external replica configuration
Before you begin
Before you start this task, you must have a Cloud SQL instance and anexternal MySQL instance that meets therequirements for externalreplicas.
Source instance requirements
The source instance for an external read replica must be a primary or standaloneinstance. You can't use a Cloud SQL read replica as the source instancefor an external read replica. Read replicas are sometimes recreated from the diskclone of their primary instance and their replication state to an external readreplica can't be maintained by the read replica.
Configure the primary instance
- Go to theCloud SQL Instances page in the Google Cloud console.
- Enable access on the primary instance for the IP address of the external replica.
For information about enabling IP access, seeConfiguring access for IP connections.
- Record the public IP address and the public outgoing IP address of the primary instance for later use. You can find these values on the instance'sOverview page.
- Click the Cloud Shell icon
in the upper right corner. - At the Cloud Shell prompt, use the built-in MySQL client to connect to your primary instance:
gcloudsqlconnectPRIMARY_INSTANCE_NAME\--user=root
- Enter your root password. You should then see the mysql prompt.
- Create a special user for replication and grant replication privileges:
CREATEUSER'REPLICATION_USER'@'%'IDENTIFIEDBY'REPLICATION_USER_PASSWORD';GRANTREPLICATIONSLAVEON*.*TO'REPLICATION_USER'@'%';
- If you are starting with a new database, create the same database and tables on both the primary and replica instances. For example:
CREATEDATABASEtest;USEtest;CREATETABLEreplica_test(idSERIALPRIMARYKEY,datatext);INSERTINTOreplica_test(data)VALUES('apple'),('banana'),('cherry');
- If you already have a database on the primary instance, you must create the same on the replica. To do this, export the database from the primary instance to a Cloud Storage bucket and import it into the replica. Learn more about Exporting data from Cloud SQL to a SQL dump file in Cloud Storage.
Configure the external replica
Warning: This procedure overwrites any data hosted in a MySQL database onthe replica, including users and passwords, with the settings and data from theprimary instance.- On the machine hosting the replica, seed your new external MySQL instance with the export file you created from the primary instance.
For example, the following command loads an exported file named
mydump.sql:mysql --user=root --password < mydump.sql
- Determine the server ID for this replica-primary pair.
The server ID is a numeric value (for example, "3") that must be unique across the external replica configuration (each replica must have a unique server ID).
- Add the following options to the replica's
my.cnfoption file: Note:You must set[mysqld]server-id=[SERVER_ID]gtid_mode=ONenforce_gtid_consistency=ONlog_slave_updates=ONreplicate-ignore-db=mysqlbinlog-format=ROWlog_bin=mysql-binexpire_logs_days=1read_only=ON
gtid_mode=ONfor all externalreplicas in a configuration that includes a Cloud SQL primary instance. Changing thedefault togtid_mode=OFFcauses replication with the external serverto fail.For more information about MySQL replication options, seeReplication and Binary Logging Options.
- Restart the
mysqldprocess to cause the configuration fileto be read. - In a
mysqlclient on the replica, enter the following command:CHANGE MASTER TO MASTER_HOST='MASTER_IP_ADDRESS', MASTER_USER='REPLICATION_USER',MASTER_PASSWORD='REPLICATION_PASSWORD', MASTER_AUTO_POSITION=1;
- Start replication on the replica:
START SLAVE;
Confirm replication status:
SHOW SLAVE STATUS\G;
If you see "Waiting for master to send event", replication is working.
Demote the primary instance of an external replica
When you have a Cloud SQL instance with an external replica, you canreverse the configuration, which causes these changes to happen:
- The external replica becomes the new primary instance.
- The Cloud SQL instance becomes a read replica, replicating from theserver that was previously the external replica (now called the sourcedatabase server).
To reverse the external replica configuration:
Create asource representation instance.
This instance will represent the source database server to the Cloud SQL replica after the demotion operation completes.
gcloud auth loginACCESS_TOKEN="$(gcloud auth print-access-token)"curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ --header 'Content-Type: application/json' \ --data '{ "name": "SOURCE_REPRESENTATION_NAME", "region": "REGION", "databaseVersion": "EXTERNAL_SERVER_DATABASE_VERSION", "onPremisesConfiguration": { "hostPort": "EXTERNAL_SERVER_IP:EXTERNAL_SERVER_PORT" } }' \ -X POST \ https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT-ID/instancesUse the region where you want your Cloud SQL replica to reside.
Start the demotion process.
Because this API call requires you to provide sensitive information, you shoulduse a JSON fileto provide your data to cURL, rather than providing it on the command line.
Create the data file:
{ "demoteMasterContext": { "replicaConfiguration": { "mysqlReplicaConfiguration": { "username": "REPLICATION_USERNAME", "password": "PASSWORD", "caCertificate": "EXTERNAL_SERVER_CA", "clientCertificate": "CLIENT_CERT", "clientKey": "PRIVATE_KEY" } }, "masterInstanceName": "SOURCE_REPRESENTATION_NAME", },}Then, call the API.
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ --header 'Content-Type: application/json' \ --data @PATH_TO_DATA_FILE \ https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT-ID/instances/INSTANCE_NAME/demoteMasterFor more information about your options for SSL/TLS, seeSSL/TLS options. For more information about the properties used by the
replicaConfigurationobject, see Replicating from an External Server.Wait for the external replica to complete all pending transactions from the primary instance.
When the replica is caught up, the
SHOW SLAVE STATUScommand will showSeconds Behind Masteras 0, and theExecuted_Gtid_Setvalue will be identical between the external replica and the Cloud SQL primary.Use the
mysqlclient to stop replication on the external replica:STOP SLAVERESET SLAVE ALL
Wait for the Cloud SQL instance to start replicating from the external server, which is now the source database server.
Running the
SHOW SLAVE STATUScommand on the Cloud SQL instance provides replication status.- When the Cloud SQL instance is successfully replicating from the source database server, set the
read_onlyflag on the source database server tooffand update your applications to point to the source database server.
gtid_mode is set toOFF in the external primary, these replicas are not supported after the demotion. You can delete such replicas before you demote your instance or changegtid_mode toON in your external primary before you demote.Troubleshoot
| Issue | Troubleshooting |
|---|---|
Error message:The slave is connecting ... master has purged binary logs containing GTIDs that the slave requires. | The primary Cloud SQL instance has automatic backups and binary logs and point-in-time recovery is enabled, so it should have enough logs for the replica to be able to catch up. However, in this case although the binary logs exist, the replica doesn't know which row to start reading from. Create a new dump file using the correct flag settings, and configure the external replica using that file
|
What's next
- Learn how tomanage replicas.
- Learn aboutrequirements and best practices for the external replica configuration.
- Learn more aboutMySQL replication.
- Learn more aboutreplication options.
- Learn more aboutchecking replication status.
- Learn more aboutreplicating from an external server.
Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2025-12-17 UTC.