如何在使用 NodeJS 和 Express 时创建 MySQL 连接池?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37102364/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a MySQL connection pool while working with NodeJS and Express?
提问by Utkarsh Kaushik
I am able to create a MySQL connection like this:
我能够像这样创建一个 MySQL 连接:
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
But I would rather like to initiate a pool and use it across my project.
但我更愿意启动一个池并在我的项目中使用它。
回答by Utkarsh Kaushik
Just to help some one in future, this worked for me:
只是为了帮助将来的某个人,这对我有用:
I created a mysql connector file containing the pool:
我创建了一个包含池的 mysql 连接器文件:
// Load module
var mysql = require('mysql');
// Initialize pool
var pool = mysql.createPool({
connectionLimit : 10,
host : '127.0.0.1',
user : 'root',
password : 'root',
database : 'db_name',
debug : false
});
module.exports = pool;
Later you can simply include the connector in another file lets call it manageDB.js:
稍后您可以简单地将连接器包含在另一个文件中,我们称之为 manageDB.js:
var pool = require('./mysqlConnector');
And made a callable method like this:
并制作了一个这样的可调用方法:
exports.executeQuery=function(query,callback){
pool.getConnection(function(err,connection){
if (err) {
connection.release();
throw err;
}
connection.query(query,function(err,rows){
connection.release();
if(!err) {
callback(null, {rows: rows});
}
});
connection.on('error', function(err) {
throw err;
return;
});
});
}
回答by Nam Le
There is some bugs in Utkarsh Kaushik solution:
Utkarsh Kaushik 解决方案中存在一些错误:
if (err), the connection can not be released.
connection.release();
and when it has an err, next statement
.query
always execute although it gets an error and cause the app crashed.when the result is null although query success, we need to check if the result is null in this case.
如果(错误),则无法释放连接。
connection.release();
并且当它有错误时,下一条语句
.query
总是会执行,尽管它会出错并导致应用程序崩溃。当查询成功但结果为空时,我们需要检查结果是否为空。
This solution worked well in my case:
这个解决方案在我的情况下效果很好:
exports.getPosts=function(callback){
pool.getConnection(function(err,connection){
if (err) {
callback(true);
return;
}
connection.query(query,function(err,results){
connection.release();
if(!err) {
callback(false, {rows: results});
}
// check null for results here
});
connection.on('error', function(err) {
callback(true);
return;
});
});
};
回答by sumitjainjr
You can create a connection file, Let's called dbcon.js
您可以创建一个连接文件,我们称之为 dbcon.js
var mysql = require('mysql');
// connect to the db
dbConnectionInfo = {
host: "localhost",
port: "3306",
user: "root",
password: "root",
connectionLimit: 5, //mysql connection pool length
database: "db_name"
};
//For mysql single connection
/* var dbconnection = mysql.createConnection(
dbConnectionInfo
);
dbconnection.connect(function (err) {
if (!err) {
console.log("Database is connected ... nn");
} else {
console.log("Error connecting database ... nn");
}
});
*/
//create mysql connection pool
var dbconnection = mysql.createPool(
dbConnectionInfo
);
// Attempt to catch disconnects
dbconnection.on('connection', function (connection) {
console.log('DB Connection established');
connection.on('error', function (err) {
console.error(new Date(), 'MySQL error', err.code);
});
connection.on('close', function (err) {
console.error(new Date(), 'MySQL close', err);
});
});
module.exports = dbconnection;
Now include this connection to another file
现在将此连接包含到另一个文件中
var dbconnection = require('../dbcon');
dbconnection.query(query, params, function (error, results, fields) {
//Do your stuff
});