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

# ==============================================================================
# ORBIT OF OPS - BRANDING & COLORS
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
BLUE='\e[1;34m'
MAGENTA='\e[1;35m'
RED='\e[1;31m'
RESET='\e[0m'
BOLD='\e[1m'

clear
echo -e "${CYAN}${BOLD}"
cat << "EOF"
  ____       _     _ _            __    ___            
 / __ \     | |   (_) |          / _|  / _ \           
| |  | |_ __| |__  _| |_   ___  | |_  | | | |_ __  ___ 
| |  | | '__| '_ \| | __| / _ \ |  _| | | | | '_ \/ __|
| |__| | |  | |_) | | |_ | (_) || |   | |_| | |_) \__ \
 \____/|_|  |_.__/|_|\__| \___/ |_|    \___/| .__/|___/
                                            | |        
                                            |_|        
EOF
echo -e "${RESET}"
echo -e "${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP351 INITIATED <<<${RESET}\n"

# PRE-FLIGHT CHECKS & VARIABLES (DYNAMIC AUTO-FETCH)
# ==============================================================================
echo -e "${BOLD}${YELLOW}[Orbit of Ops] Auto-fetching Project, Zone, and Region...${RESET}"

export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)
if [[ -z "$PROJECT_ID" ]]; then
    export PROJECT_ID=$DEVSHELL_PROJECT_ID
fi

export PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)' 2>/dev/null)

export ZONE=$(gcloud compute project-info describe \
    --format="value(commonInstanceMetadata.items[google-compute-default-zone])" 2>/dev/null | tail -n 1)

if [[ -z "$ZONE" ]]; then
    echo -e "${BOLD}${RED}⚠️ Could not auto-detect the default zone via gcloud metadata.${RESET}"
    read -p "${BOLD}${CYAN}Please enter the lab Zone (e.g., us-east1-c): ${RESET}" ZONE
    export ZONE
fi

export REGION=${ZONE%-*}

gcloud config set compute/zone $ZONE 2>/dev/null
gcloud config set compute/region $REGION 2>/dev/null

echo -e "✅ Project ID: ${GREEN}$PROJECT_ID${RESET}"
echo -e "✅ Zone:       ${GREEN}$ZONE${RESET}"
echo -e "✅ Region:     ${GREEN}$REGION${RESET}\n"

# ==============================================================================
# TASK 1: ENABLE APIS & CREATE CONNECTION PROFILE
# ==============================================================================
echo -e "${BLUE}${BOLD}[Orbit of Ops] Enabling Required APIs...${RESET}"
gcloud services enable datamigration.googleapis.com servicenetworking.googleapis.com --quiet || true

echo -e "${CYAN}${BOLD}[Orbit of Ops] Discovering Source MySQL Instance IP...${RESET}"
export SOURCE_VM_NAME=$(gcloud compute instances list --format="value(name)" | head -n 1)

if [[ -z "$SOURCE_VM_NAME" ]]; then
    echo -e "${RED}${BOLD}⚠️ Could not auto-detect the Source VM name via gcloud.${RESET}"
    read -p "$(echo -e ${CYAN}${BOLD}Please enter the exact Source VM Name from your console: ${RESET})" SOURCE_VM_NAME
fi

export SOURCE_IP=$(gcloud compute instances describe $SOURCE_VM_NAME --zone=$ZONE --format='get(networkInterfaces[0].accessConfigs[0].natIP)')

if [[ -z "$SOURCE_IP" ]]; then
    echo -e "${RED}${BOLD}⚠️ Could not auto-detect the Source VM IP.${RESET}"
    read -p "$(echo -e ${CYAN}${BOLD}Please enter the Source VM External IP: ${RESET})" SOURCE_IP
fi

echo -e "✅ Source VM Name: ${GREEN}$SOURCE_VM_NAME${RESET}"
echo -e "✅ Source IP Found: ${GREEN}$SOURCE_IP${RESET}\n"

echo -e "${MAGENTA}${BOLD}[Orbit of Ops] Creating MySQL Connection Profile...${RESET}"
gcloud database-migration connection-profiles create mysql mysql-source-profile \
    --display-name="mysql-source-profile" \
    --region=$REGION \
    --host=$SOURCE_IP \
    --port=3306 \
    --username=admin \
    --password=changeme \
    --quiet || true

echo -e "\n${GREEN}${BOLD}🎉 TASK 1 COMPLETE! Please proceed to the UI instructions for Tasks 2 & 3.${RESET}"
export SOURCE_VM_NAME=$(gcloud compute instances list --format="value(name)" | head -n 1); gcloud compute ssh $SOURCE_VM_NAME --zone=$ZONE --quiet --command="mysql -u admin -pchangeme -e \"use customers_data; update customers set gender = 'FEMALE' where addressKey = 934;\""; echo -e "\n\e[1;32m\e[1m✅ Database updated successfully! Allow a minute for replication, then check your progress in the lab portal.\e[0m"