Cloud SQL
Connecting to GCP Cloud SQL from a DevZero workspace.
If you're more of a visual learner, check out the video tutorial!
Architecture Diagram
Prerequisites
- Follow the Connecting to GCP guide.
Existing Database
Step 1: Accessing Cloud SQL Instance from a DevBox
- Go to Cloud SQL > Instances and note down the Internal IP Address of the instance.
- Go to VPC network > Routes and select the region where you have deployed the instance.
- Copy the Destination IP Range of the Peering Subnet route which contains the IP address of the Cloud SQL Instance.
- SSH into your Bastion Host and enter the following command:
sudo dz net connect --ssh --advertise-routes=<Destionation-IP-Range>
- Go to DevBox.
- Connecting to the Database.
Run the following commands:
To install the mysql client cli:
sudo apt install mysql-client
To access the database:
mysql -h <Endpoint> -u <Username> --database <Database Name> -p
To install the psql client cli:
sudo apt install postgresql-client
To access the database:
psql -h <Endpoint> --username <Username> -d <Database Name> --password
New Database
Step 1: Creating an Cloud SQL Instance
- Go to Cloud SQL > Create an instance.
- Use one of the database Engine in the Configuration.
- Choose the Available Versions of the Database.
- Enter your Instance ID and Password.
- Go to Choose region and zonal availability and select the zone where you have provisioned your Private Subnet.
- Scroll down to Customize your instance > Connections and choose Private IP only.
- Enter your VPC when prompted.
- Click on create a private service access connection and select Use automatically assigned IP range while choosing Allocated IP range.
- Click on Create Instance.
Step 2: Accessing Cloud SQL from DevBox
- Go to Cloud SQL > Instances and note down the Internal IP Address of the instance.
- Go to VPC network > Routes and select the region where you have deployed the instance.
- Copy the Destination IP Range of the Peering Subnet route which contains the IP address of the Cloud SQL Instance.
- SSH into your Bastion Host and enter the following command:
sudo dz net connect --ssh --advertise-routes=<Destionation-IP-Range>
- Go to DevBox.
- Connecting to the Database.
Run the following commands:
To install the mysql client cli:
sudo apt install mysql-client
To access the database:
mysql -h <Endpoint> -u <Username> --database <Database Name> -p
To install the psql client cli:
sudo apt install postgresql-client
To access the database:
psql -h <Endpoint> --username <Username> -d <Database Name> --password