Error connect ENOENT /var/run/mysqld/mysqld.sock in node.js
<p>I'm running scotch box (a vagrant LAMP stack) on Windows.</p>

<p>I have no problems connecting to MySQL normally through PHP or Navicat.</p>

<p>but I'm trying to connect to it in <code>Node.js</code> and I get the error "<code>Error connecting to MySQL: Error: connect ECONNREFUSED 127.0.0.1:3306</code>"</p>

<p>From what I've understood by searching around it seems that I need to set the socket path</p>

<p>but when I do that I get the error "<code>Error connecting to MySQL: Error: connect ENOENT /var/run/mysqld/mysqld.sock</code>"</p>

<p>I am 100% sure this is the correct socket path. I've SSHed into vagrant and verified that mysqld.sock is actually there and it is correctly set in /etc/mysql/my.cnf. Skip networking is also OFF.</p>

<p>I've also tried setting the bind-address to 0.0.0.0 and commenting out the bind-address entirely, renaming mysqld.sock and restarting the mysql server so that it would recreate mysqld.sock again. Also tried disabling my firewall (node.js is allowed anyway) and reinstalling MySQL.</p>

<p>None of this has worked and the error has remained the same: "<code>Error connecting to MySQL: Error: connect ENOENT /var/run/mysqld/mysqld.sock</code>"</p>

<p>this is the code I have so far:</p>

<pre><code>var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : 'root',
  database : 'test',
  socketPath : '/var/run/mysqld/mysqld.sock'
});

connection.connect(function(err) {
  if (err) {
    console.error('Error connecting to MySQL: ' + err.stack);
    return;
  }
   console.log('connected as id ' + connection.threadId);
   });
</code></pre>

<p>How do I fix this?</p>