1

I am trying to connect to my database from my Nodejs server like this:

const db = new Sequelize(sql.database, sql.username, sql.password, sql.config);

const connectDB = async () => {
  try {
    await db.authenticate();
    logger.info(`MySQL DB Connected: ${sql.config.host}`)
  } catch (error) {
    logger.error(`Error: ${error}`);
    process.exit(1)
  }
}

My configuration looks like:

const sql = {
 database: process.env.SQL_DATABASE as string,
  username: process.env.SQL_USER as string,
  password: process.env.SQL_PASSWORD,
  config: {
    dialect: "mysql" as Dialect,
    host: "/cloudsql/{instance}" as string,
    dialectOptions: {
      socketPath: "/cloudsql/{instance}",
    },
  },
};

However I am getting a sequelizeconnectionerror: connect ENOENT

Is there anything I'm missing to connect to my Cloud SQL instance in GCP?

Donnald Cucharo
  • 3,866
  • 1
  • 10
  • 17
tryingToBeBetter
  • 365
  • 1
  • 6
  • 16
  • ENOENT means [Error NO ENTry (or Error NO ENTity)](https://stackoverflow.com/a/19902850/16531441). Are you trying to connect to the Cloud SQL Public IP or Private IP? If public IP, have you set up Cloud SQL Auth proxy? Also, what format did you use on `{instance}` value? Additionally, where is your code running? App Engine Standard, Flexible, Cloud Functions? – Mabel A. Dec 29 '21 at 05:29

2 Answers2

0

Your sql.config object is missing a "port" key to access the database at. The default port in MySql is 3306.

Depending on your database setup you should also add ssl configuration options.

If you have SSL setup you'd do that by adding a key "ssl" to your dialectOptions with an object containing "key", "ca" and "cert" passing the appropriate certificate to each of them.

Your code would then look like this:

const sql = {
  database: process.env.SQL_DATABASE as string,
  username: process.env.SQL_USER as string,
  password: process.env.SQL_PASSWORD,
  config: {
    dialect: "mysql" as Dialect,
    host: "/cloudsql/{instance}" as string,
    port: process.env.SQL_PORT,
    dialectOptions: {
      socketPath: "/cloudsql/{instance}",
      ssl: {
        key: process.env.SQL_KEY,
        ca: process.env.SQL_CA,
        cert: process.env.SQL_CERT
      }
    },
  },
};
0

I was running into a similar situation and eventually resolved it by removing various options. Here is a minimal configuration using Sequelize to connect via Public IP Address to Google Cloud SQL:

const sequelize = new Sequelize('DATABASE_NAME', 'USER', 'PASSWORD', {
  dialect: 'mysql',
  host: 'PUBLIC_IP'
});

Couple gotchas:

  • Make sure you also have the appropriate network added to Authorized Networks in the connections settings of the instance dashboard.
  • Make sure you have added a database to your instance, and are referring to that database name and not the instance name.
  • Add SSL configuration or ensure "SSL required" option is unchecked in Google Cloud instance networking options.

This setup has worked for me in a Next.js/React app with Auth.js using @next-auth/sequelize-adapter.

Cody P
  • 41
  • 1
  • 7