Search code examples
mysqlnode.jsgoogle-cloud-platformgoogle-cloud-functionsgoogle-cloud-sql

Connecting Cloud Function (nodejs) to CloudSQL mySQL database


I'm trying to connect a Google Cloud Function (Gen 2, nodejs 18) to a CloudSQL mySQL database. They are both in the same Project and in the same region in Google Cloud.

Frankly, I'm wrapped around the axle trying to connect for hours now and countless web pages.

I was following: https://cloud.google.com/sql/docs/mysql/connect-functions#node.js But that only gives me the connection part. I'm not clear how to best use the pool that it sets up. It links me to code in GitHub, but that code has much more functionality and complexity than I'm ready to do. I just want to read from a simple table.

I've double-checked all my connections. I'm able to connect to the table via mySQL Workbench using the user and password. I've given Cloud SQL Client permissions to the service account. I just can't believe it's this hard to connect a Google product to a Google product on the same project.

Can someone just give me the proper code that I can cut & paste and add my credentials to read a table from my database? I'm looking for everything from "const mysql = require('promise-mysql');" to returning the values from the database in the console log. I can work through the rest if I can just figure out this connection.

On the database, both public and private IP connectivity is enabled.

If there is anything else I need to setup, please let me know.

My latest version is:

var mysql = require('mysql');

var con = mysql.createConnection({
  user: '***********',
  password: '***************',
  database: '***********',
  socketPath: '/cloudsql/******************'
});

con.connect(function(err) {
  if (err) throw err;
  con.query("SELECT * FROM **********", function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});

But from that I get the error: connect ENOENT /cloudsql/<> at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)

I would appreciate some guidance.


Solution

  • Much appreciation to @lostest in the earlier response, which helped me get closer to the answer. In particular, the Connectivity Tests helped me tremendously.

    The answer is to configure the related Cloud Run properly. Specifically, I needed to navigate to the Cloud Run instance associated to my Cloud Function*. Click on "Edit & Deploy New Revision" and then scroll down to "Cloud SQL Connections" and select my Cloud SQL instance name.

    To get the Connectivity test to work, I needed to make sure to select the Public IP and port 3306.

    Also of note is that I'm connecting through Cloud SQL Auth Proxy (still a little fuzzy to me) and not a VPC. This means I'm connecting through the public IP rather than the internal private IP.

    *As it turns out, I left out an important fact above. I was running a Gen 2 Cloud Function, which uses Cloud Run.

    Also, for the record, my connection string is simply:

    var con = mysql.createConnection({ user: '*********', password: '***********', socketPath: '/cloudsql/***************' });