Javascript NodeJS的MySQL连接错误
我已经按照下面的步骤对代码做了一些更改。Javascript NodeJS的MySQL连接错误,javascript,mysql,node.js,server,centos,Javascript,Mysql,Node.js,Server,Centos,我已经按照下面的步骤对代码做了一些更改。 但它不起作用。我也尝试过使用池。请帮助我解决此问题。可能重复@pritaeas Hi,我尝试了该解决方案。不适合我(因为此代码在本地主机上运行良好。仅在生产(CentOS)上触发错误).nodejs mysql是否有防火墙授予的权限?您的密码是什么using@p0k8_我正在为我的服务器使用不同的凭据。这些是我的本地主机配置详细信息。我确信我的凭据是正确的。是否有重新连接事件 var app = require('express')(); var htt
但它不起作用。我也尝试过使用池。请帮助我解决此问题。可能重复@pritaeas Hi,我尝试了该解决方案。不适合我(因为此代码在本地主机上运行良好。仅在生产(CentOS)上触发错误).nodejs mysql是否有防火墙授予的权限?您的密码是什么using@p0k8_我正在为我的服务器使用不同的凭据。这些是我的本地主机配置详细信息。我确信我的凭据是正确的。是否有
重新连接
事件
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var fs = require('fs');
var mysql = require('mysql');
app.get('/', function(req, res){
res.send('<h1>My App</h1>');
});
var db_config = {
host: 'localhost',
user: 'root',
database: 'database',
password: '',
dialect: 'mysql',
insecureAuth: true
};
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();
http.listen(3000, function(){
console.log('listening on *:3000');
});
db error { Error: Connection lost: The server closed the connection.