gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 07:54:15 27 4
gpt4 key购买 nike

我安装了 felixge/node-mysql通过 npm (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>

任何人都可以帮助我我的错误是什么?

最佳答案

需要设置socketPath

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

});

关于javascript - felixge/node-mysql 无法连接到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8286135/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com