Mysql: How to configure node-mysql to connect via a IP address?

Created on 25 Dec 2016  路  3Comments  路  Source: mysqljs/mysql

I'm trying to connect to a remote database but whatever I do I get a getaddrinfo ENOTFOUND error.

My remote mysql server is correctly configured and I can connect remotly to it with Sequal from my localhost as well as another server, privileges are fine, bind-address too and my.cf and so on.

Got this error:

Error connecting to Db
err:  { [Error: getaddrinfo ENOTFOUND 12.34.56.78  12.34.56.78 :3306]
  code: 'ENOTFOUND',
  errno: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: '12.34.56.78 ',
  host: '12.34.56.78 ',
  port: 3306,
  fatal: true }

I use the code to connect:

var mysql = require("mysql");

// First you need to create a connection to the db
var con = mysql.createConnection({
  host: "12.34.56.78 ",
  user: "peter",
  password: "1234"
});

con.connect(function(err){
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

con.end(function(err) {
  // The connection is terminated gracefully
  // Ensures all previously enqueued queries are still
  // before sending a COM_QUIT packet to the MySQL server.
  if(err) console.log('err: ', err);
  else console.log('done: ');
});

Remote access on Sequel works well:
sequel work

question

Most helpful comment

It's no problem; it seems dumb that Node.js would just try to do a DNS lookup on that mistake, though. I'm not sure were Node.js core stands on this, but if they are open to it, I think that getting a fix to either strip trailing whitespace or throw an error for invalid host would be a good contribution to Node.js core for the entire ecosystem :) ! We simply pass the host to the net.createConnection API in Node.js core.

All 3 comments

Probably remove the trailing space from the host option in your configuration.

oh no, it s my stupid mistake, and spent 2 hour to figure out what s wrong.
Thx @dougwilson

It's no problem; it seems dumb that Node.js would just try to do a DNS lookup on that mistake, though. I'm not sure were Node.js core stands on this, but if they are open to it, I think that getting a fix to either strip trailing whitespace or throw an error for invalid host would be a good contribution to Node.js core for the entire ecosystem :) ! We simply pass the host to the net.createConnection API in Node.js core.

Was this page helpful?
0 / 5 - 0 ratings