App engine service cannot connect to Google Cloud SQL database sometimes? - google-app-engine

I deployed a Django app to Google app engine flexible env, and it uses a Postgresql db from google cloud sql.
The website could be open, but if I refresh the page at a very short interval, then the website will fail with sometime error saying
Exception Value:
could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/cloudsql/[my-db-connection-name]/.s.PGSQL.5432"?
I wonder why could it fail? Does it mean Google app engine or cloud sql sucks? Should I switch to Heroku or AWS?

It looks like you are not connecting to your Cloud SQL Instance correctly. Make sure you are connecting from App Engine flexible environment to Cloud SQL following the instructions from this link [1].
If your instance has public IP address use this link [2] or if your instance has private IP address use this link [3] to configure connectivity for your Cloud SQL instance.
[1] https://cloud.google.com/sql/docs/postgres/connect-app-engine-flexible
[2] https://cloud.google.com/sql/docs/postgres/configure-ip
[3] https://cloud.google.com/sql/docs/postgres/configure-private-ip

Related

Unable to connect Google App Engine and Cloud SQL using Flask and SQL Alchemy

I'm attempting to connect a Flask App Engine instance to a Cloud SQL instance and the connection works when I run things locally but doesn't work when I deploy it using gcloud app deploy. I'm using flask_sqlalchemy to connect to the database with the following URI string
app.config["SQLALCHEMY_DATABASE_URI"]= f"postgresql://postgres:{PASSWORD}#{PUBLIC_IP_ADDRESS}:5432/{DBNAME}"
However this results in a timeout error that looks like this:
OperationalError: (psycopg2.OperationalError) could not connect to server: Connection timed out
Try taking a look at these pages:
Connecting from App Engine standard environment to Cloud SQL
Connecting from App Engine flexible environment to Cloud SQL
There are instructions for both Private and Public IP.
You may be also interesting this demo app:
https://github.com/GoogleCloudPlatform/python-docs-samples/tree/master/cloud-sql/postgres/sqlalchemy

Access Google Cloud SQL - SQL Server from Google Cloud Function

I'm trying to contact a SQL Server hosted on Google Cloud SQL using Private IP and VPC serverless access in Google Cloud Function)
This is actually not working (The Google Cloud Function reach its timeout) but in the same condition I succeed contacting on the private IP a PostgresSQL server, all other things being equaled (the infrastructure is deployed by Terraform to ensure exact same conditions, I just change the database_version)
The database, the VPC Serverless access and the Google Cloud Functions are all deployed in the same region.
Is there any workaround or solution to make it work?
If you check the oficial documentation Connecting to Cloud SQL from Cloud Functions, you will see that there is no support for connecting to SQL Server Beta from Google Cloud Functions.
This page is not available for SQL Server database engine

Does traffic from App Engine to Cloud SQL travel over the internet or on Google internal network?

We have this discussion in our office and can not come to a conclusion. So I am reaching out here for some advice.
We have a Google Cloud SQL running with no public IP. Google App engine from different App Engine project connect to this single cloud SQL by authorizing their service account.
There are no VPC setup between the projects. The apps are on google app engine standard environment. The instance's private IP is not used in the app projects.
The connections between the projects are made using the tutorial found here
https://cloud.google.com/sql/docs/mysql/connect-app-engine
creating an connection string as
mysql+pymysql://<db_user>:<db_pass>#/<db_name>?unix_socket=/cloudsql/<cloud_sql_instance_name>
The question is how does the traffic flow from other App Engine projects to this Cloud SQL instance?
Does the connect handshake go via the internet (ie outside Google's Network) or does google handles the traffic and routes it internally without the request ever going to the internet?
It would be a great help if any one can help answer these questions.
The answer to this actually varies depending on which version of App Engine you are using.
On older versions of App Engine Standard, the /cloudsql/ unix socket connected over an internal network directly to your instance.
On more recent versions of App Engine Standard, it uses a version of the Cloud SQL proxy to authenticate your connection to the instance via it's public IP. This is why the Connecting from App Engine page states your Cloud SQL instance must have a public IP.
If you have configured your Cloud SQL to use a Private IP address then connectivity occurs using VPC Network Peering and your communication from your Google App Engine (running inside Google and VPC connected) to your managed Cloud SQL (running on a separated VPC Network) is all internal within Google using VPC.
Details on this can be found in the article here:
Introducing private networking connection for Cloud SQL
Private IP (MySql)
Configuring private IP connectivity (PostgreSQL)
The connection from the App Engine Standard to the Cloud SQL instance it is made over the internet. There are more internal services between the App Engine and the Cloud SQL, but the calls are not made to the private IP of the instance, by default.
If you look in the readme of the connector's repository you can see that you can use almost the same method to connect to the Cloud SQL instance from your local env. That might be a clue that things happen on the internet.

MySqlException: Unable to connect to any of the specified MySQL hosts. Asp.netCore application on Google Cloud Platform

I have developed an application in asp.net core EF, with MySql DB. Application is deployed on Google Cloud App Engine. I am trying Connecting Google Cloud App Engine application to Cloud MySql under same cloud project. Locally i am able to connect this database, but when deployed on google cloud i am getting error
MySqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
MySql.Data.Common.StreamCreator.GetTcpStream(MySqlConnectionStringBuilder settings)
MySqlException: Unable to connect to any of the specified MySQL hosts.**
MySql.Data.MySqlClient.NativeDriver.Open()
I am using below connecting string
"DefaultConnection": "host=33.444.**.**;port=3306;database=mYdb;user=root;password=pASS"
img
Take a look at the "Connecting from App Engine" page here on the Cloud SQL docs. You need to enable the Cloud SQL server in your app.yaml, and then connect via 127.0.0.1:5432.

Cannot connect to cloud SQL [SQLSTATE[HY000] [2002] No such file or directory]

I have set up 2 projects in my Google Cloud console. The following are the two projects that I have set up in my console.
1. Cloud SQL + App Engine
2. App Engine (New)
So the idea is App Engine (1) is running the same database as App Engine (2). I have already set up the IAM Permission Page and IAM Admin and Project Page.
I have given both projects as Project Editor role too, but still my (2) still can't connect to my (1) database.
Any help please?
Granting access to an application does not automatically enable a database user account to connect to the instance.
You may connect through a proxy, in which case you should follow these steps:
Enable the API
Install the proxy client on your local machine
Determine how you will authenticate the proxy
If required by your authentication method, create a service account
Determine how you will specify your instances for the proxy
Start the proxy
Update your application to connect to Cloud SQL using the proxy
You can find related details on the Connecting to Cloud SQL from External Applications.
This documents provides steps that cover configuring access for IP connections as well.
If you connect from within the app engine environment, you may have a look at Connecting from App Engine.

Resources