【发布时间】:2011-11-27 14:08:07
【问题描述】:
我通过 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>
谁能帮帮我我的错误是什么?
【问题讨论】:
-
你有一个拼写错误
console.log(results);应该读作console.log(result); -
问题仍然存在。顺便说一句谢谢
-
我不确定,但也许模块不喜欢不使用数据库的 SELECT。尝试连接到数据库,然后选择。
-
那么你的root密码是root吗?您可以从哪里以 root 身份登录。
-
我只能从 localhost 以 root 身份登录,但是是的,我的密码是 root。
标签: javascript mysql node.js database-connection