skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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/***************' });


  2. Several things:

    • It looks like you’re including the socket path instead of the host information (the server’s ip address.) The rest of your code looks good

    • For troubleshooting purposes, I’d remove the Query and add it back in after you make sure your connection is good

    Try this:

    var mysql = require('mysql');
    
    var con = mysql.createConnection({
      host: "localhost",
      user: "yourusername",
      password: "yourpassword"
    });
    
    con.connect(function(err) {
      if (err) throw err;
      console.log("Connected!");
    });
    • If you’re still having connectivity issues, make sure you’ve also added your home/office/web server network’s IP Address to be allowed to connect to Google Cloud. Try both an Inbound and Outbound test through your Google Cloud mySQL Connection Menu (server IP to your IP and vice-versa.)

    • If you have SSL enabled, you’ll need to generate a certificate and include some additional code for your connection

    • Make sure you’ve setup tables on your mySQL server and inserted some data into them or else there won’t be anything for the query to return


    2023.04.29 Reply to @YKStacker (too long for comments):

    The localhost should be the IP address of the Google Cloud mySQL Database you’re attempting to connect to. If you go to your Google Cloud SQL Overview Page, it’s located under "Connect to this instance."

    Then, go to the "Connections" page located on the menu to the left, then go to the "Connectivity Tests" Tab. Click "Create"

    • Inbound
    • TCP
    • Current Cloud Instance
    • Other
      • IP address
        • Enter the External IP for the network that hosts your mySQL Workbench/Webserver
        • If it’s the network you’re currently on, just go to IP Chicken to get it
    • Uncheck "This is n IP address used in Google Cloud
    • Make sure Firewall settings for your local network allow incoming traffic on Port 80
    • Create

    This is going to test your ability to receive data from Google Cloud.

    Next, go back to "Connectivity Tests" and create another connection that will test your ability to send data to Google Cloud (just reverse the order, but check the box showing the ip used in Google Cloud and select your project.)

    If this doesn’t work, go to the "Networking" tab and make sure to add your Public IP address to "Authorized Networks" and try again.

    If these tests fail, there is something wrong with your connection settings.

    Here is more info about settings for package.json: Google Cloud – Specifying Dependencies for Node.js

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search