Overview and What You Will Learn
Database Migration Service (DMS) moves a live database into Cloud SQL by copying the existing data first and then replicating every new change until you cut over, so downtime shrinks from hours to minutes. In this lab you will prepare a MySQL source, create the source and destination connection profiles, run a continuous migration job, and promote the Cloud SQL instance as the new primary.
- Preparing a MySQL source for replication
- Creating source and Cloud SQL destination connection profiles
- Creating, verifying and starting a continuous migration job
- Reading the job's phase to know when replication is caught up
- Cutting over safely with a one-way promote
Why This Matters in Production
DMS shortens downtime because the long data copy happens while the source keeps serving traffic. Imagine a team planning a weekend outage to export and import an orders database, only to find the export alone takes twelve hours and the application must stay offline for all of it. With continuous replication, a platform team at a company like Zerodha can keep trading-adjacent services running and take only a short, planned pause at cutover.
Core Principles
A DMS continuous job runs in three stages: a full dump of the existing data, then change data capture (CDC) that replays new writes from the source's binary log, then a promote that detaches the destination and makes it a standalone primary. The source stays online and serving traffic through the first two stages.
stays online)]:::src --> DUMP[Full data dump]:::tool DUMP --> CDC[Continuous replication]:::tool CDC --> DST[(Cloud SQL
destination)]:::cloud DST --> CUT([Promote at cutover]):::ok classDef src fill:#DBEAFE,color:#1E293B classDef tool fill:#326CE5,color:#FFFFFF classDef cloud fill:#EDE9FE,color:#1E293B classDef ok fill:#10B981,color:#FFFFFF
The destination is read-only until you promote it, and promotion ends replication permanently. Writes are not paused for you: you stop the application's writes yourself, wait for replication to catch up, and then promote.
Detailed Step-by-Step Practical Lab
- Create the project, link billing, and enable the APIs:
gcloud projects create mumbai-dms-lab-2026 --name="Database Migration Lab"gcloud billing projects link mumbai-dms-lab-2026 \ --billing-account=0A1B2C-3D4E5F-6A7B8Cgcloud config set project mumbai-dms-lab-2026gcloud services enable datamigration.googleapis.com sqladmin.googleapis.com compute.googleapis.com- Prepare the source MySQL server. Binary logging must be on in ROW format, and the migration user needs replication privileges:
-- Check these on the source (mumbai-onprem-mysql-1, 10.20.4.15)SHOW VARIABLES LIKE 'log_bin'; -- expected: ONSHOW VARIABLES LIKE 'binlog_format'; -- expected: ROW -- Create the migration user with a strong generated passwordCREATE USER 'migration-user'@'%' IDENTIFIED BY 'REPLACE_WITH_GENERATED_PASSWORD';GRANT EXECUTE, RELOAD, REPLICATION CLIENT, REPLICATION SLAVE, SELECT, SHOW VIEW ON *.* TO 'migration-user'@'%';FLUSH PRIVILEGES;- Create the source connection profile. Read the password into a variable so it never lands in your shell history:
read -s -p "Source DB password: " SRC_DB_PASSWORD gcloud database-migration connection-profiles create mysql \ source-mysql-profile \ --region=asia-south1 \ --host=10.20.4.15 \ --port=3306 \ --username=migration-user \ --password="$SRC_DB_PASSWORD"SecurityNever put database passwords directly in commands or scripts. Use a prompt, a secret manager, or an environment variable, and require SSL on the source connection for any traffic that leaves your network.
- Create the Cloud SQL destination profile. The instance it creates takes the profile's name:
gcloud database-migration connection-profiles create cloudsql \ orders-db-migrated \ --region=asia-south1 \ --database-version=MYSQL_8_0 \ --tier=db-n1-standard-2 \ --source-id=source-mysql-profile- Create the continuous migration job and run its verification check:
gcloud database-migration migration-jobs create dms-job-orders-db \ --region=asia-south1 \ --type=CONTINUOUS \ --source=source-mysql-profile \ --destination=orders-db-migrated \ --peer-vpc=projects/mumbai-dms-lab-2026/global/networks/vpc-migration gcloud database-migration migration-jobs verify dms-job-orders-db \ --region=asia-south1TipFlag names for connectivity and destination settings differ between DMS releases and between source types. Run
gcloud database-migration migration-jobs create --helpand pick the connectivity method that matches your network, such as VPC peering for a private source reached over VPN or Interconnect.
- Start the job, then watch its phase. The job moves from
FULL_DUMPtoCDConce it is replicating changes:
gcloud database-migration migration-jobs start dms-job-orders-db \ --region=asia-south1 gcloud database-migration migration-jobs describe dms-job-orders-db \ --region=asia-south1 \ --format="value(state,phase)"Rehearse the cutover. Read from the destination to check row counts and recent data. It is read-only, so you can test queries safely before promoting.
Cut over. Stop writes on the application, make the source read-only, confirm the replication delay in the job's monitoring page has dropped to zero, and then promote:
-- On the source, after the application has stopped writingSET GLOBAL read_only = ON;gcloud database-migration migration-jobs promote dms-job-orders-db \ --region=asia-south1- Confirm the job finished and the destination is a standalone primary:
gcloud database-migration migration-jobs describe dms-job-orders-db \ --region=asia-south1 \ --format="value(state)"# Expected: COMPLETED- Clean up the job, both profiles, and the Cloud SQL instance the migration created:
gcloud database-migration migration-jobs delete dms-job-orders-db \ --region=asia-south1 --quietgcloud database-migration connection-profiles delete source-mysql-profile \ --region=asia-south1 --quietgcloud database-migration connection-profiles delete orders-db-migrated \ --region=asia-south1 --quietgcloud sql instances listgcloud sql instances delete orders-db-migrated --quietgcloud projects delete mumbai-dms-lab-2026 --quietProduction Best Practices & Common Pitfalls
Common MistakePlanning a migration around one long maintenance window, assuming the whole transfer must happen inside it. With continuous replication, application downtime can be limited to the cutover itself, often minutes rather than the hours or days of a manual export and import.
TipRun the entire migration once against a staging copy before the real one. A rehearsal shows how long the full dump takes, whether replication keeps up, and exactly how long your cutover steps take.
RememberPromotion is one-way. Once the Cloud SQL instance is promoted, changes made on it do not flow back to the source, so keep a rollback plan and do not decommission the source until the new primary has been stable.
- Keep the source reachable for the whole replication phase. If connectivity breaks for long, replication can fall behind or fail, and the source must still hold the binary logs the job needs.
- Retain binary logs long enough. Set binary log retention to cover your longest expected pause plus a margin, or DMS may lose its place in the log.
- Freeze schema changes during the migration. DDL on the source mid-migration can break or stall replication, so schedule schema work before the job starts or after cutover.
- Check tables without primary keys. Tables with no primary key replicate poorly and slowly, so add keys on the source before migrating.
- Size the destination for real load. Pick a tier that handles production traffic, since the cutover is not the time to discover an undersized instance.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud database-migration connection-profiles create mysql |
Define the source database connection |
gcloud database-migration connection-profiles create cloudsql |
Define the Cloud SQL destination |
gcloud database-migration migration-jobs create |
Create a migration job |
gcloud database-migration migration-jobs verify |
Check the job's prerequisites before starting |
gcloud database-migration migration-jobs start |
Begin the dump and replication |
gcloud database-migration migration-jobs promote |
Cut over to the destination as primary |