Question

Unable to connect Node.JS to MySql on same ubuntu machine. Timeout error/ConnectionRefused.

I have written a Node.Js Application for demo purpose to connect to DB. I ma new to DO platform and connecting remote servers. The Node.Js application that I have written, I am able to connect to MySQL DB (present/using Managed DB Cluster provided by DO.) The logic is same, connection string details different. But, that same Node.Js application is unable to establish a connection to the MySQL present on the same ubuntu machine. I have taken a LEMP stack on Ubuntu. On, the LEMP stack ubuntu machine. I have PhpMyAdmin also which is working fine. In addition to LEMP, I have Node installed on the same Ubuntu Virtual Machine. This Node.Js application is unable to establish a connection to the MySQL which is a part of LEMP on this Ubuntu machine. But, the node.js application is able to establish connection to MySQL Managed DB Cluster.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hello,

If you are connecting to the local MySQL installation, you need to try the following:

  • Change the database host from the server IP to 127.0.0.1 so that the Node.js service would connect to the MySQL service locally rather then via the public interface
  • Make sure that the MySQL service is running:
systemctl status mysql

If this is still not working, can you share the exact error that you are getting and also the connection string that you are using? Make sure to remove any sensitive information before sharing the connection string here.

Best,

Bobby