Skip to content

Module does not work in Node.js 0.6 (was: ECONNREFUSED) #718

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
EgidioCaprino opened this issue Jan 20, 2014 · 42 comments
Closed

Module does not work in Node.js 0.6 (was: ECONNREFUSED) #718

EgidioCaprino opened this issue Jan 20, 2014 · 42 comments
Labels

Comments

@EgidioCaprino
Copy link

Hello.

I'm trying to connect to MySQL server in this way.

var config = require('./config.js'),
    mysql = require('./node_modules/mysql/index.js'),
    csv = require('./node_modules/csv/index.js'),
    fs = require('fs');

console.log(config.db);

var db = mysql.createConnection(config.db);
db.connect();

but I get this error:

{ [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  fatal: true }

I tried all the solution on the web but nothing worked.

@jdrydn
Copy link

jdrydn commented Jan 20, 2014

Interesting how you're loading the mysql module - why not:

var mysql = require("mysql");

@sidorares
Copy link
Member

  1. Do you run mysql server locally? 2) can you connect to it with command line client (with "-h 127.0.0.1" switch)? 3) what is output of ps ax |grep mysqld?

@EgidioCaprino
Copy link
Author

@jdrydn I load it in that way because npm install mysql put it in subdirectory of the project.

@sidorares The server is local and I can connect to it by CLI (mysql -u user -p db -h 127.0.0.1). This is the output of ps ax |grep mysqld

root@host2:~# ps ax |grep mysqld
 7384 ?        Ssl    7:05 /usr/sbin/mysqld
30539 pts/0    S+     0:00 grep --color=auto mysqld

Thank you guys.

@jdrydn
Copy link

jdrydn commented Jan 20, 2014

Yeh, NPM installs modules to a node_modules folder - See http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders ;)

(Therefore, require("mysql") resolves to /node_modules/mysql/index.js ;) technically)
(Also, if you have multiple Node projects, all using the same module, you can put node_modules folders up the hierarchy so multiple projects share the same modules 👍 )

@EgidioCaprino
Copy link
Author

@jdrydn do you think this is the problem? What do you mean exactly with up the hierarchy?

@jdrydn
Copy link

jdrydn commented Jan 20, 2014

No, I think the problem lies in your database config, especially if you can connect over the CLI, could you comment with the object you have at config.db (obviously blanking out usernames and password ;)). I'm just commenting on the way you seem to have pulled in the mysql module, which I've never seen used like that before!

@jdrydn
Copy link

jdrydn commented Jan 20, 2014

I.e. Mine often looks like:

    var config = {
        database: {
            host: "localhost",
            user: "myawesomeuser",
            password: "mysecurepassword",
            database: "thedatabase"
        }
    };

    var mysql = require("mysql");

    var connection = mysql.createConnection(config.database);

@EgidioCaprino
Copy link
Author

Thank you. This is the definition of config.db.

exports.db = {
    host: '127.0.0.1',
    user: 'user',
    password: 'passwd',
    database: 'zadmin_shopriparando'
};

@EgidioCaprino
Copy link
Author

What settings do you think are wrong with my DB?

@jdrydn
Copy link

jdrydn commented Jan 20, 2014

What I would do, for debugging irritating connection issues like this, is put the contents of the example (https://github.com/felixge/node-mysql#introduction) into a file testdatabase.js, fill in your connection details, and try running that on it's own ($ node testdatabase.js).

Do you still get a connection issue?


Edit: If no, then perhaps it's a problem with your user not being able to access your database? Check the privileges of of your database, make sure the user can do stuff on the database.

@EgidioCaprino
Copy link
Author

The problem persists. I tried with

var mysql = require('mysql');
var connection = mysql.createConnection({
    host : 'testdb',
    user: 'me',
    password: 'beryqeqes',
    database: 'zadmin_testdb'
});

connection.connect();

but I get the same error. I do not know if it matters, but we use ZPanel to manage databases and users.

@jdrydn
Copy link

jdrydn commented Jan 20, 2014

Take off the database parameter, can you connect to the MySQL server?

@EgidioCaprino
Copy link
Author

No. Same error :(

@tgvashworth
Copy link

Possibly stupid question but: you do actually have a mysql server running, right?

@jdrydn sup ;)

@EgidioCaprino
Copy link
Author

@PhUU sure. I can also connect to the database through CLI, with the same credentials I use in the script.

@dougwilson
Copy link
Member

This simply means that you cannot connect to MySQL because it has networking disabled. You can connect to it using the UNIX socket. Get the path using mysql_config --socket and they set that as the socketPath option in the connection configuration.

@jdrydn
Copy link

jdrydn commented Jan 20, 2014

@PhUU Sup dawg ;)

@EgidioCaprino
Copy link
Author

@dougwilson thank you. I will try as soon as I can.

@cazgp
Copy link

cazgp commented Jan 24, 2014

mysql_config --socket + socketPath worked :)

@s2zaman
Copy link

s2zaman commented Jan 30, 2014

I got the same error as below while running compound server

Compound server listening on 0.0.0.0:3000 within development environment
connection.connect err { [Error: connect ECONNREFUSED]
  code: 'ECONNREFUSED',
  errno: 'ECONNREFUSED',
  syscall: 'connect',
  fatal: true }
connection.connect err { [Error: connect ECONNREFUSED]

I guessed it as missing of MySQL.
So i used WAMP Server’s MySQL. (actually i stopped the APACHE and PHP services in it and used only the MySQL service). This was temporary solution.

What may be the possible good solution for this Error.

@jacog
Copy link

jacog commented Mar 4, 2014

Can confirm same issue as OP. Have one lamp stack, php connects without problems. node with mysql (from same host as db and default port) gives:

{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
fatal: true }

connecting via socketPath works.
User has access to applicable schemas. User can connect from localhost. User can connect from php.

@dougwilson
Copy link
Member

@jacog this is not a bug. Your PHP is connecting over a UNIX socket, not over TCP. This module simply makes you explicitly chose instead of defaulting to a hard-coded UNIX socket path like PHP does. If you want to connect over TCP with this module, you'll need to enable networking in your MySQL server, otherwise continue to use the socketPath option.

@jacog
Copy link

jacog commented Mar 4, 2014

@dougwilson thanx for the info, I didn't know php does that by itself. It is a bit weird that even though in php you specify hostname / port and not socket path. Perhaps it does it because it is on the same box. I looked over the mysql cnf, didnt see anything disabling networking. Will have another look. Thanks.

@dougwilson
Copy link
Member

@jacog the setting is called skip-networking: http://dev.mysql.com/doc/refman/5.1/en/server-options.html#option_mysqld_skip-networking It is typically enabled (i.e. it disables TCP) out-of-the-box on things like Ubuntu.

@dougwilson
Copy link
Member

@jacog The setting is probably in the /etc/mysql/my.cnf file. See if there is a skip-networking line in there, remove it, and restart MySQL. This will open your MySQL install up to the network, though, so be careful.

@jacog
Copy link

jacog commented Mar 5, 2014

@dougwilson Interesting thing is, I can connect to my server from my workstation (which is a different machine altogether) with mysql workbench - using ip / port. Well connecting via socket is fine. For anyone strugling with this, you can find your socketPath in /etc/mysql/my.cnf on ubuntu it should be /var/run/mysqld/mysqld.sock

@dougwilson
Copy link
Member

@jacog it's possible your MySQL is bound and listening only on your external IP and not on your localhost as well. Another possibility is that your localhost is resolving to something weird so changing it to 127.0.0.1 may work.

@dougwilson
Copy link
Member

To everyone here, I'm going to close this issue since it has gotten very long as is not even a bug with this module unless there is a patch that fixes the issue. ECONNREFUSED comes with within node.js and is because there is either nothing listening on 127.0.0.1:3306.

There are many ways you can check this. One way is to use netstat to verify there is a TCP listening socket at 127.0.0.1:3306:

sudo netstat -nalp | grep 3306
tcp        0      0 127.0.0.1:3306          0.0.0.0:*               LISTEN      18241/mysqld

@LoicGombeaud
Copy link

I am getting the same error; the MySQL server I am trying to connect to is on a different host, and I can connect to it via the CLI client as well as via PHP, by specifying its IP (it also works by specifying its DNS name). I can also telnet to it.
When trying the following code, I get the same error as mentioned above: ECONNREFUSED

var mysql = require('mysql');

var connection = mysql.createConnection({
       host: 'db.domain.tld',
       user: 'myuser',
       password: 'secret',
       database: 'something'
});
connection.connect(function(err) {
       console.log(err);
});

Can you please tell me how to test further, to find the exact cause of the issue?

@dougwilson dougwilson added the FAQ label Apr 17, 2014
@dougwilson
Copy link
Member

@loicAG ECONNREFUSED means that something is actively rejecting your TCP connection to db.domain.tld on port 3306. You can always try running this command from the machine that would run your node.js mysql client: nc -v -z db.domain.tld 3306. If this fails, there is something wrong with how your MySQL server it setup, something wrong with the network between the machines, or a firewall blocking the connection somewhere.

@cazgp
Copy link

cazgp commented Apr 18, 2014

Whilst I haven't had this problem in a while, my comment up there turned out to be false (socket path merely led to a different error). Attempting to connect to the database from the command line worked perfectly. In the end I gave up with node-mysql and switched to sequelize which connected first time... That seems to me like it's a bug with node-mysql itself.

@dougwilson
Copy link
Member

@cazcazn sequelize is actually using this library to connect to MySQL, so I don't know what to tell you, besides that you seem to be able to connect to MySQL using this module through sequelize.

@cazgp
Copy link

cazgp commented Apr 19, 2014

Hmm so the only thing different in the way sequelize connects is it adds a port and a timezone. Like I say, I haven't looked at this in a while so can't play with anything, was just adding what worked for me.

@evanmoss
Copy link

This might be a dumb question, but what version of NodeJS are you using? I once accidentally reinstalled 0.6 instead of 0.10, and I had this issue.

@JalfResi
Copy link

FIX: make sure you are using the latest version of nodejs. This library doesn't work with node 0.6

Just had this exact issue. Appears that for some reason this library is incompatible with node 0.6. Tried everything suggested on this thread along with every other thread I could Google, but evanmoss was the only one that worked.

You might want to think about including a version check in the library on load and refuse to activate the module if the version if incompatible.

@dougwilson
Copy link
Member

@JalfResi there is a version check: https://github.com/felixge/node-mysql/blob/master/package.json#L28 The npm install would have yelled at you and this library does not meet the requirements. We cannot add a version check to the code without depending on another library and we won't do that :) Just read the output from when npm install mysql runs.

@evanmoss
Copy link

My issue - at least - was that I was using a PaaS, so if it spit some error
messages, I missed it. My lesson learned: be more careful when installing
things! I didn't realize I accidentally chose the older version of node.

On Friday, August 29, 2014, Douglas Christopher Wilson <
[email protected]> wrote:

@JalfResi https://github.com/JalfResi there is a version check:
https://github.com/felixge/node-mysql/blob/master/package.json#L28 The npm
install would have yelled at you and this library does not meet the
requirements. We cannot add a version check to the code without depending
on another library and we won't do that :) Just read the output from when npm
install mysql runs.


Reply to this email directly or view it on GitHub
#718 (comment).

@dougwilson
Copy link
Member

Sure :) It's just Node.js does not give us a way to check the version in runtime--all there is is a string like 'v0.6.4', which we'd have to parse properly, etc. Or pull in an entire module just to assert we are not on < 0.8 when it's really npm's job :)

@JalfResi
Copy link

JalfResi commented Sep 1, 2014

I installed it via npm and no error was raised during install. If there was an error message, and I missed it, it still installed the library successfully. This is a flaw. If the library is incompatible then it shouldn't install at all, unless I force it. It did nothing of the sort and installed it successfully with no error message (that I could see).

I'm new to nodejs and I must say that things issues like this are fustrating. On top of that, now it appears that there is a problem with the MySQL insert statement; I keep getting . So it appears that not only does this library NOT check which version of node it is installed against, it now appears that it doesn't check which version of MySQL it is connecting to, as I now get the error message "1 query failed: Error: ER_PARSE_ERROR: You have an error in your SQL syntax;" I got it to dump out the SQL it's trying to run and ran it on the mysql command line client and it runs without error.

@JalfResi
Copy link

JalfResi commented Sep 1, 2014

Which version of MySQL does this library support? Which version of the MySQL client does the parser support?

@dougwilson
Copy link
Member

Seriously open a new issue. This issue is about ECONNREFUSED which you are not experiencing. This library adds the appropriate checks for now 0.8+. If it installs, it is a bug in npm which does the install incorrectly, not this library, as this library has no control over the install except telling npm the requirements, which it does.

Which version of MySQL does this library support?

This library supports MySQL 4 and up.

Which version of the MySQL client does the parser support?

As for your SQL error, you are giving invalid SQL. This library does nothing with the SQL except forwards it to the server, so it supports whatever SQL your MySQL server does. You can refer to your MySQL server manual to know what your server supports.

@dougwilson dougwilson changed the title ECONNREFUSED Module does not work in Node.js 0.6 (was: ECONNREFUSED) Sep 1, 2014
@dougwilson
Copy link
Member

Hi! For all those who stumble upon this issue, if you just always seem to be getting the ECONNREFUSED error when trying to use this module, please check the version of Node.js you are using. If it is 0.6, please note this module requires Node.js 0.8 and above (and yes, npm, the defacto Node.js package manager, installs modules even when they specify they are not compatible with your version of Node.js).

If you believe you are having an issue with the module, please feel free to open a new issue by going to https://github.com/felixge/node-mysql/issues/new

@mysqljs mysqljs locked and limited conversation to collaborators Sep 1, 2014
dougwilson added a commit that referenced this issue Sep 7, 2014
dougwilson added a commit that referenced this issue Sep 7, 2014
seangarner pushed a commit to seangarner/node-mysql that referenced this issue May 11, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests