Trouble Connecting to MySQL Database – getaddrinfo ENOTFOUND Error

0
35
Asked By Wanderlust98 On

Hey everyone! I'm struggling to connect to my MySQL database using Node.js. I've set up a `server.js` file with the necessary code to create a connection, but I keep encountering the error: `getaddrinfo ENOTFOUND `. I have MySQL version 2.18.1 installed and am using Digital Ocean. I've tried different configurations, including using and omitting the trusted sources and ports. Interestingly, I can connect through DBeaver without issues. Can anyone suggest what might be going wrong?

4 Answers

Answered By DevGuru On

The `ENOTFOUND` error typically means Node can’t find your DB server at that address. Ensure you're using the exact hostname or IP from Digital Ocean. If you're connecting in DBeaver but not in Node, check for firewalls, network configurations, or VPN issues that might block the connection. Also, filling in all fields (host, user, pass, db) in your connection object is crucial. And yeah, `mysql2` is definitely the way to go nowadays!

Answered By CodeNinja42 On

It looks like you're passing empty strings, which might be overriding the defaults for your connection configuration. If you're only using empty strings, consider not passing the object at all. Otherwise, ensure you fill in the connection details with the right information from Digital Ocean.

Wanderlust98 -

I left it empty on purpose, but I do fill those in from Digital Ocean. I ran a DNS lookup, and now I’m hitting an ETIMEDOUT error instead.

Answered By ByteSmasher On

Make sure to use the full hostname or IP from Digital Ocean as the `host`. You can test if Node.js can access the internet with this code:

```javascript
const dns = require('node:dns');
dns.lookup('google.com', (err) => console.log(err || 'node can access the internet'));
```
If that works, replace `'google.com'` with your Digital Ocean credentials. Also, consider switching to `mysql2`, as it's generally better than the regular `mysql` package.

Wanderlust98 -

Thanks! I added the correct IP address and port again, and it seems to work! But now I'm getting an 'ER_NOT_SUPPORTED_AUTH_MODE' error. I’ll look into that and also check out `mysql2`!

CodingWhiz -

I installed `mysql2`, followed your suggestion, and everything works perfectly now! It feels great to see 'Connected!' in the console, lol.

Answered By TechSavvy123 On

First off, check if your host name resolves correctly. If it doesn’t, that’s likely the cause of the error you're seeing. Make sure you're using the exact host name or IP address provided by Digital Ocean, not just `localhost`.

Wanderlust98 -

Thank you, I'll look into this!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.