Search code examples
mysqlnode.jssequelize.jsgcloudip-address

SequelizeConnectionError: connect ETIMEDOUT Google Cloud Run


I have to build a platform to retrieve data from a sql database on another host,for this project I use js nodes with sequels.Locally I can connect without problems using id from the database and allowing access to my static ip,using the following code instance.

const sequelize = new Sequelize(
config.DB,
config.DBUSER,
config.DBPASSWORD,
{
    host:'DB IP ADRESS',
    dialect:mysql,

    pool:{
        max:config.pool.max,
        min:config.pool.min,
        acquire:config.pool.acquire,
        idle:config.pool.idle
    }
 }
)

sequelize.authenticate()

https://sequelize.org/docs/v6/other-topics/dialect-specific-things/

The problem occurs when I upload the project on google cloud platform, in cloud run I get an ip address that I save on my host to have access but the problem is the following, that ip address is constantly changing.

ErrorSequelizeConnectionError: Host '107.178.333.70' is not allowed to connect to this MariaDB server

Of course I read and was interested in working with VM and buying a fixed ip from google. But after reading some forums on the internet, I would have a question if it would be possible to connect to sql instead of ip to use dns records?

Or using a hostname?

I tried to follow the documentation from the sequel, Dialect-Specific Things, I have integrated tedious modules,I found this example for mssql:

const sequelize = new Sequelize('database', null, null, {
   dialect: 'mssql',
   dialectOptions: {
       authentication: {
        type: 'ntlm',
        options: {
           domain: 'yourDomain',
           userName: 'username',
           password: 'password'
        }
      },
     options: {
         instanceName: 'SQLEXPRESS'
     }
  }
 })

Why am I trying with hostname because in php I managed this connection type:

'hostname' => 'domain.com',
'username' => 'dbUserName',
'password' => 'dbPassword',
'database' => 'db',

What I want is to discover an alternative to the connection by ip address or by dns records or by hostname in case I can't opt for the last option to purchase a fixed ip address.


Solution

  • My suggestion is : Create a VPC Network Peering ( allows internal IP address connectivity across two Virtual Private Cloud (VPC) networks regardless of whether they belong to the same project or the same organization ) between your VPC and your on-prem network where your MySQL Postgres is hosted and then use Serverless VPC Access to connect a Cloud Run service directly to your VPC network.

    Serverless VPC Access connectors make it possible for you to connect directly to your Virtual Private Cloud network from serverless environments such as Cloud Run, App Engine, or Cloud Functions. Configuring Serverless VPC Access allows your serverless environment to send requests to your VPC network using internal DNS and internal IP addresses (as defined by RFC 1918 and RFC 6598).

    Here is the documentation which explains how to set this up.