Skip to content
loading...

Connecting to db with nodejs

github logo ・1 min read  

Hi everyone, I have a question about backend with node js. I am trying to connect to a server with ssh and after that connect to the db with login pass in another server. SSH part is successful however I can not connect to the db, the error is connect ETIMEDOUT. I am using sequelize ORM and ssh2, net packages. Can someone pls come up with ideas why this can happen?

twitter logo DISCUSS (8)
markdown guide
 

Timeout usually points to the database hostname not being accessible. This can be caused by it not being running, or more commonly security configuration like a firewall. Can you connect to the database from the remote terminal, but not node? Or can you not connect at all?

What database engine are you using?

 

Thanks for contact!. I can connect with terminal and mysql workbench but not with node. The db engine is innoDB.

 

Next most likely culprit would be your ORM configuration. Have you worked with these packages before?

Yes. I can connect to a db located in the server which I connect via ssh and I do this with the same packages. However this time the db is not in the same server as the server I connect via ssh.

So your application is on Server A, which you can ssh into, and your database is on Server B.

You can connect to the database server via MySQL workbench from your local system, and you mentioned you can connect to the database via the command line.

Is the command line connection initiated locally or from Server A?

Where are your servers hosted? Is it a cloud service or custom install?

The command line connection initiated locally. From my local terminal I can ssh into server A and then enter server B. DB on server B is accessible only from server A via login password. The servers are custom install.

It sounds like you're connecting to server A via ssh, then connecting to server B via ssh, and then connecting to the database from the command line on server B.

If this is true, then this won't be an effective troubleshooting step.

Instead, ssh into server A, and then try to connect to the database server (not the computer it's running on) via the mysql command line on server A.

  • ssh into server A
  • run mysql -h IP_ADDRESS_OF_SERVER_B -u MYSQL_USERNAME -PPASSWORD

Note that there is no space between the -P and the password. A live example might look like: mysql -h 35.123.0.10 -u myUser -PreallySecurePassword123

If you can connect successfully using the above, then your connection details in your code (through your ORM/DB configuration) are wrong.

If you can't connect using the above, then there are network issues like a firewall that is blocking traffic either incoming or outgoing between server A and server B.

Your next step would be to troubleshoot that by reviewing the networking setup of those two machines, using pings or traceroutes as a starting point.

Yes, it seems the code part has issues. CMD ssh and mysql part were done the way you said and they worked. Thanks a lot and will come back with more questions if needed!

Classic DEV Post from Aug 27 '19

How do *you* pronounce sudo?

Not necessarily how it's "supposed" to be pronounced, what do you say? (in your head or aloud) H...

MihranH profile image
Hi everyone, I am js/reactjs/nodejs developer.