我通过npm (npm install mysql)安装了felixge/node-mysql,现在我想连接到mysql数据库,但每次尝试连接时都会发生以下错误:
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect' }
undefined我的nodejs源码:
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);
});但是我可以很容易地通过命令行访问我的MySQL数据库:
[user@host] $ mysql -u root -p
Enter password:
Welcome 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> 有人能帮我吗?我的错误是什么?
发布于 2011-11-28 03:57:22
我不得不修改mysql配置文件。我注释掉了skip-networking,它解决了我的问题。
如果有人能给我解释一下为什么这么好,我会非常感谢。
发布于 2013-12-10 13:54:33
您需要设置socketPath
mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
socketPath : '/var/run/mysqld/mysqld.sock',
});发布于 2014-01-13 16:03:01
我在MacOSX上使用了MAMP,记住添加了这个
"/Applications/MAMP/tmp/mysql/mysql.sock":“socketPath”
{
"host": "localhost",
"port": 3306,
"user": "nodejs",
"password": "nodejs",
"database": "crawler",
"socketPath": "/Applications/MAMP/tmp/mysql/mysql.sock"
}https://stackoverflow.com/questions/8286135
复制相似问题