2

When i try to connect to my MySQL, server sends me an error Error: connect ETIMEDOUT

Here is sample code

const mysql = require('mysql');
const mysqlConfig = {
        connectionLimit: 1,
        host: "remote_host_ip",
        user: "server_user",
        password: "server_pass",
        database: "server_db",
        port: 3306
    };
    mysql.createConnection(mysqlConfig).connect(function (err) {
        if (!err) {
            console.log("Database is connected");
        } else {
            console.log("Database is not connected " + err);
        }
    });

I am on firebase blaze(pay as you go) plan.

mj sunny
  • 119
  • 1
  • 1
  • 10
  • Does your MySQL database allow connections from external IP addresses? – Chris Dec 03 '18 at 13:02
  • Is your project on a paid plan or on the free/Spark plan? If the latter, then calls to non-Google services are blocked to prevent abuse. See https://stackoverflow.com/questions/42784135/cloud-functions-for-firebase-billing-account-not-configured – Frank van Puffelen Dec 03 '18 at 15:52
  • @Chris you are right. My MySQL database does not allow connections from external IP address. – mj sunny Dec 04 '18 at 03:42

1 Answers1

1

I know the question title already states it, but you are not using GCP Cloud SQL, right?

In that case, there's a great possibility of your MySQL server is not reachable from Cloud Functions. Are you sure network connectivity is OK?

Besides, even in Cloud Functions, it's a good idea to use connection pools. Consider using it, it will be something like that:

  var config = {
    user: 'root',
    password: 'akdaskdasdaE',
    database: 'database1'
  }
  config.connectionLimit = 10
  config.multipleStatements = true

  // needed in GCP Cloud SQL, but it seems it's not your case
  // config.socketPath = `/cloudsql/__INSTANCE_CONNECTION_NAME__`

  var connectionPool = mysql.createPool(config)
  connectionPool.on('connection', () => {
      console.log(`[connectionPool] new connection opened`)
  })

  // then you use connectionPool.getConnection(..)
Feu
  • 5,372
  • 1
  • 31
  • 57
  • you are correct my MySQL database is not reachable from firebase cloud functions. – mj sunny Dec 04 '18 at 03:46
  • do you mean ur database had a firewall rule ... or is it that firebase cloud functions do not allow out going data connections to another db ? – Aman Satija Jun 29 '20 at 10:05
  • 1
    as far as I know there is no network restriction from cloud functions to data connections in other dbs/networks/cloud providers, etc – Feu Jul 02 '20 at 17:33
  • @mjsunny by the way, if you think that's a correct answer, please accept it! cheers – Feu Jul 02 '20 at 17:34