增加数据库重启方案
This commit is contained in:
parent
6b9969116f
commit
e299562220
|
|
@ -1,12 +1,35 @@
|
||||||
var mysql = require('mysql');
|
var mysql = require('mysql');
|
||||||
var client = mysql.createConnection({
|
var dbConfig = {
|
||||||
host: '127.0.0.1',
|
host: '127.0.0.1',
|
||||||
user: 'test', // mysql的账号
|
user: 'test', // mysql的账号
|
||||||
password: '123456', // mysql 的密码
|
password: '123456', // mysql 的密码
|
||||||
database: 'mybookmarks',
|
database: 'mybookmarks',
|
||||||
multipleStatements: true,
|
multipleStatements: true,
|
||||||
port: 3306
|
port: 3306
|
||||||
|
};
|
||||||
|
var client = {}
|
||||||
|
|
||||||
|
function handleDisconnect() {
|
||||||
|
client = mysql.createConnection(dbConfig);
|
||||||
|
|
||||||
|
client.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.
|
||||||
|
client.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();
|
||||||
|
|
||||||
Date.prototype.format = function(fmt) { //author: meizz
|
Date.prototype.format = function(fmt) { //author: meizz
|
||||||
var o = {
|
var o = {
|
||||||
|
|
@ -24,8 +47,6 @@ Date.prototype.format = function(fmt) { //author: meizz
|
||||||
return fmt;
|
return fmt;
|
||||||
}
|
}
|
||||||
|
|
||||||
client.connect();
|
|
||||||
|
|
||||||
// select 最多返回一行的话,返回对象,否则返回数组
|
// select 最多返回一行的话,返回对象,否则返回数组
|
||||||
// insert 返回关键字
|
// insert 返回关键字
|
||||||
// update delete 返回影响的行数
|
// update delete 返回影响的行数
|
||||||
|
|
|
||||||
|
|
@ -482,7 +482,6 @@ api.post('/uploadBookmarkFile', upload.single('bookmark'), function(req, res) {
|
||||||
var file = req.file;
|
var file = req.file;
|
||||||
res.json(file);
|
res.json(file);
|
||||||
parseHtml(file.path, function(data) {
|
parseHtml(file.path, function(data) {
|
||||||
console.log(data);
|
|
||||||
var bookmarks = data.bookmarks;
|
var bookmarks = data.bookmarks;
|
||||||
var tagsName = data.tags;
|
var tagsName = data.tags;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue