Nodejs app can't connect to local MySQL on FreeBSD 8.2-STABLE

guys.

I've installed MySQL module for node.js using npm install mysql and bumped into connection error.

Here's my js code.

var mysql      = require('mysql');
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'user',
  password : 'userpassword',
  database : 'node'
});

connection.connect();

connection.query('SELECT * FROM table', function(err, rows, fields) {
  if (err) throw err;
  console.log('The solution is: ' + fields);
});

connection.end();

Here's the output I get:

Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'node'@'146.66.*.*' (using password: YES)
at Handshake.Sequence._packetToError (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:48:14)
at Handshake.ErrorPacket (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/sequences/Handshake.js:101:18)
at Protocol._parsePacket (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/Protocol.js:270:23)
at Parser.write (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/Parser.js:77:12)
at Protocol.write (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/usr/local/www/apache22/data/node/node_modules/mysql/lib/Connection.js:82:28)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
--------------------
at Protocol._enqueue (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/Protocol.js:135:48)
at Protocol.handshake (/usr/local/www/apache22/data/node/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/usr/local/www/apache22/data/node/node_modules/mysql/lib/Connection.js:108:18)
at Object.<anonymous> (/usr/local/www/apache22/data/node/local_db.js:9:12)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)

The thing is that instead of connecting to MySQL via localhost (as I specified in connection options) it goes through externalIPaddress (for no obvious reason) and firewall drops connection in accordance with it's rules.

I've searched for solutions in other topics, but either error output or suitable solutions differ a lot.

I've also tried popular solutions for related problems from other questions and this is the list of options, that didn't work for me: 1. Flushing MySQL privileges table after new user creation and granting necessary privileges. 2. Modifying /etc/hosts to get proper answer from nslookup localhost and nslookup localhost. 3. Manually pointing path to MySQL socket also hasn't change anything. 4. I do not use host and server options at the same time. 5. I've tried 3306 port specifying.

After all, I still get the same error.

Thank's for your help in advance.

By default mysql binds and listens to the "everything" address at 0.0.0.0. Perhaps your node and your node.js module are doing the same thing. This would make requests and responses come over your external interface and trigger your firewall.

From your output I assume node and MySQL are on the same machine and that you are using Apache as a reverse proxy. If this is the case only httpd should be listening to ports on external addresses: node, mysql, etc. should be talking to each other over local interfaces or via sockets.

Check your configuration and make sure node and mysql-server are binding/listening to ports on the lo interface, localhost or 127.0.0.1. This could resolve your problem.

HTH, G'Cito