felixge/node-mysql 无法连接到数据库

发布于 2024-12-18 09:18:10 字数 1285 浏览 0 评论 0原文

我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扎心 2024-12-25 09:18:10

您需要设置socketPath

mysql = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'password',
    socketPath  : '/var/run/mysqld/mysqld.sock',

});

You need to set the socketPath

mysql = require('mysql');
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'password',
    socketPath  : '/var/run/mysqld/mysqld.sock',

});
你的笑 2024-12-25 09:18:10

我不得不修改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.

孤者何惧 2024-12-25 09:18:10

我在 MacOSX 上使用 MAMP,请记住添加此

"socketPath": "/Applications/MAMP/tmp/mysql/mysql.sock"

{
    "host": "localhost",
    "port": 3306,
    "user": "nodejs",
    "password": "nodejs",
    "database": "crawler",
    "socketPath": "/Applications/MAMP/tmp/mysql/mysql.sock"
}

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"
}
白龙吟 2024-12-25 09:18:10

当您使用node-mysql连接到mysql时,您需要指定您的主机名。如果你在Linux上,你可以使用以下命令找到: 主机名

所以你知道,我有同样的问题,我可以通过命令行连接到mysql而不指定主机名,但在节点中你需要更改它。至少就我而言。 (v0.4.10) 为什么?我不确定,所以也许其他人有想法。

var mysql = require("mysql")
var db = mysql.createClient({
    host: --- put what you got from the hostname command ---,
    user:"root",
    password:"root"
});

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.

var mysql = require("mysql")
var db = mysql.createClient({
    host: --- put what you got from the hostname command ---,
    user:"root",
    password:"root"
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文