I need to deploy a node.js app that can interact with a database. I've already configured the Cloud SQL Database, for which I've whitelisted my computer public IP. When I run my app locally, everything is fine and I can access my data. However, when it comes to deployment with Google App Engine, I can't figure out how to connect to my database. What I've done so far :
app.yaml
file which contains runtime: nodejs12
.localhost
(as both the GAE and Cloud SQL are hosted on the same server) or the full INSTANCE_CONNECTION_NAME
.Cloud SQL Client
role to the appropriate service, on the IAM Admin PanelI read on the documentation that I may have to use a proxy. I don't really know what to do about that, so I didn't do anything. Do you thing it is a way worth exploring ?
Thank you in advance.
The documentation pages are here:
https://cloud.google.com/sql/docs/mysql/connect-app-engine-standard
For Public IP connection:
Connecting with Unix sockets
Once correctly configured, you can connect your service to your Cloud SQL instance's unix domain socket using the format:
/cloudsql/INSTANCE_CONNECTION_NAME
These connections are automatically encrypted without any additional configuration. The code samples shown below are extracts from more complete examples on the GitHub site. Click View on GitHub to see more.
Warning: Linux based operating systems have a maximum socket path length of 107 characters. If the total length of the path exceeds this length, you will not be able to connect with a socket from App Engine.
cloud-sql/mysql/mysql/server.jsView on GitHub
const dbSocketPath = process.env.DB_SOCKET_PATH || "/cloudsql"
// Establish a connection to the database
return await mysql.createPool({
user: process.env.DB_USER, // e.g. 'my-db-user'
password: process.env.DB_PASS, // e.g. 'my-db-password'
database: process.env.DB_NAME, // e.g. 'my-database'
// If connecting via unix domain socket, specify the path
socketPath: `${dbSocketPath}/${process.env.INSTANCE_CONNECTION_NAME}`,
// Specify additional properties here.
...config
});
}
If you're connecting with Private IP, ignore all that, and you can just use the Private IP with port 3306 (for MySQL) or 5432 (for Postgres).