Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / Node.js mysql pool使用实例

前段时间在写一个版本发布工具,用到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.htmUbuntu 14.04下搭建Node.js开发环境  http://www.linuxidc.com/Linux/2014-12/110983.htmUbunru 12.04 下Node.js开发环境的安装配置 http://www.linuxidc.com/Linux/2014-05/101418.htmNode.Js入门[PDF+相关代码] http://www.linuxidc.com/Linux/2013-06/85462.htmNode.js开发指南 高清PDF中文版 +源码 http://www.linuxidc.com/Linux/2014-09/106494.htmNode.js入门开发指南中文版 http://www.linuxidc.com/Linux/2012-11/73363.htmUbuntu 编译安装Node.js http://www.linuxidc.com/Linux/2013-10/91321.htmNode.js 的详细介绍:请点这里
Node.js 的下载地址:请点这里本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-05/117798.htm