前段时间在写一个版本发布工具,用到express+mysql实现,当站点运行很长一段空白时间后,node进程会自动down掉,提示mysql连接错误,谷歌后发现是mysql自身的特性导致,因此后来改为mysql pool连解决次问题!
mysql模块为felixge/node-mysql
源码如下:
/** * Created by kevalin on 2015/4/22. */ var express = require('express'); var router = express.Router(); var mysql = require('mysql'); var conf = require('../config/dbconnection'); //定义pool池 var pool = mysql.createPool( { host : conf.dbMysql.host, user : conf.dbMysql.user, password : conf.dbMysql.password, database : conf.dbMysql.database, port : conf.dbMysql.port } ); router.get('/', function(req, res) { var selectSites = "select *, date_format(do_time, '%Y-%m-%d %H:%i:%s') as time from siteinfo order by id"; pool.getConnection(function(err, connection) { if (err) throw err; connection.query(selectSites, function(err, rows) { if (err) throw err; res.render('sites', {title : '站点分布', results : rows}) }); //回收pool connection.release(); }); }); module.exports = router;