I installed the felixge/node-mysql
via npm (npm install mysql
), and now i want to connect to the mysql database, but every time I try to connect this error happens:
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
undefined
my nodejs source:
var mysql = require("mysql")
var db = mysql.createClient({
host:"localhost",
user:"root",
password:"root"
});
db.query("SELECT 1",function(err,result,fields){
if(err) {
console.log(err);
}
console.log(result);
});
But i can easily access to my MySQL database via mand line:
[user@host] $ mysql -u root -p
Enter password:
Wele to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.18-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT 1
-> ;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
mysql>
Can anybody help me what is my mistake?
I installed the felixge/node-mysql
via npm (npm install mysql
), and now i want to connect to the mysql database, but every time I try to connect this error happens:
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
undefined
my nodejs source:
var mysql = require("mysql")
var db = mysql.createClient({
host:"localhost",
user:"root",
password:"root"
});
db.query("SELECT 1",function(err,result,fields){
if(err) {
console.log(err);
}
console.log(result);
});
But i can easily access to my MySQL database via mand line:
[user@host] $ mysql -u root -p
Enter password:
Wele to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.5.18-log Source distribution
Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> SELECT 1
-> ;
+---+
| 1 |
+---+
| 1 |
+---+
1 row in set (0.00 sec)
mysql>
Can anybody help me what is my mistake?
Share edited Nov 27, 2011 at 14:46 Gergely Fehérvári asked Nov 27, 2011 at 14:08 Gergely FehérváriGergely Fehérvári 7,9516 gold badges52 silver badges77 bronze badges 9-
You have a spelling mistake
console.log(results);
should readconsole.log(result);
– davin Commented Nov 27, 2011 at 14:36 - the problem persists. btw thanks – Gergely Fehérvári Commented Nov 27, 2011 at 14:45
- I'm not sure, but maybe the module doesn't like SELECTs without USEing a database. Try connecting to a db and then selecting. – davin Commented Nov 27, 2011 at 14:57
- so is your password for root, root? Where can you log as root from. – Tony Hopkinson Commented Nov 27, 2011 at 15:06
- i can login as root only from localhost, but yes my password is root. – Gergely Fehérvári Commented Nov 27, 2011 at 15:09
4 Answers
Reset to default 4You need to set the socketPath
mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
socketPath : '/var/run/mysqld/mysqld.sock',
});
I had to modify the mysql configuration file. I mented out skip-networking
and it fixed my problem.
If anybody can explain me why is that good I would thank it.
I use MAMP on MacOSX, remember add this
"socketPath": "/Applications/MAMP/tmp/mysql/mysql.sock"
{
"host": "localhost",
"port": 3306,
"user": "nodejs",
"password": "nodejs",
"database": "crawler",
"socketPath": "/Applications/MAMP/tmp/mysql/mysql.sock"
}
When you connect to mysql with node-mysql, you need to specify your hostname. If you're on linux you can find out using the mand : hostname
So you know, i have the same problem i can connect to mysql via mand line without specify the hostname but in node you need to change it. At least in my case. (v0.4.10) Why ? I'm not sure so maybe someone else have an idea.
var mysql = require("mysql")
var db = mysql.createClient({
host: --- put what you got from the hostname mand ---,
user:"root",
password:"root"
});