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
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
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.
How to connect Google Cloud SQL with Apps (like Google App Engine) and Tools like (mySQL workbench) in a secure way?
In this document we'll see how to create a Google Cloud SQL Instance and connect them in your Google App Engine application and MySQL Workbench admin tool.
Google Cloud SQL:
Google Cloud SQL is a fully-managed database service that makes it easy to set-up, maintain, manage and administer your relational MySQL databases in the cloud.
Google Cloud SQL provides a relational database that you can use with your App Engine application. Cloud SQL is a MySQL database that lives in Google's cloud.
refer:
https://cloud.google.com/sql/
https://cloud.google.com/sql/docs/
Creating SQL Instances:
A Google Cloud SQL instance is a MySQL database hosted in Google's cloud.
Go to the Cloud SQL Instances page in the Google Cloud Platform
Console (https://console.cloud.google.com/sql/instances) and Click
Create instance.
Click Choose First Generation, Enter a name and Choose a tier for
the instance and Click Create.
After the instance finishes initializing, select the instance to
open it.
In Access Control > Users, Click Create user account and create a
user with name root and specify a password (root_password). This
creates the MySQL user 'root'#'%'.
In Databases, Click New Database and create a database with a
DataBase name (DataBase_Name)
MySQL Workbench:
MySQL Workbench is a unified visual tool for database architects, developers, and DBAs. MySQL Workbench provides data modeling, SQL development, and comprehensive administration tools for server configuration, user administration, backup, and much more.
refer http://www.mysql.com/products/workbench/
Now we'll see how to connect to your Google Cloud SQL instance database with MySQL Workbench.
Configuring access
Go to the Cloud SQL Instances page in the Google Cloud Platform
Console and select the instance.
In Access Control > IP address, Click Request IPv4 adddress and copy
it(Instance_IPv4_address). It is needed to connect your Google Cloud
SQL instance database with Admin tools like MySQL Workbench.
note: You will be charged for IPv4 address # $0.01 each hour the instance is inactive and $0.1 each hour the instance is active
Google 'ip address' to find your public IP address
In Access Control > Authorization > Authorized networks, click Add
network and enter your IP address.
In Access Control > Users, Create a user with username (userName),
password (password) and the option 'Allow any host selected'. It is
recommended to use a seperate user account to access from WorkBench
Connecting
In the MySQL Workbench home view, click New Connection.
In the Setup New Connection window, provide a Connection Name,
Hostname and Username
Click Test Connection. You will be prompted for a password.
Once the MySQL connection is made successful, Click OK and click on
the saved connection to open SQL Editor
Google App Engine:
Google App Engine is a platform for building scalable web applications and mobile backends. App Engine will scale your application automatically.
refer https://cloud.google.com/appengine
Now we'll see how to set up a connection between an App Engine application and a Cloud SQL instance.
Configuring access
Go to the Cloud SQL Instances page in the Google Cloud Platform
Console and select the instance.
In Access Control > Authorization > Authorized App Engine
applications, click Add application ID and enter the application ID.
Click Done and Save.
In Overview > Properties Copy the 'Instance connection name'
(Instance_Connection_Name)
In your Google Web Application Project,
war/WEB-INF/appengine-web.xml add,
true
Code sample:
An Exaple for Google App Engine - Java Standard Environment
public static Connection connect() throws ClassNotFoundException, SQLException {
String url = null;
{
if (SystemProperty.environment.value() == SystemProperty.Environment.Value.Production) {
// Connecting from App Engine.
Class.forName(Messages.getString("com.mysql.jdbc.GoogleDriver"));
url = Messages.getString("jdbc:google:mysql://{{Instance_Connection_Name}}/{{DataBase_Name}}?user=root&password={{root_password}}");
} else {
// Connecting from an external network or localhost
Class.forName(Messages.getString("com.mysql.jdbc.Driver"));
url = Messages.getString("jdbc:mysql://{{Instance_IPv4_address}}:3306/{{DataBase_Name}}?user={{userName}}&password={{password}}");
}
Connection conn = DriverManager.getConnection(url);
return conn;
}
}
I would like to add to what Newton said:
The instance connection name is not just your "project_id:instance_id". Please go to the Google Cloud SQL Instance Overview page and copy the value in 'Instance connection name' field. Often 'Instance connection name' has region names or other values included, so be careful to replace it.
I have a Google Cloud SQL project : cwdataproject (Project ID). I have created a schema named cwdb in it. Its Instance ID is : cwdataproject:cwinstance
I have provided access and privilege to cwdb for root user "root"
I have another app Google App Engine : messageframework (Project ID)
Project ID: messageframework
I have provided Authorized App Engine Applications in cwdataproject for messageframework
I have provided Authorized Networks for my local IP. and I am able to connect from local My SQL client successfully.
My code to connect Google Cloud SQL from messageframework app is as below.
Code:
Class.forName("com.mysql.jdbc.GoogleDriver");
url = "jdbc:google:mysql://cwdataproject:cwinstance/cwdb?user=ajoysinha;
DriverManager.getConnection(url);
But is is giving the following error
Access denied for user 'root'#'localhost' (using password: NO)
How can we resolve this connectivity problem?
I am able to connect Cloud SQL from my local MySql client and do all the operation. but not from GAE.