This page describes how to set up and use a managed import for data when replicating from an external server to Cloud SQL.
You must complete all the steps on this page. When finished, you can administer and monitor the source representation instance the same way as you would any other Cloud SQL instance.
Before you begin
Before you begin, complete these steps:
Verify your replication settings
After your setup is complete, ensure that the Cloud SQL replica can replicate from the external server.
The following external sync settings must be correct.
- Connectivity between the Cloud SQL replica and external server
- Replication user privileges
- Version compatibility
- The Cloud SQL replica is not already replicating
To verify these settings, open a Cloud Shell terminal and enter the following commands:
curl
gcloud auth login
ACCESS_TOKEN="$(gcloud auth print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data '{
"syncMode": " SYNC_MODE
",
"syncParallelLevel": " SYNC_PARALLEL_LEVEL
",
"selectedObjects": " SELECTED_OBJECTS
"
}' \
-X POST \
https://sqladmin.googleapis.com/sql/v1beta4/projects/ PROJECT_ID
/instances/ REPLICA_INSTANCE_ID
/verifyExternalSyncSettings
example
gcloud
auth
login
ACCESS_TOKEN
=
"$(gcloud auth print-access-token)"
curl
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data '{
"syncMode"
:
"online"
,
"syncParallelLevel"
:
"optimal"
,
"selectedObjects"
:[
{
"database"
:
"db1"
}
,
{
"database"
:
"db2"
}
]
}'
\
-
X
POST
\
https
:
//
sqladmin
.
googleapis
.
com
/
sql
/
v1beta4
/
projects
/
myproject
/
instances
/
myreplica
/
verifyExternalSyncSettings
These calls return a list of type sql#externalSyncSettingErrorList
.
If the list is empty, then there are no errors. A response without errors appears like this:
{ "kind" : "sql#externalSyncSettingErrorList" }
EXTERNAL_SYNC_MODE_UNSPECIFIED
, ONLINE
, and OFFLINE
.Verify the setting that controls the speed at which data from tables of a database are transferred. The following values are available:
-
min:
Takes the lowest amount of compute resources on the database. This is the slowest speed for transferring data. -
optimal:
Provides a balanced performance with an optimal load on the database. -
max:
Provides the highest speed for transferring data, but this might cause an increased load on the database.
Note:
The default value for this parameter is optimal
because this setting provides a good speed to transfer the data and it has a reasonable impact on the database. We recommend that you use this value.
Update a source representation instance
To update the databases that you want to migrate from the source representation
instance to the destination Cloud SQL instance, update the list of
database names that are associated with the selectedObjects
parameter. If you
don't use this parameter or provide an empty list as the value for the parameter,
then all databases are migrated from the source to the destination.
source.json
{ "name" : " SOURCE_NAME " , "region" : " REGION " , "databaseVersion" : " DATABASE_VERSION " , "onPremisesConfiguration" : { "selectedObjects" : " SELECTED_OBJECTS " , "username" : " USERNAME " , "password" : " PASSWORD " } }
example
//
example
of
source
.
json
for
external
server
that
//
-
initiates
replication
from
a
Cloud
SQL
managed
import
//
-
doesn
'
t
use
SSL
/
TLS
{
"name"
:
"cloudsql-source-instance"
,
"region"
:
"us-central1"
,
"databaseVersion"
:
"POSTGRES_9_6"
,
"onPremisesConfiguration"
:
{
"selectedObjects"
:[
{
"database"
:
"db1"
}
,
{
"database"
:
"db3"
}
],
"username"
:
"newReplicationUser"
,
"password"
:
"525#@%*@"
}
}
Property | Description |
---|---|
SOURCE_NAME | The name of the source representation instance. |
REGION | The region where the source representation instance resides. |
DATABASE_VERSION | The database version running on your
external server. The options are POSTGRES_9_6
, POSTGRES_10
, POSTGRES_11
, POSTGRES_12
, POSTGRES_13
, POSTGRES_14
, POSTGRES_15
, POSTGRES_16
, or POSTGRES_17
. |
SELECTED_OBJECTS | An updated comma-separated list of objects, specifying databases that you're migrating from the source representation instance instance to the destination Cloud SQL instance. |
USERNAME | The replication user account on the external server. |
PASSWORD | The password for the account. |
Then, to modify the source representation instance in Cloud SQL, open a Cloud Shell terminal and enter the following commands:
curl
gcloud auth login
ACCESS_TOKEN="$(gcloud auth print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data @ JSON_PATH
\
-X PATCH \
https://sqladmin.googleapis.com/sql/v1beta4/projects/ PROJECT_ID
/instances/ SOURCE_NAME
example
gcloud
auth
login
ACCESS_TOKEN
=
"$(gcloud auth print-access-token)"
curl
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data @./source.json \
-
X
PATCH
\
https
:
//
sqladmin
.
googleapis
.
com
/
sql
/
v1beta4
/
projects
/
MyProject
/
instances
/
cloudsql
-
source
-
instance
Property | Description |
---|---|
JSON_PATH | The path to the JSON
file that contains the
request data for the external server. |
PROJECT_ID | The ID of your Google Cloud project. |
SOURCE_NAME | The name of the source representation instance. |
Start replication on the external server
After verifying that you can replicate from the external server, start the replication. The speed for performing the replication for the initial import process is up to 500 GB per hour. However, this speed can vary based on the machine tier, data disk size, network throughput, and nature of your database.
curl
gcloud auth login
ACCESS_TOKEN="$(gcloud auth print-access-token)"
curl --header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data '{
"syncMode": " SYNC_MODE
",
"skipVerification": " SKIP_VERIFICATION
",
"syncParallelLevel": " SYNC_PARALLEL_LEVEL
"
}' \
-X POST \
https://sqladmin.googleapis.com/sql/v1beta4/projects/ PROJECT_ID
/instances/ REPLICA_INSTANCE_ID
/startExternalSync
example
gcloud
auth
login
ACCESS_TOKEN
=
"$(gcloud auth print-access-token)"
curl
--header "Authorization: Bearer ${ACCESS_TOKEN}" \
--header 'Content-Type: application/json' \
--data '{
"syncMode"
:
"online"
,
"syncParallelLevel"
:
"optimal"
}'
\
-
X
POST
\
https
:
//
sqladmin
.
googleapis
.
com
/
sql
/
v1beta4
/
projects
/
MyProject
/
instances
/
replica
-
instance
/
startExternalSync
Provide a setting that controls the speed at which data from tables of a database are transferred. The following values are available:
-
min:
Takes the lowest amount of compute resources on the database. This is the slowest speed for transferring data. -
optimal:
Provides a balanced performance with an optimal load on the database. -
max:
Provides the highest speed for transferring data, but this might cause an increased load on the database.
Note:
The default value for this parameter is optimal
because this setting provides a good speed to transfer the data and it has a reasonable impact on the database. We recommend that you use this value.
Monitor the migration
Once you start replication from the external server, you need to monitor replication. To learn more, see Monitoring replication . You can then complete your migration.
Troubleshoot
Consider the following troubleshooting options:
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.
pg_replication_slots
system view and filtering on the active
column. Unused
slots can be dropped to remove WAL segments using the pg_drop_replication_slot
command.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.
- All tables must have a unique/primary key. Every update on such a table without a unique/primary key causes full table scans on th replica.
- 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.
If you must use hash indexes, upgrade to PostgreSQL 10+. Otherwise, if you also want to use replicas, don't use hash indexes in PostgreSQL 9.6.
SELECT * from pg_stat_activity where state = 'active' and pid = XXXX and username = 'cloudsqlreplica'
is expected to run continuously on your primary instance.Recreate the replica after stopping all running queries.
To resolve this issue, complete the following steps:
- Turn on the log_duration
flag and set the
log_statement
parameter toddl
. This provides you with both the queries and the run time on the database. However, depending on your workload, this might cause performance issues. - On both the primary instance and the read replica, run
explain analyze
for the queries. - Compare the query plan and check for differences.
If this is a specific query, then modify the query. For example, you can change the order of the joins to see if you get better performance.
Review your replication logs
When you verify your replication settings , logs are produced.
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 the Instance dropdown.
- Select the
replication-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's outgoing IP address .
- Your SSL/TLS configuration is correct.
- Your replication user, host, and password are correct.
What's next
- Learn about updating an instance .
- Learn about managing replicas .
- Learn about monitoring instances .
- Learn about promoting your Cloud SQL replica .