Exlley Santos
Exlley Santos

Reputation: 31

Cloud Functions - PrismaClientInitializationError: Can't reach database server at `my.cloudsql.ip`:`3306

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.

Upvotes: 0

Views: 4927

Answers (2)

ninoorta
ninoorta

Reputation: 998

In my case, It is because I use the generated password from cloud SQL .Thus, I encode it and my connection string is like

DATABASE_URL=mysql://{USER:{PASSWORD}@{INSTANCE_PUBLIC_IP}:3306/{DB_NAME}

Upvotes: 1

Diego Olalde
Diego Olalde

Reputation: 76

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

Upvotes: 3

Related Questions