Javascript felixge/node-mysql-coudn';无法连接到数据库
我安装了via npm(Javascript felixge/node-mysql-coudn';无法连接到数据库,javascript,mysql,node.js,database-connection,Javascript,Mysql,Node.js,Database Connection,我安装了via npm(npm install mysql),现在我想连接到mysql数据库,但每次尝试连接时都会发生以下错误: { [Error: connect ECONNREFUSED] code: 'ECONNREFUSED', errno: 'ECONNREFUSED', syscall: 'connect' } undefined 我的nodejs来源: var mysql = require("mysql") var db = mysql.createClient({
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>
谁能帮我一下我的错误是什么?当你用节点mysql连接到mysql时,你需要指定你的主机名。如果您使用的是linux,您可以使用以下命令找到:hostname 所以你知道,我有同样的问题,我可以通过命令行连接到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"
});
我不得不修改mysql配置文件。我注释掉了
跳过网络
,它解决了我的问题
如果有人能解释为什么这么好,我会感谢的。您需要设置socketPath
mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'password',
socketPath : '/var/run/mysqld/mysqld.sock',
});
我在MacOSX上使用MAMP,记得添加这个吗 “socketPath”:“/Applications/MAMP/tmp/mysql/mysql.sock”
您有拼写错误
console.log(结果)代码>应读取console.log(结果)代码>问题仍然存在。顺便说一句,我不确定,但可能模块不喜欢不使用数据库的选择。请尝试连接到数据库,然后选择。root的密码是多少?您可以从哪里以root用户身份登录。我只能从localhost以root用户身份登录,但我的密码是root。Node-mysql默认使用localhost:3306。使用skip networking,mysql只使用unix套接字(使用mysqladmin变量| grep sock
查找位置),mysql客户端可以连接到该套接字,而节点代码不能(它不知道位置)。使用socketPath
指定套接字路径并取消注释跳过网络
以获得更好的mysql性能
{
"host": "localhost",
"port": 3306,
"user": "nodejs",
"password": "nodejs",
"database": "crawler",
"socketPath": "/Applications/MAMP/tmp/mysql/mysql.sock"
}