DB Error: connection lost: server Closed the connection
DB Error: connection lost: server Closed the connection
当没有请求 mysql 它进入空闲状态时,我遇到了这个错误,我们遇到了这个数据库错误。我正在使用节点,mysql 部署到 openshift 集群上。
如何使数据库连接保持活动状态,使服务器永远不会关闭连接?
粉煤灰
请让我知道有什么解决办法吗?我被困了过去 2 周
更新-
以下是我使用的代码
`var connection;
function handleDisconnect() {
connection = mysql.createConnection({
host: config.db.host,
user: config.db.user,
password: config.db.password,
database: config.db.database,
port: config.db.port,
}); // 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();`
因为您正在使用 Node.js,您可以使用连接池。
以下是 link 的片段。注意,connection.release();
它不会破坏连接,但允许再次使用连接。
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
// Use the connection
connection.query('SELECT something FROM sometable', function (error, results, fields) {
// When done with the connection, release it.
connection.release();
// Handle error after the release.
if (error) throw error;
// Don't use the connection here, it has been returned to the pool.
});
});
当没有请求 mysql 它进入空闲状态时,我遇到了这个错误,我们遇到了这个数据库错误。我正在使用节点,mysql 部署到 openshift 集群上。
如何使数据库连接保持活动状态,使服务器永远不会关闭连接? 粉煤灰 请让我知道有什么解决办法吗?我被困了过去 2 周
更新- 以下是我使用的代码
`var connection;
function handleDisconnect() {
connection = mysql.createConnection({
host: config.db.host,
user: config.db.user,
password: config.db.password,
database: config.db.database,
port: config.db.port,
}); // 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();`
因为您正在使用 Node.js,您可以使用连接池。
以下是 link 的片段。注意,connection.release();
它不会破坏连接,但允许再次使用连接。
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret',
database : 'my_db'
});
pool.getConnection(function(err, connection) {
if (err) throw err; // not connected!
// Use the connection
connection.query('SELECT something FROM sometable', function (error, results, fields) {
// When done with the connection, release it.
connection.release();
// Handle error after the release.
if (error) throw error;
// Don't use the connection here, it has been returned to the pool.
});
});