Search code examples
proxyconfigurationgoogle-cloud-sqllaunch

Where to launch the Google Cloud SQL proxy?


I am having problems because the proxy dies, maybe it is because I launch it from the console, I am a novice with Cloud SQl.

Google Cloud Storage mounted it on the instance configuration page in the custom metadata section with the key 'startup-script'. That's the good place to launch Cloud SQL proxy too?

I've also seen it launch by console:

./cloud_sql_proxy -instances=xxx:us-central1:my-instance=tcp:3306 &
and
nohup ./cloud_sql_proxy -instances=xxx:us-central1:my-instance=tcp:3306 &

Thinking about it starting automatically on reboot, what's the good way?

I use an instance with Debian 9


Solution

  • Install the Cloud SQL Proxy on /usr/local/bin directory

    cd /usr/local/bin
    sudo wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy
    sudo chmod +x cloud_sql_proxy
    

    The best way to initialize the Cloud SQL Proxy is by using a systemd service

    A Linux service is an application (or set of applications) that runs in the background waiting to be used

    1.- Create this file /etc/systemd/system/cloud-sql-proxy.service

    Please replace this string <INSTANCE_CONNECTION_NAME> with your instance connection name , for example: myproject:myregion:myinstance

    [Unit]
    Description=Connecting MySQL Client from Compute Engine using the Cloud SQL Proxy
    Documentation=https://cloud.google.com/sql/docs/mysql/connect-compute-engine
    Requires=networking.service
    After=networking.service
    
    [Service]
    WorkingDirectory=/usr/local/bin
    ExecStart=/usr/local/bin/cloud_sql_proxy -dir=/var/run/cloud-sql-proxy -instances=<INSTANCE_CONNECTION_NAME>=tcp:3306
    Restart=always
    StandardOutput=journal
    User=root
    
    [Install]
    WantedBy=multi-user.target
    

    2.- run sudo systemctl daemon-reload

    3.- run sudo systemctl enable cloud-sql-proxy

    4.- run sudo systemctl start cloud-sql-proxy

    For additional information please check this guide