最近服务器很不稳定,于是重装了mysql 和php 服务,但是接着却遇到了很头疼的麻烦。 远程连接mysql是总是提示: 复制代码代码如下: Lost connection to MySQL server at ‘reading initial communication packet', system error: 0 很明显这是连接初始化阶段
问题详情:
Error: Connection lost: The server closed the connection.
at Protocol.end (/opt/node-v0.10.20-linux-x64/IM/node_modules/mysql/lib/protocol/Protocol.js:73:13)
at Socket.onend (stream.js:79:10)
at Socket.EventEmitter.emit (events.js:117:20)
at _stream_readable.js:920:16
at process._tickCallback (node.js:415:13)
解决方案:我用虚拟机上的java程序连接本地mysql,遇到is not allowed to connect to this MySQL server的问题。 从网上查了下Host 'XXX' is not allowed to connect to this MySQL server 解决方案/如何开启MySQL的远程帐号 如何开启MySQL的远程帐号-1)首先以 root 帐
var db_config = {
host: 'localhost',
user: 'root',
password: '',
database: 'example'
};
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();