You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Google Cloud Shell连接GCP MySQL实例及解决连接报错问题

Hey there! Let's break down why you're seeing that error and walk through the best ways to connect your Cloud Shell Python script to your GCP MySQL instance.

Why the "Host not allowed" error happens

That error pops up because your Cloud SQL MySQL instance restricts incoming connections to specific IP addresses or networks by default. Cloud Shell uses a dynamic public IP that changes every time you start a new session, so that 35.200.234.37 isn't in your instance's authorized network list yet.

We've got two solid fixes for this—one for quick testing, and a more secure, long-term solution.

Fix 1: Temporarily authorize your Cloud Shell IP (for quick tests)

If you just need to test your connection fast, here's what to do:

  • First, grab your current Cloud Shell public IP. Run this in Cloud Shell:
    curl ifconfig.me
    
    You'll get an IP like the one in your error message.
  • Head to the GCP Console, navigate to your Cloud SQL MySQL instance, and go to the Connections tab.
  • Under Authorized networks, click Add network, paste the IP you just copied, add a note like "Cloud Shell temp IP", and save the changes.
  • Now try running your Python script again—it should connect successfully. Just keep in mind: if you restart Cloud Shell later, your IP will change, and you'll have to repeat this step.

This is GCP's official preferred method—no need to deal with dynamic IPs, and you don't even need to expose your MySQL instance to the public internet. Here's how to set it up:

  1. First, make sure the Cloud SQL Admin API is enabled. Run this in Cloud Shell if you haven't already:
    gcloud services enable sqladmin.googleapis.com
    
  2. Download and set up the Cloud SQL Auth Proxy (if you haven't used it before):
    wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
    chmod +x cloud_sql_proxy
    
  3. Start the proxy. You'll need your instance's connection name (found on your Cloud SQL instance's Overview page, formatted as [PROJECT-ID]:[REGION]:[INSTANCE-NAME]):
    ./cloud_sql_proxy -instances=<YOUR-INSTANCE-CONNECTION-NAME>=tcp:3306
    
    Leave this terminal tab open—keep the proxy running while you connect.
  4. Open a new Cloud Shell tab, then use this Python code to connect (replace placeholders with your actual credentials):
    import mysql.connector
    
    try:
        conn = mysql.connector.connect(
            host="127.0.0.1",
            user="YOUR-MYSQL-USERNAME",
            password="YOUR-MYSQL-PASSWORD",
            database="YOUR-DATABASE-NAME"
        )
        if conn.is_connected():
            cursor = conn.cursor()
            cursor.execute("SELECT VERSION()")
            db_version = cursor.fetchone()
            print(f"Connected successfully! MySQL version: {db_version[0]}")
    except mysql.connector.Error as e:
        print(f"Error connecting: {e}")
    finally:
        if conn.is_connected():
            cursor.close()
            conn.close()
    
    Since the proxy creates a local tunnel to your Cloud SQL instance, connecting to 127.0.0.1:3306 bypasses the IP restriction entirely.

A quick note: If your MySQL instance uses a private IP only, the Auth Proxy still works perfectly—no need to mess with VPC configurations for Cloud Shell.

内容的提问来源于stack exchange,提问作者Phoebe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.12 05:17:23