歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Node.js mysql pool使用實例

Node.js mysql pool使用實例

日期:2017/3/1 9:29:13   编辑:Linux編程

前段時間在寫一個版本發布工具,用到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;

下面的內容你可能也喜歡

如何在CentOS 7安裝Node.js http://www.linuxidc.com/Linux/2015-02/113554.htm

Ubuntu 14.04下搭建Node.js開發環境 http://www.linuxidc.com/Linux/2014-12/110983.htm

Ubunru 12.04 下Node.js開發環境的安裝配置 http://www.linuxidc.com/Linux/2014-05/101418.htm

Node.Js入門[PDF+相關代碼] http://www.linuxidc.com/Linux/2013-06/85462.htm

Node.js開發指南 高清PDF中文版 +源碼 http://www.linuxidc.com/Linux/2014-09/106494.htm

Node.js入門開發指南中文版 http://www.linuxidc.com/Linux/2012-11/73363.htm

Ubuntu 編譯安裝Node.js http://www.linuxidc.com/Linux/2013-10/91321.htm

Node.js 的詳細介紹:請點這裡
Node.js 的下載地址:請點這裡

Copyright © Linux教程網 All Rights Reserved