Wednesday, 19 April 2017

node mysql connection.connect does not work

Having an odd issue with node mysql. I am running a mysql container (docker) on my local machine with a standard setup. I am able to get past mysql.createConnection, but my code always fails at connection.connect. MySQL Error logs on the container show no attempted/failed connections, and I can connect via Workbench. Has anyone had this experience trying to connect to MySQL via node.

var connection = mysql.createConnection({
  host     : '127.0.0.1',
  user     : 'user',
  password : 'password',
  database : 'db',
  port     : 3306,
  debug    : true 
});

console.log('Connect to server', connection.state);

connection.connect(function(err) {
  if (err) {
    console.log('error connecting: ' + err.stack);
    return;
  }
  else {
    console.log('connected as id ' + connection.threadId);
  }
});

console.log(connection.state); //Never makes it to this line. 



via Jonathan

No comments:

Post a Comment