简体   繁体   中英

Google Cloud SQL connection in NodeJS app (Express) --> Error: connect ENOENT

I'm trying to deploy my NodeJS app on Google Cloud services and getting the following error hen I try to call a database query using Postman through localhost/8080, which is where my server is listening: connect ENOENT /cloudsql/<MY_CONNECTION_NAME> . Here's my database connection file, config.js :


const mysql = require('mysql');
const dotenv = require('dotenv');

dotenv.config();

const {
  DB_PASS,
  DB_USER,
  DB_NAME,
} = process.env;

const config = {
  user: DB_USER,
  password: DB_PASS,
  database: DB_NAME,
  socketPath: `/cloudsql/${process.env.CLOUD_SQL_CONNECTION_NAME}`,
};

const connection = mysql.createConnection(config);

// create connection
connection.connect((err) => {
  if (err) throw err;
  console.log(`Connected to database ${DB_NAME}`);
});

module.exports.connection = connection;

I know that Google recommends using a pool to connect, but I'm afraid that doing that will require me rewriting all my database queries, and I'm on a tight deadline.

I've been able to successfully shell into the database with MYSQL using the terminal.

Take a look at the Connecting to App Engine page. In particular, here are some things you should check if the socket isn't present:

  1. For GAE Flex, make sure you have the following in your app.yaml :
beta_settings:
  cloud_sql_instances: <INSTANCE_CONNECTION_NAME>
  1. Ensure the SQL Admin API is enabled and make sure you have the correct IAM permissions ( Cloud SQL Client1 or higher) on your service account ( service-PROJECT_NUMBER@gae-api-prod.google.com.iam.gserviceaccount.com`). If between projects, make sure you have the

  2. Make sure you are spelling <INSTANCE_CONNECTION_NAME> correctly. It should be in the format <PROJECT>:<REGION>:<INSTANCE> - you can copy it exactly from the "Instance Details" page for your instance.

Additionally, using a connection pool will have no effect on your queries. Using a pool only means that when you "open" a connection, it is actually reusing an existing connection, and when you "close" a connection it puts it back in the pool for your application to use elsewhere. The queries you perform using the pool should be exactly the same.

this is happen if you are using flex as env in app.yaml to run without errors remove the env:flex from your app.yaml , your app.yaml need to look like this app.yaml

and you will successfully connected to the cloud sql without causing errors console log

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM