Im trying to deploy my NodeJS app to Google cloud functions connected to a Cloud SQL instance (MySQL) and using Prisma ORM.
The deployment was successful, but whenever I access an API route with a connection to the database, I get the following error as a response:
PrismaClientInitializationError: Can't reach database server at `my.cloudsql.ip`:`3306`. Please make sure your database server is running at `my.cloudsql.ip`:`3306`.
My database string looks like this: "mysql://user:password@cloud-sql-ip/database?host=/cloudsql/instance-connection-name"
I alread try adding ?connect_timeout=300
to the connection string of the database as mentioned here. But I didn't succeed.
It also took me a while to figure this out, here's the URL that worked for me:
DATABASE_URL=mysql://{USER}:{PASSWORD}@{INSTANCE_PUBLIC_IP}:3306/{DB_NAME}?socket={INSTANCE_CONNECTION_NAME}
Then, in your schema.prisma:
datasource db {
provider = "MySQL"
url = env("DATABASE_URL")
}
ALSO, remember to update the IAM permissions in your Cloud Function/Cloud SQL instance to authorize the Cloud function to read from DB: https://cloud.google.com/sql/docs/mysql/connect-functions