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

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

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}

Related

Connect to Google Cloud MySQL Database

I'm trying to connect to my MySQL database using a soft called Beekeeper-studio. I created a SQL instance, inside I created a database (let's called it myDatabase) and a username and password (test and testPassword). And I tooke the public IP address on the overview page of this instace.
So I'm trying to make a connection to the db with the URL like that: mysql://test:testPassword#XX.XX.XX.XXX:3006/myDatabase
So when I'm trying to connect, i have a timeout error: connect ETIMEDOUT XX.XX.XX.XXX:3006
I'm trying to connect to the db before using it on my nodejs API. Any idea?
You'll need to either:
Add your machine's IP to an authorized network, or
Run the Cloud SQL Auth Proxy on your machine.

Google Apps Script won't connect to MySQL database but will connect to free sql database server

I'm trying to use Google Apps Script to connect to an SQL database provided by my hosting company. I know the credentials are correct because I'm able to access the database via PHPMyAdmin with no problem.
When I try to connect via Google Apps Script it's not working and I only get the exception error as follows:
Exception: Failed to establish a database connection. Check connection string, username, and password.
I've tried/I'm Using:
Appending the /"useSSL=false at the end of the URL as recommended by other forums
Jdbc.getConnection(url,user,pwd) for external databases
Port 3306
Creating a new database with new credentials to see if it was a corrupted database
Is there any way that it may be a setting inside PHPMyAdmin or any privileges I need from my hosting company?

AWS IAM Authentication to MySQL from dotnet core code running in Lambda

I have a lambda function written in dotnet core 2.1. This code uses the Oracle MySql provider (v8.0.19). A MySQL RDS (v5.7.26) is running with a user configured to use the AWSAuthenticationPlugin as described in the AWS article linked below.
I'm able to connect to this RDS using a normal username/password combination, but I want to use IAM Authentication instead. This article describes how to do this using the mysql client on a linux server: https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.IAMDBAuth.Connecting.AWSCLI.html
I've created the IAM policy and I'm able to load the token in the lambda function via RDSAuthTokenGenerator.GenerateAuthToken. The connection string is built as follows:
MySqlConnection c = new MySqlConnection();
MySqlConnectionStringBuilder csb = new MySqlConnectionStringBuilder();
csb.Server = "dbinstanceName.xxx.us-xxx-1.rds.amazonaws.com";
csb.Port = 3306;
csb.Database = "Database";
csb.UserID = "DatabaseUserName";
csb.Password = RDSAuthTokenGenerator.GenerateAuthToken("dbinstanceName.xxx.us-xxx-1.rds.amazonaws.com", 3306, "DatabaseUserName");
csb.SslMode = MySqlSslMode.VerifyFull;
c.ConnectionString = csb.ConnectionString;
When I pass this token as the value for password in the connection string, I get the exception:
"errorType": "MySqlException",
"errorMessage": "Authentication method 'mysql_clear_password' not supported by any of the available plugins."
This MySQL article describes Client-Side cleartext pluggable Authentication (https://dev.mysql.com/doc/refman/5.7/en/cleartext-pluggable-authentication.html), however I don't know how to enable this plugin in the MySQL provider in dotnet.
Can anyone suggest a way to send this RDS Token in the connection string in clear text in dotnet core? I can't seem to figure out how this is done.
I was able to make this work using a different MySQL provider. If you are stuck, try using the MySqlConnector provider instead:
https://mysqlconnector.net/
With this provider, I did not have to change anything about the code/connection string above.
Hope this is helpful for someone else.

What is the connection string for Google Cloud mySQL, in node.js?

The scenario is as follows:
I am writing an node.js app locally and want it (the mySQL modul, rather) to connect to a mySQL db running on Google Cloud Platform.
I can not get the connection string right and keep getting errors. GCP holds two names for the database - one i picked, one generated, more complex. Problably I should supply one of these into the connection string as "database", the host is likely the IP address with port (or not(?)), I am not sure what the "user" should be, or if it is necessary. Not enough info on the GCP help pages or elsewhere.
tl;dr: I need the following to connect a node.js app with a mySQL module remotely to a GCP database:
var con = mysql.createConnection({
host:"11.222.333.444:3306",
user: "me"
password:"passwo3d",
database:"projectname-1528892969949:europe-west5:dbname"
});
Thank you!
The raw connection string should look something like: mysql://user:password#localhost:port/db
So I think your code should look something like:
var con = mysql.createConnection({
host:"11.222.333.444", <-- remove the port from your string
user: "me"
password:"passwo3d",
database:"dbname" <-- you have it as the instance name .. its just suppose to be the DB name
});
You can also try connecting to the instance using the cloud proxy setup. You host would just be localhost.
Finally you can always go the GCP console -> go to your instance and open up a shell. I think they print out the gcloud cmd in there and you can copy values from the console as well.
Depending on how you are pushing your code, you may have to change a few connections strings in your manifests as well. If you can share those I can help further.
For that to happen, you have to first enable remote access for your mysql on your GCP. A step by step guide is over here, you can leave the first few steps for installing mysql.

Google Apps Script JDBC connection error - Amazon RDS MySQL

I followed the Google Apps Script JDBC example shown at https://developers.google.com/apps-script/jdbc but have been unsuccessful connecting to a MySQL database hosted at Amazon. My connection string looks like this (scrubbed):
var conn = Jdbc.getConnection("jdbc:mysql://myAmazonhost:3306/", "myUser", "myPass");
I'm able to connect remotely to the Amazon MySQL database from my development system using both MySQL Workbench and Navicat and have checked/verified the host name, username and password in the Google script.
I added the Google servers listed under Accessing Local Databases to the Amazon Security Group but no luck. I keep getting the error:
Failed to establish a database connection. Check connection string,
username and password.
I'm probably missing something simple. Anyone else connecting from a Google script to an Amazon MySQL database? I'd sure appreciate any advice. Thanks.
(Edit...)
Sorry, I inadvertently removed the database name when I sanitized my example above. The DB name is specified in the code.
var conn = Jdbc.getConnection("jdbc:mysql://myAmazonhost:3306/myDB", "myUser", "myPass");
Twelve hours after I posted this question, the script started working--no database connection errors. I built a report that created a spreadsheet using information retrieved from the Amazon MySQL database. (So cool to see all this working in the cloud.)
But... I hadn't made any changes to the JDBC connection string. And no changes were made to the Security Group configuration on the Amazon side. It just started working.
This morning, I started working on the script. I didn't get very far--the big ugly red database connection error message popped up. No changes to code. It just stopped working. I verified the MySQL database is up and accessible from my desktop admin program. No issues.
Is anyone else experiencing erratic connections from Google scripts to external databases? I'm hoping this is just an isolated glitch.
Turns out that the issue was related to the database engine version. I created a new instance using engine version 5.5.25a and the connection worked. I went back to my original database and modified it from 5.5.8 to 5.5.25a and it worked as well. Not sure what's going on with Google Apps Script but changing the engine version to 5.5.25a fixed the problem.
You have to specify which database to connect to. Try
var conn = Jdbc.getConnection("jdbc:mysql://myAmazonhost:3306/DB_NAME", "myUser", "myPass");