Migrate MySQL Data to Cloud SQL Using Database Migration Service

Solution for Migrate MySQL Data to Cloud SQL Using Database Migration Service. 1 lab: GSP351. Fast copy-paste commands for Google Cloud.

GSP351 — Migrate MySQL Data to Cloud SQL Using Database Migration Service: Challenge Lab

Estimated time: 1 hour 15 minutes

# 🚀 Migrate MySQL Data to Cloud SQL using Database Migration Service: Challenge Lab > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created for educational purposes, hands-on practice, and Google Cloud certification preparation. This guide is designed to help learners understand Database Migration Service, MySQL, Cloud SQL, migration jobs, continuous migration, and database replication through practical exercises. Always attempt the lab yourself first and follow Google

clear
CYAN='\e[1;36m'
BLUE='\e[1;34m'
RESET='\e[0m'
BOLD='\e[1m'
GREEN='\e[1;32m'
YELLOW='\e[1;33m'
MAGENTA='\e[1;35m'

echo -e "${CYAN}${BOLD}"
echo "   ____        _     _ _            __   ___              "
echo "  / __ \      | |   (_) |          / _| / _ \             "
echo " | |  | |_ __| |__   _| |_  ___   | |_ | | | |_ __  ___   "
echo " | |  | | '__| '_ \| | __| / _ \  |  _|| | | | '_ \/ __|  "
echo " | |__| | |  | |_) | | |_ | (_) | | |  | |_| | |_) \__ \  "
echo "  \____/|_|  |_.__/|_|\__| \___/  |_|   \___/| .__/|___/  "
echo "                                             | |          "
echo "                                             |_|          "
echo -e "${RESET}"
echo -e "${BLUE}${BOLD}╔════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${BLUE}${BOLD}║    🚀 BROUGHT TO YOU BY ORBIT OF OPS                       ║${RESET}"
echo -e "${BLUE}${BOLD}╚════════════════════════════════════════════════════════════╝${RESET}\n"

echo -e "${YELLOW}Extracting dynamic Region, Zone, and Source IP directly from VM...${RESET}"
export VM_NAME=$(gcloud compute instances list --format="value(name)" 2>/dev/null | head -n 1)
export ZONE=$(gcloud compute instances list --filter="name=$VM_NAME" --format="value(zone)" 2>/dev/null)
export REGION=${ZONE%-*}
export EXT_IP=$(gcloud compute instances describe $VM_NAME --zone=$ZONE --format="value(networkInterfaces[0].accessConfigs[0].natIP)" 2>/dev/null)

echo -e "✅ Region:          ${GREEN}$REGION${RESET}"
echo -e "✅ Zone:            ${GREEN}$ZONE${RESET}"
echo -e "✅ Source VM:       ${GREEN}$VM_NAME${RESET}"
echo -e "✅ Source Ext IP:   ${GREEN}$EXT_IP${RESET}\n"

echo -e "${CYAN}Enabling Database Migration and Service Networking APIs...${RESET}"
gcloud services enable datamigration.googleapis.com servicenetworking.googleapis.com --quiet

echo -e "${CYAN}Creating MySQL Source Connection Profile (Using External IP)...${RESET}"
gcloud database-migration connection-profiles create mysql mysql-source-profile \
    --display-name="mysql-source-profile" \
    --region=$REGION \
    --host=$EXT_IP \
    --port=3306 \
    --username="admin" \
    --password="changeme" \
    --quiet

echo -e "\n${GREEN}${BOLD}✅ Command 1 Complete! Click 'Check my progress' on Task 1 and proceed to the Manual UI Steps below.${RESET}"
CYAN='\e[1;36m'
BLUE='\e[1;34m'
RESET='\e[0m'
BOLD='\e[1m'
GREEN='\e[1;32m'
YELLOW='\e[1;33m'
MAGENTA='\e[1;35m'

echo -e "${BLUE}${BOLD}╔════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${BLUE}${BOLD}║    🚀 COMMAND 2 OF 2: REPLICATION TEST & PROMOTION         ║${RESET}"
echo -e "${BLUE}${BOLD}╚════════════════════════════════════════════════════════════╝${RESET}\n"

echo -e "${MAGENTA}${BOLD}Check your lab instructions for the exact Continuous Migration Job Name.${RESET}"
read -p "Enter Continuous Migration Job Name: " SQL_CONTINUOUS
echo ""

echo -e "${YELLOW}Extracting VM Name, Region, and Zone dynamically...${RESET}"
export VM_NAME=$(gcloud compute instances list --format="value(name)" 2>/dev/null | head -n 1)
export ZONE=$(gcloud compute instances list --filter="name=$VM_NAME" --format="value(zone)" 2>/dev/null)
export REGION=${ZONE%-*}

echo -e "✅ Found VM: ${GREEN}$VM_NAME${RESET} in Zone: ${GREEN}$ZONE${RESET}\n"

echo -e "${CYAN}Testing Continuous Sync: Updating source database record on $VM_NAME...${RESET}"
gcloud compute ssh $VM_NAME --zone=$ZONE --quiet --command="mysql -u admin -pchangeme -e \"use customers_data; update customers set gender = 'FEMALE' where addressKey = 934;\""

echo -e "${YELLOW}Waiting 30 seconds for Database Migration Service to replicate the change...${RESET}"
sleep 30

echo -e "${CYAN}Promoting the Cloud SQL Instance to Standalone (This takes 1-2 minutes)...${RESET}"
gcloud database-migration migration-jobs promote $SQL_CONTINUOUS --region=$REGION --quiet

echo ""
echo -e "${GREEN}${BOLD}# ─────────────────────────────────────────────────────────────${RESET}"
echo -e "${GREEN}${BOLD}#  Lab cleared? Like the video and subscribe to Orbit of Ops 🚀${RESET}"
echo -e "${GREEN}${BOLD}#  youtube.com/@orbitofops${RESET}"
echo -e "${GREEN}${BOLD}# ─────────────────────────────────────────────────────────────${RESET}"