felixge/node-mysql 无法连接到数据库
我通过 npm 安装了 felixge/node-mysql
(npm install 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>
任何人都可以帮我看看我的错误是什么?
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 command line:
[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>
Can anybody help me what is my mistake?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要设置socketPath
You need to set the socketPath
我不得不修改mysql配置文件。我注释掉了
skip-networking
它解决了我的问题。如果有人能解释一下为什么那么好,我会很感激。
I had to modify the mysql configuration file. I commented out
skip-networking
and it fixed my problem.If anybody can explain me why is that good I would thank it.
我在 MacOSX 上使用 MAMP,请记住添加此
"socketPath": "/Applications/MAMP/tmp/mysql/mysql.sock"
I use MAMP on MacOSX, remember add this
"socketPath": "/Applications/MAMP/tmp/mysql/mysql.sock"
当您使用node-mysql连接到mysql时,您需要指定您的主机名。如果你在Linux上,你可以使用以下命令找到: 主机名
所以你知道,我有同样的问题,我可以通过命令行连接到mysql而不指定主机名,但在节点中你需要更改它。至少就我而言。 (v0.4.10) 为什么?我不确定,所以也许其他人有想法。
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 command : hostname
So you know, i have the same problem i can connect to mysql via command 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.