关闭连接 MySQL Node.js
Close connection MySQL Node.js
我正在使用 ExpressJS 和 MySQL 开发 Node 应用程序。我正在使用这个模块 https://github.com/felixge/node-mysql/ 并且我正在学习它的使用。
我遇到了如何正确关闭连接的问题。
我就是这样做的:
app.post('/example', function (req, res) {
//BD
var connection = mysql.createConnection({
host: config.database.host,
port: config.database.port,
user: config.database.user,
password: config.database.password,
database: config.database.database
});
var sql = '';
if (varExample != null) {
sql = 'Random query';
connection.query(sql, function (err, results) {
//Get results
connection.end();
});
}
});
有时我必须多次调用此方法才能将数据插入数据库。那时我得到一个错误 'Too many connections'.
在这些情况下关闭连接的方法是什么?
你不应该做的是每次收到请求都打开一个连接。每次连接都很慢,其次驱动程序通常会为您打开一个连接池,所以应该不是问题。无需关闭与 mysql.
的连接
基本上你必须这样做
//BD
var connection = mysql.createConnection({
host: config.database.host,
port: config.database.port,
user: config.database.user,
password: config.database.password,
database: config.database.database
});
app.post('/example', function (req, res) {
var sql = '';
if (varExample != null) {
sql = 'Random query';
connection.query(sql, function (err, results) {
//Get results
});
}
});
编辑:添加池选项
拥有一个连接池基本上是我们大多数人想要的服务器必须做很多查询。
它只是稍微改变了您创建连接的方式。
var connection = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret'
});
我知道已经有一个可接受的答案,但您真正应该做的是创建一个数据库池,其他答案确实没有给出示例。您必须将其设置为与创建与库的普通数据库连接略有不同。
-edit 您不必担心关闭连接。
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret'
});
pool.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
exports.Pool = pool;
我正在使用 ExpressJS 和 MySQL 开发 Node 应用程序。我正在使用这个模块 https://github.com/felixge/node-mysql/ 并且我正在学习它的使用。 我遇到了如何正确关闭连接的问题。
我就是这样做的:
app.post('/example', function (req, res) {
//BD
var connection = mysql.createConnection({
host: config.database.host,
port: config.database.port,
user: config.database.user,
password: config.database.password,
database: config.database.database
});
var sql = '';
if (varExample != null) {
sql = 'Random query';
connection.query(sql, function (err, results) {
//Get results
connection.end();
});
}
});
有时我必须多次调用此方法才能将数据插入数据库。那时我得到一个错误 'Too many connections'.
在这些情况下关闭连接的方法是什么?
你不应该做的是每次收到请求都打开一个连接。每次连接都很慢,其次驱动程序通常会为您打开一个连接池,所以应该不是问题。无需关闭与 mysql.
的连接基本上你必须这样做
//BD
var connection = mysql.createConnection({
host: config.database.host,
port: config.database.port,
user: config.database.user,
password: config.database.password,
database: config.database.database
});
app.post('/example', function (req, res) {
var sql = '';
if (varExample != null) {
sql = 'Random query';
connection.query(sql, function (err, results) {
//Get results
});
}
});
编辑:添加池选项 拥有一个连接池基本上是我们大多数人想要的服务器必须做很多查询。 它只是稍微改变了您创建连接的方式。
var connection = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret'
});
我知道已经有一个可接受的答案,但您真正应该做的是创建一个数据库池,其他答案确实没有给出示例。您必须将其设置为与创建与库的普通数据库连接略有不同。
-edit 您不必担心关闭连接。
var mysql = require('mysql');
var pool = mysql.createPool({
connectionLimit : 10,
host : 'example.org',
user : 'bob',
password : 'secret'
});
pool.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
exports.Pool = pool;