19

I'm running a server on c9.io using Node.js and trying to connect to Mysql I guess I get this error:

Error: getaddrinfo ENOTFOUND

because the connection to the db is wrong.

I'm using this:

var connection = mysql.createConnection({
    host: "REMOTE_ADDR",
    user: "MYUSERNAME", // this is replaced by my username
    database: "c9",
    port: 3306
});

Any idea what's wrong?

Thanks!

hong4rc
  • 3,999
  • 4
  • 21
  • 40
Nathan
  • 223
  • 1
  • 3
  • 7

6 Answers6

28

After one hour, and I don't know why, I found the issue.

In my case I replaced

host: 'localhost'

by

host: '127.0.0.1'
João Pimentel Ferreira
  • 14,289
  • 10
  • 80
  • 109
17

I know it's been a while since this was asked but I spent the better part of today trying to fix this. What to check for:

Within your nodejs source:

  • Try it without the 'port' option unless on your db VM, MySQL is listening on a port besides 3306. I had this option specified but had connection problems until I removed it. And 3306 is already the default value for the option anyway.
  • Try it with the actual host IP in the host 'option'. I was using an actual domain name and, more often than not, node-mysql would throw the same exact "errorError: getaddrinfo ENOTFOUND" When I used the IP address, no more errors. Also, I did not get this error while on Ubuntu instances in AWS. As soon as I switched over to Ubuntu VMs in Azure, I got the problem.
  • Set the "debug" connection option to true which will give you a verbose trace of what's happening during the connection

On your db VM/Box/Instance:

  • Ensure that MySQL is listening on the right port
  • If you're seeing the error when trying to make concurrent connections using a pool, check that your max_connections and max_user_connections haven't been changed from the default settings in my.conf
  • Monitor "SHOW PROCESSLIST;" in MySQL to see if you see any issues there
Scott Graph
  • 537
  • 5
  • 11
5

This code works for me.

var mysql = require('mysql');

var con = mysql.createConnection({
    host: '127.0.0.1',
    port: '3306',
    user: 'yourUsername',
    password: '**********'
});

con.connect(function(err){
    if(err) throw err;
    console.log('connected!');
});

the host and port name find them in your mysql server. in-place of '127.0.0.1' you can use 'localhost'

daniel ernest
  • 345
  • 3
  • 9
  • Code-only answers are discouraged. Please click on [edit] and add some words summarising how your code addresses the question, or perhaps explain how your answer differs from the previous answer/answers. [From Review](https://stackoverflow.com/review/low-quality-posts/22127779) – Nick Feb 06 '19 at 00:08
0

I was facing this issue. I did fix it simply you have to change your configuration of a connection string like if you are running on local machine try

host:'localhost' or host:'127.0.0.1'

...and set your user name and if you want this to publish your code on server then give the host according to server if in docker container give it name host:'db'.

Zain Arshad
  • 1,885
  • 1
  • 11
  • 26
zshan4444
  • 380
  • 3
  • 7
0

I almost spend two hours to understand what is the problem with the mysql database. I am using Windows cmd. I tried multiple ways e.g

  1. npm install mysql2
  2. Changing localhost to 127.0.0.1
  3. Refreshing node

so in order to resolve this issue

[Error: getaddrinfo ENOTFOUND 127.0.0.1:3306]

I open XAMPP and start the MySQL database then manually check the credentials and works fine for me.

var mysql = require('mysql');

var connection = mysql.createConnection({
  host    : 'localhost',
  user    : 'test',
  password: 'test123',
}); 
Omore
  • 614
  • 6
  • 18
0

I have also stumbled upon this problem.

you it is important that you set the port into the 'port' variable. you can not do it like that: host: "IPADDRESS:Port" like host: "127.0.0.1:3306"

this has solved the issue for me.

If this is already correct check out the connection via terminal using telnet ip port for example telnet 127.0.0.1 3306. If telnet gives back an immediate error than the connection did not work but if telnet is stuck then the connection could work. This helped me too. The idea to check it this way you can find here