Use a dump file to set up replication from external databases

MySQL  |  PostgreSQL  |  SQL Server

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;
PropertyDescription
NEW_HOSTSpecify theoutgoing IP of theCloud SQL replica.
OLD_HOSTThe current value assigned toHost that you want tochange.
USERNAMEThe replication user account on the external server.
GCP_USERNAMEThe username for the Google Cloud Platform (GCP) user account.
HOSTThe 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/verifyExternalSyncSettings

example

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
PropertyDescription
SYNC_MODEverifyExternalSyncSettings verifies that youcan keep the Cloud SQL replica andexternal serverin sync after replication is set up. Sync modesincludeEXTERNAL_SYNC_MODE_UNSPECIFIED,ONLINE, andOFFLINE.
SKIP_VERIFICATIONWhether or not to skip the built-in verificationstep before syncing your data. Only recommendedif you have alreadyverified your replication settings.
PROJECT_IDThe ID of your project in Google Cloud.
REPLICA_INSTANCEThe 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 themysqldump utility bundled with MySQL.
  • Whilemysqldump is 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:

  1. In Google Cloud,create a Cloud Storage bucket.
  2. 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
PropertyDescription
EXTERNAL_HOSTThe IPv4 or DNS address for the external server.
EXTERNAL_PORTThe port for the external server.If the external server is hosted on Cloud SQL,this is3306.
USERNAMEThe name of the replication user account or useraccount on the external server that has database readpermissions.
PASSWORDReplication user password.
DATABASE_LISTSpace-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_DATAIf 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_TABLEIf you want to add aDROP TABLE statement beforeeachCREATE TABLE statement, include--add-drop-table.
ROUTINESIf you want to show stored routines, such asprocedures and functions, in the output for dumpeddatabases, include--routines.
COMPRESSIf you want to compress all information sent betweenthe Cloud SQL replica and the external server,use--compress.
GZIPIf 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.
BUCKETThe name of the bucket you created in Step 1 tocontain the dump file.
DUMP_FILENAMEA 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"}
PropertyDescription
PRIMARY_INSTANCE_NAMEThe name of the Cloud SQL instance that's associated with the source representation instance.
REGION_NAMEThe name of the region that's assigned to the source representation instance.
DB_NAME_AND_VERSIONThe name and version number of the database that's associated with the source representation instance.
IP_ADDRESS_AND_PORTThe IP address and port number reserved for the source representation instance.
USERNAMEThe username of the source representation instance.
PASSWORDThe password of the source representation instance.
DUMP_FILE_PATHThe 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
PropertyDescription
JSON_PATHThe path of theJSON file that's stored in the Cloud Storage bucket.This file contains data about the source representationinstance.
PROJECT_IDThe ID of your project in Google Cloud.
SOURCE_REPRESENTATION_INSTANCEThe 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
PropertyDescription
SYNC_MODEVerifies that you can keep the Cloud SQLreplica and external server in sync afterreplication is set up.
SKIP_VERIFICATIONWhether or not to skip the built-in verificationstep before syncing your data. Only recommendedif you have alreadyverified your replication settings.
PROJECT_IDThe ID of your project in Google Cloud.
REPLICA_INSTANCEThe 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

IssueTroubleshooting
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 usingmysqldump flags for managed import migration, see Allowed and default initial sync flags

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 asbinlog-do-db,binlog-ignore-db,replicate-do-db orreplicate-ignore-db are not set in a conflicting way.

Run the commandshow master status on the primary instance to see the current settings.

The initial data migration was successful but data replication stops working after a while.Things to try:

  • Check the replication metrics for your replica instance in the Cloud Monitoring section of the Google Cloud console.
  • The errors from the MySQL IO thread or SQL thread can be found inCloud Logging in themysql.err log files.
  • The error can also be found when connecting to the replica instance. Run the commandSHOW SLAVE STATUS, and check for the following fields in the output:
    • Slave_IO_Running
    • Slave_SQL_Running
    • Last_IO_Error
    • Last_SQL_Error
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:

  1. Go to the Logs Viewer in the Google Cloud console.

    Go to the Logs Viewer

  2. Select the Cloud SQL replica from theInstance dropdown.
  3. Select thereplication-setup.log log 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

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.