0

I'm trying to connect to a MYSQL database using the npm MYSQL library(https://www.npmjs.com/package/mysql), but I'm getting a error that the server can't be connected to. When I go into the server, I can see the DB, and I validated my credentials and they're valid. My app lives on our seperate server with IP address ending in 123.75, while the DB is on 123.74. I'm using a route I setup on Express to call the DB. I've listed the Express & NPM MYSQL code below (credentials redacted), along with the error I'm receiving.

NPM MYSQL:

 const config = sql.createConnection( {
    host: '123.74:3306',
    user: 'root',
    pass: '****',
    database: '****'
});

Express Route:

app.use('/t',function(req,res){
      config.connect(function(err){
          if (err){
              res.send(err);
              return;
          }
          res.send('connected');
      });
      config.end();

  });

Error:

{"code":"ENOTFOUND","errno":"ENOTFOUND","syscall":"getaddrinfo","hostname":"123.74:3306","host":"123.74:3306","port":3306,"fatal":true}

Update:

I found the issue in the library, and posted a solution.

Justin E. Samuels
  • 867
  • 10
  • 28
  • That hostname doesn't look like a valid IP, did you not copy correctly into your question or is that really what your code shows? – Paul Feb 03 '17 at 17:10
  • The IP I redacted to put on SoF. I am putting it in the xxx.xx.xxx.xx form – Justin E. Samuels Feb 03 '17 at 17:12
  • Possible duplicate of [errorError: getaddrinfo ENOTFOUND - mysql](http://stackoverflow.com/questions/25521755/errorerror-getaddrinfo-enotfound-mysql) – Paul Feb 03 '17 at 17:14
  • This is definitely because the `mysql` library is unable to reach the server. Try specifying the port separately, like this: `host: "123.74", port: 3306`. Also try to see if you can connect from the `mysql` command line using the settings you specified. – Churro Feb 03 '17 at 20:13

2 Answers2

1

You have to correct your config object removing the port from the host and adding it as an object key.

const config = sql.createConnection( {
        host: '123.74',
        port: 3306,
        user: 'root',
        pass: '****',
        database: '****'
});
  • This worked wonders for me, any idea why I had to do this? I am using sails – mhisham Apr 27 '21 at 20:03
  • This is how the library works, they have different fields dividing the responsability. If you look to the error log, you will see that library interprets the number 3306 as the correct port but it doesn't remove it from the host address. – Luis Felipe Martins Jul 16 '21 at 14:32
0

The issue was the mysql library I was using. After looking into the numerous Issues raised on GitHub, it seemed like my problem was very common, so I decided to switch to another library called "mysql2", I supplied the same credentials as before and my data flowed out no problem. I put a link to the library on NPM below.

https://www.npmjs.com/package/mysql2

Justin E. Samuels
  • 867
  • 10
  • 28