Use a dump file to set up replication from external databases Stay organized with collections Save and categorize content based on your preferences.
This page describes the process for setting up replication when you have a dumpfile that you created from your external server.
You must complete all the steps on this page. When finished, you can administerand monitor the source representation instance the same way as you would anyother Cloud SQL instance.
Before you begin
Before you start, you should haveconfigured the external server,created thesource representation instance,and set up theCloud SQL replica.
Update permissions for the replication user
The replication user on the external server is configured toaccept connections from any host (%). You should update this user accountso that it can only be used with the Cloud SQL replica.Open a terminal on the external server and enter these commands:
mysql Client
UPDATEmysql.userSETHost='NEW_HOST'WHEREHost='OLD_HOST'ANDUser='USERNAME';GRANTREPLICATIONSLAVE,EXECUTEON*.*TO'GCP_USERNAME'@'HOST';FLUSHPRIVILEGES;
example
UPDATEmysql.userSETHost='192.0.2.0'WHEREHost='%'ANDUser='replicationUser';GRANTREPLICATIONSLAVE,EXECUTEON*.*TO'gcp_user'@'gmail.com';FLUSHPRIVILEGES;
| Property | Description |
|---|---|
| NEW_HOST | Specify theoutgoing IP of theCloud SQL replica. |
| OLD_HOST | The current value assigned toHost that you want tochange. |
| USERNAME | The replication user account on the external server. |
| GCP_USERNAME | The username for the Google Cloud Platform (GCP) user account. |
| HOST | The hostname for the Google Cloud Platform (GCP) user account. |
Verify your replication settings
After your setup is complete, ensure that the Cloud SQL replicacan replicate from the external server.
First, ensure that your external sync settings are correct. To do this,use the commands below to verify:
- Connectivity between the Cloud SQL replica and external server
- Replication user privileges
- Version compatibility
- The Cloud SQL replica is not already replicating
- Binlogs are enabled on the external server
- A global transaction identifier (GTID) is enabled
Open a terminal and enter these commands to verify external syncsettings are correct:
curl
gcloud auth loginACCESS_TOKEN="$(gcloud auth print-access-token)"curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \ --header 'Content-Type: application/json' \ --data '{ "syncMode": "SYNC_MODE", "skipVerification": "SKIP_VERIFICATION" }' \ -X POST \ https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/REPLICA_INSTANCE/verifyExternalSyncSettingsexample
gcloudauthloginACCESS_TOKEN="$(gcloud auth print-access-token)"curl--header "Authorization: Bearer ${ACCESS_TOKEN}" \--header 'Content-Type: application/json' \--data '{"syncMode":"online",}'\-XPOST\https://sqladmin.googleapis.com/sql/v1beta4/projects/myproject/instances/myreplica/verifyExternalSyncSettings| Property | Description |
|---|---|
| SYNC_MODE | verifyExternalSyncSettings verifies that youcan keep the Cloud SQL replica andexternal serverin sync after replication is set up. Sync modesincludeEXTERNAL_SYNC_MODE_UNSPECIFIED,ONLINE, andOFFLINE. |
| SKIP_VERIFICATION | Whether or not to skip the built-in verificationstep before syncing your data. Only recommendedif you have alreadyverified your replication settings. |
| PROJECT_ID | The ID of your project in Google Cloud. |
| REPLICA_INSTANCE | The ID of your Cloud SQL replica. |
Export your database to a Cloud Storage bucket
You can populate a Cloud SQL replica with amysqldump file located in aCloud Storage bucket. These conditions apply:
- You must use the
mysqldumputility bundled with MySQL. - While
mysqldumpis running, do not perform anyDDL operations on the external server.Doing so could cause inconsistencies in the export file.
To export your database to a Cloud Storage bucket, follow these steps:
- In Google Cloud,create a Cloud Storage bucket.
- Open a terminal using a client that connects to the external database serverand run the following command.
mysqldump
mysqldump\--host=EXTERNAL_HOST\--port=EXTERNAL_PORT\--user=USERNAME\--password=PASSWORD\--databases=DATABASE_LIST\--hex-blob\SOURCE_DATA\--no-autocommit\--default-character-set=utf8mb4\--single-transaction\--set-gtid-purged=on\ADD_DROP_TABLE\ROUTINES\COMPRESS\GZIP\|gcloudstoragecp-gs://BUCKET/DUMP_FILENAME
example
mysqldump\--host=192.0.2.1\--port=3306\--user=replicationUser\--password\--databasesguestbookjournal\--hex-blob\--master-data=1\--no-autocommit\--default-character-set=utf8mb4\--single-transaction\--compress\|gzip\|gcloudstoragecp-gs://replica-bucket/external-database.sql.gz
| Property | Description |
|---|---|
| EXTERNAL_HOST | The IPv4 or DNS address for the external server. |
| EXTERNAL_PORT | The port for the external server.If the external server is hosted on Cloud SQL,this is3306. |
| USERNAME | The name of the replication user account or useraccount on the external server that has database readpermissions. |
| PASSWORD | Replication user password. |
| DATABASE_LIST | Space-separated list of all databases on the externalserver, except for the system databases (sys,mysql,performance_schema, andinformation_schema). Use theSHOW DATABASES MySQLcommand to list your databases. |
| SOURCE_DATA | If you're using an earlier version of MySQL than 8.0.26,then use--master-data as the value for this parameter.For versions of MySQL that are 8.0.26 or higher,set the value of this parameter to--source-data. |
| ADD_DROP_TABLE | If you want to add aDROP TABLE statement beforeeachCREATE TABLE statement, include--add-drop-table. |
| ROUTINES | If you want to show stored routines, such asprocedures and functions, in the output for dumpeddatabases, include--routines. |
| COMPRESS | If you want to compress all information sent betweenthe Cloud SQL replica and the external server,use--compress. |
| GZIP | If you want to compress the dump file even more, use| gzip. If your database contains data that doesnot compress well, such as binary incompressible dataor JPG images, don't use this. |
| BUCKET | The name of the bucket you created in Step 1 tocontain the dump file. |
| DUMP_FILENAME | A file with this name is created in your bucket.This file contains the contents of the database onyour external server. |
Update the source representation instance with the file path of the Cloud Storage bucket
The source representation instance is a Cloud SQL instance that represents thesource database server to the Cloud SQL replica. It's visible in the Google Cloud console and appears the same as a regular Cloud SQL instance, but it contains no data, requires no configuration or maintenance, and doesn't affect billing.
Thesource.json file contains information about the sourcerepresentation instance.
REST
{"name":"PRIMARY_INSTANCE_NAME","region":"REGION_NAME","databaseVersion":"DB_NAME_AND_VERSION","onPremisesConfiguration":{"hostPort":"IP_ADDRESS_AND_PORT","username":"USERNAME","password":"PASSWORD"},"dumpFilePath":"DUMP_FILE_PATH"}
example
{"name":"cloudsql-source-instance","region":"us-central1","databaseVersion":"MYSQL_5_7","onPremisesConfiguration":{"hostPort":"192.0.2.0:3306","username":"replicationUser","password":"486#@%*@"},"dumpFilePath":"gs://replica-bucket/source-database.sql.gz"}
| Property | Description |
|---|---|
| PRIMARY_INSTANCE_NAME | The name of the Cloud SQL instance that's associated with the source representation instance. |
| REGION_NAME | The name of the region that's assigned to the source representation instance. |
| DB_NAME_AND_VERSION | The name and version number of the database that's associated with the source representation instance. |
| IP_ADDRESS_AND_PORT | The IP address and port number reserved for the source representation instance. |
| USERNAME | The username of the source representation instance. |
| PASSWORD | The password of the source representation instance. |
| DUMP_FILE_PATH | The path of the dump file that contains the contents of the database on your external server. |
For a list of the full set of parameters that you can use with thesource.json file, seeREST Resource: instances.
TheonPremisesConfiguration parameter contains information that Cloud SQL needs to connect to the on-premises source. For more information about the values of this parameter, seeOnPremisesConfiguration.
After you set up the Cloud SQL replica, you need to update your sourcerepresentation instance with the file path of the Cloud Storage bucket.
REST
gcloudauthloginACCESS_TOKEN="$(gcloudauthprint-access-token)"curl--header"Authorization: Bearer${ACCESS_TOKEN}"\--header'Content-Type: application/json'\--data@JSON_PATH\-XPATCH\https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT-ID/instances/SOURCE_REPRESENTATION_INSTANCE
example
gcloudauthloginACCESS_TOKEN="$(gcloudauthprint-access-token)"curl--header"Authorization: Bearer${ACCESS_TOKEN}"\--header'Content-Type: application/json'\--data@./source.json\-XPATCH\https://sqladmin.googleapis.com/sql/v1beta4/projects/MyProject/instances/cloudsql-source-instance
| Property | Description |
|---|---|
| JSON_PATH | The path of theJSON file that's stored in the Cloud Storage bucket.This file contains data about the source representationinstance. |
| PROJECT_ID | The ID of your project in Google Cloud. |
| SOURCE_REPRESENTATION_INSTANCE | The name of the sourcerepresentation instance. |
Start replication on the external server
After you have verified that you can replicate fromthe external server, you are ready to perform the replication.
During the initial import process, do not perform anyDDL operations on the external server.Doing so could cause inconsistencies during the import. After the importprocess completes, the replica uses the binary logs on theexternal server to catch up to the current state of theexternal server.
Open a terminal, log in usinggcloud, then enter thecurl command toreplicate from the external server.
REST
gcloudauthloginACCESS_TOKEN="$(gcloudauthprint-access-token)"curl--header"Authorization: Bearer${ACCESS_TOKEN}"\--header'Content-Type: application/json'\--data'{ "syncMode": "SYNC_MODE", "skipVerification": "SKIP_VERIFICATION" }'\-XPOST\https://sqladmin.googleapis.com/sql/v1beta4/projects/PROJECT_ID/instances/REPLICA_INSTANCE/startExternalSync
example
gcloudauthloginACCESS_TOKEN="$(gcloudauthprint-access-token)"curl--header"Authorization: Bearer${ACCESS_TOKEN}"\--header'Content-Type: application/json'\--data'{ "syncMode": "online", "skipVerification": false }'\-XPOST\https://sqladmin.googleapis.com/sql/v1beta4/projects/MyProject/instances/replica-instance/startExternalSync
| Property | Description |
|---|---|
| SYNC_MODE | Verifies that you can keep the Cloud SQLreplica and external server in sync afterreplication is set up. |
| SKIP_VERIFICATION | Whether or not to skip the built-in verificationstep before syncing your data. Only recommendedif you have alreadyverified your replication settings. |
| PROJECT_ID | The ID of your project in Google Cloud. |
| REPLICA_INSTANCE | The ID of your Cloud SQL replica. |
Clean up your storage
If you replicated from a file in a bucket, you can remove this fileand bucket. See the Cloud Storage documentation forDeleting Objects andDeleting Buckets.
Proceed with replication
Once you start replication from the external server, you need to monitorreplication and then complete your migration. To learn more, seeMonitoring replication.
Troubleshoot
| Issue | Troubleshooting |
|---|---|
Lost connection to MySQL server during query when dumping table. | The source may have become unavailable, or the dump contained packets too large. Make sure the external primary is available to connect. You can also modify the values of thenet_read_timeout andnet_write_timeout flags on the source instance to stop the error. For more information on the allowable values for these flags, seeConfigure database flags. To learn more about using |
| The initial data migration was successful, but no data is being replicated. | One possible root cause could be your source database has defined replication flags which result in some or all database changes not being replicated over. Make sure the replication flags such as Run the command |
| The initial data migration was successful but data replication stops working after a while. | Things to try:
|
mysqld check failed: data disk is full. | The data disk of the replica instance is full. Increase the disk size of the replica instance. You can either manually increase the disk size or enable auto storage increase. |
Review your replication logs
When youverify your replication settings, logs areproduced.
You can view these logs by following these steps:
Go to the Logs Viewer in the Google Cloud console.
- Select the Cloud SQL replica from theInstance dropdown.
- Select the
replication-setup.loglog file.
If the Cloud SQL replica is unable to connect to the external server,confirm the following:
- Any firewall on the external server is configured to allow connections from the Cloud SQL replica'soutgoing IP address.
- Your SSL/TLS configuration is correct.
- Your replication user, host, and password are correct.
What's next
- Learn aboutupdating an instance.
- Learn aboutmanaging replicas.
- Learn aboutmonitoring instances.
- Learn aboutpromoting your Cloud SQL replica topromote the replica to a standalone instance and stop replicating from theexternal 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-15 UTC.