gpt4 book ai didi

mysql - 处理 Disconnect Express JS Throw Error Access Denied for user localhost

转载 作者:行者123 更新时间:2023-11-28 23:12:06 26 4
gpt4 key购买 nike

你好,我正在尝试处理断开连接我的服务器 express js,我正在使用此句柄断开连接代码,它抛出本地主机中用户拒绝访问的错误。

var connection;

function handleDisconnect() {
connection = mysql.createConnection(db_config); // Recreate the connection, since
// the old one cannot be reused.

connection.connect(function(err) { // The server is either down
if(err) { // or restarting (takes a while sometimes).
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000); // We introduce a delay before attempting to reconnect,
} // to avoid a hot loop, and to allow our node script to
}); // process asynchronous requests in the meantime.
// If you're also serving http, display a 503 error.
connection.on('error', function(err) {
console.log('db error', err);
if(err.code === 'PROTOCOL_CONNECTION_LOST') { // Connection to the MySQL server is usually
handleDisconnect(); // lost due to either server restart, or a
} else { // connnection idle timeout (the wait_timeout
throw err; // server variable configures this)
}
});
}

handleDisconnect();

和我的连接主机

 var mysql = require('mysql');  
var db_config = module.exports = {

'connection': {
'host': 'localhost',
'user': 'root',
'password': ''
},
'database': 'database'
};

它也发生在我的服务器上,它抛出错误,例如用户 localhost 的访问被拒绝。

错误是这样显示的

Error: ER_DBACCESS_DENIED_ERROR: Access denied for user ''@'localhost' to database 'databases'

最佳答案

错误消息告诉空用户 (at) localhost 是不允许的。这使我进入了 db_config:将代码行中的 db_config 替换为 db_config.connection

connection = mysql.createConnection(db_config.connection);

关于mysql - 处理 Disconnect Express JS Throw Error Access Denied for user localhost,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45532456/

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