1

I have installed Node.js as below:

root@server1:~#apt-get install nodejs

Then installed the npm as below:

root@server1:~#apt-get install npm

Then installed the MySQL module inside the server directory as below:

root@server1:~#cd /var/www/
root@server1:/var/www# npm install mysql

Below is the code for node.js server (server.js) (used correct credential instead of xxx):

var mysql = require('mysql');

var db_config = {
    host      : 'localhost',
    user      : 'xxx',
    password  : 'xxx',
    database  : 'xxx'
};

var connection;

function handleDisconnect(){

    connection = mysql.createConnection(db_config);


    connection.connect(function(err) {
        if(err) {
            console.log('error when connecting to db:', err);
            setTimeout(handleDisconnect, 2000);
        }
    });

    connection.on('error', function(err) {
        console.log('db error', err);
        if(err.code === 'PROTOCOL_CONNECTION_LOST') {
            handleDisconnect();
        }
        else{
             throw err;
        }
    });
 }

 handleDisconnect();   

When I run the Node.js server like below:

root@server1:~#node /var/www/server.js

I get general error! It is below:

error when connecting to db: { [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
fatal: true }

UPDATE:

I am able to connect to MySQL from PHP (Apache2) and MySQL is working fine on my Ubuntu 12.04 server as seen below:

root@server1:~# netstat -ltnp | grep 3306

Output:

tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      556/mysqld

Would you please advise how to resolve this issue? The Node.js is working on different server but I don't know why it is not working here!

Thanks.

moderns
  • 650
  • 10
  • 23
  • The fact that you can connect from another server suggests that your users aren't set up properly. MySQL users are often set up per host (or `*` for all hosts). Check that there is a user with access from the IP of the server running your node app – Bojangles Aug 25 '14 at 11:42
  • 1
    Do you see the mysql server running on 3306 if you run a `netstat -nlp`? If not, you could also set `socketPath` to the path of the unix socket instead of setting `host`. – mscdex Aug 25 '14 at 11:46
  • I am able to connect to MySQL from Apache2 and the MySQL is working fine. I wonder why it is not working with Node.js and I can't understand the error they have only general error. Thanks for your help. I have updated the post with netstat output – moderns Aug 25 '14 at 12:02
  • What version of node, npm and mysql are you using? – tkone Aug 25 '14 at 13:02

1 Answers1

6

You need to add the value of socket path to the db_config object:

db_config.socketPath =  '/var/run/mysqld/mysqld.sock'

For example in MAMP, you go to http://localhost:8888/MAMP, and you find:

/Applications/MAMP/tmp/mysql/mysql.sock

At the end you have:

var connection = mysql.createConnection({
  host     : db_config.host,
  user     : db_config.user,
  password : db_config.pass,
  database : db_config.db,
  socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock'
});
tdebroc
  • 1,436
  • 13
  • 28