How to Authorise circle-ci with Google Cloud SQL instance - mysql

I have NodeJS applications which utilise the SQL instance from Google App Engine. I have created a continuous integration (CI) deployment environment with CircleCI and Google App Engine. The deployment works nice. Now, I want to execute my unit tests on CircleCi. But the problem is how do I connect Google Cloud SQL instance on CircleCI?
On the local system, I use Google Cloud SQL proxy OR SSL connection to establish a connection with MySQL. How do I connect to Google Cloud SQL instance from circle-ci using NodeJs MySQL package?

Related

Connect to a on-premise MySQL server from Google Cloud Run

Is it possible to connect to a On-Premise MySQL server from Google Cloud Run? I saw that you can connect to their own Cloud SQL service, but we want to use our own server.

Connecting from google cloud run to google cloud (mysql) using .net core

I have a .net core app installed as a docker on google cloud run, this app that needs to be connected to cloud sql (mysql).
When using the private ip address it, it's not working.
When using public IP, it's working, but It's not a good solution for production.
this is my connection string:
"ConnectionString": "server=10.4.16.6;database=mydb;user=root;pwd=mypwd"
When I create the app, Im able to select the database i need to connect to:
But this is not helping to connect.
The relevant docs are explaining how to do it for python and java explictly.
If you do not want to use public IP then you would need to rely on service account to connect to Cloud SQL. However, .net MySQL driver has no understanding of GCP IAM and Service accounts. So you will need to use a proxy called Cloud SQL Proxy. Cloud SQL Proxy understands IAM and Service accounts.
The flow will basically look like this:
Your app -> Regular MySQL Port -> Cloud SQL Proxy(Installed in the
app's network or locally) -> CloudSQL
You will need to do the following:
Create a service account
Assign the role of Cloud SQL Client to the created service account
Download the service account key in the json format
Set env variable GOOGLE_APPLICATION_CREDENTIALS=C:\Downloaded.json
Download Cloud SQL Proxy
Run it `cloud_sql_proxy -instances=projectname:regioname:instanceid=tcp:3306
At this point you MySQL proxy ready to accept connections at 3306, modify the connection string to take localhost or wherever you installed the Cloud SQL Proxy.
Learn more at About the Cloud SQL Proxy
You can create the Cloud Run app from the console (and select the Cloud SQL Connection) or from the gcloud command line and specify
--add-cloudsql-instances <INSTANCE-NAME> --set-env-vars INSTANCE-CONNECTION-NAME="INSTANCE_CONNECTION_NAME"
These settings automatically enable and configures the Cloud SQL proxy. You can connect to the proxy, from your asp.net Core app, using the unix domain socket using the format: /cloudsql/INSTANCE_CONNECTION_NAME.
I used the following connection string in my appsettings.json and it worked for me:
"Server=/cloudsql/INSTANCE_CONNECTION_NAME;Database=DB_NAME;Uid=USER_NAME;Pwd=PASSWORD;Protocol=unix"
NB. Make sure you have given the service account that your Cloud Run app is running under Cloud SQL Client role in IAM

Connect to GCP Cloud SQL from Compute Engine (not from App Engine)

Typical issues for teams that are migrating from AWS to GCP. How to properly connect to Cloud SQL from Compute engine.
In short that what you should know about Cloud SQL service at GCP.
As I see SQL Cloud more API rather clean MySQL socket tend to work with App Engine rather as plain DB.
List of options that you have to validate and use Cloud SQL without involving developers:
Connection using mysql-client or SQL protocol from external machine or even from Compute Engine requires to add your IP to whitelist. Keep in mind that Compute Engine you will be forced to use static IP due security limitations.In production you should use IP address with SSL
To validate connection from Compute Engine you should use Cloud Shell and gcloud utility
gcloud sql connect [INSTANCE_ID] --user=root
Other option that works only with Second Generation of instances and can be (should be) used in production is SQL Proxy that should be installed on client Compute engine and run as service. You need:
Enable SQL Cloud API
Create and use Service Account with MySQL Client permissions for your instance
Install and run SQL Proxy
Connect to localhost to use proxy as bridge to your SQL Cloud Instance
As result there open questions about "best practices" for production use:
How to automate it in the way that new instances in autoscaling group would be able to connect to Cloud SQL after start? My approach: create template that will start SQL Proxy as service. Is there another way?
How to connect to multiple Cloud SQL instances form the same Compute Engine?

How do I connect to Google Cloud SQL from Google Compute Engine in production?

I have a web application running on Google Compute Engine. I am currently running MySQL on a regular instance, and I want to migrate to Cloud SQL for scalability and reliability.
The GCE docs describe how to connect by Internet IP address or the Cloud SQL Proxy to run the MySQL client to configure a database. They do not describe how to connect to Cloud SQL for a "production" environment that needs to reliably handle a large volume of queries. The Cloud SQL Proxy seems like an elegant solution, because I prefer not to assign external IP addresses to the web servers (they are behind a load balancer), or send database traffic over the Internet.
Which method(s) are considered "production-ready" for connecting a web server to a Cloud SQL instance?
Is Cloud SQL Proxy suitable for a production application?
Is traffic encrypted between the Cloud SQL Proxy and Cloud SQL?
1) Either direct connection by IP or via Cloud SQL Proxy.
2) Yes, the Cloud SQL Proxy is production ready.
3) Yes, the connection between the Cloud SQL Proxy and the Cloud SQL instance happens over SSL.

How to connect local database to Google Cloud SQL?

Since i am new to Google App Engine, I am using MySQL database locally and I am running a sample application in Google App Engine. But i need to use database also using Google Cloud SQL, I referred some details in the Google developer zone and found some information in connecting Google Cloud SQL using googlemysql connection
I have used a sample code for MySQL connection
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
// Load the class that provides the new "jdbc:google:mysql://"
// prefix.
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://azk-net:your-instance-name/guestbook?user=root";
} else {
// Local MySQL instance to use during development.
Class.forName("com.mysql.jdbc.Driver");
url = "jdbc:mysql://127.0.0.1:3306/guestbook?user=root";
// Alternatively, connect to a Google Cloud SQL instance using:
// jdbc:mysql://ip-address-of-google-cloud-sql-instance:3306/guestbook?user=root
}
but the thing is i cannot create instance name for my project. In local it is working correctly but i have deployed in the google app engine i am getting error since instance name is not available
I am creating the instance name in the Google Cloud SQL but it is not creating it requires billing.
can anyone help me to connect the database from local to Google Cloud SQL by writing connection properties for accessing database from Google Cloud SQL.
There's no free quota for Google Cloud SQL, you have to enable billing to use it
Google offers two billing plans for Cloud SQL: Packages and Per Use. More information on pricing for Google Cloud SQL can be found on the Cloud SQL Pricing page.