Node.js MySQL模块中的mysql.createConnection和mysql.createPool有什么区别? [英] What is the difference between mysql.createConnection and mysql.createPool in Node.js MySQL module?

查看:990
本文介绍了Node.js MySQL模块中的mysql.createConnection和mysql.createPool有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图了解

mysql.createConnection

mysql.createConnection

var mysql = require('mysql');

var connection = mysql.createConnection({
  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
});

mysql.createPool

mysql.createPool

var mysql = require('mysql');

var pool  = mysql.createPool({
  connectionLimit : 10,
  host            : 'example.org',
  user            : 'bob',
  password        : 'secret' 
});

Node.js的mysql模块中的

.

in the mysql module for Node.js.

我已经熟悉 mysql.createConection .似乎这是与MySQL建立连接的默认方法.

I am already familiar with mysql.createConection. It seems this is the default way to establish a connection with MySQL.

mysql.createPool 到底做什么?

何时开始使用 mysql.createPool?

mysql.createConnection 相比,使用 mysql.createPool 有什么好处?

What are the benefits of using mysql.createPool as opposed to mysql.createConnection?

推荐答案

创建连接时,只有一个连接,该连接持续到关闭它为止(或被mysql服务器关闭).您可以通过引用将其传递并重新使用,也可以根据需要创建和关闭连接.

When you create a connection, you only have one connection and it lasts until you close it (or it is closed by the mysql server). You can pass it around by reference and re-use it, or you can create and close connections on demand.

池是存储连接的地方.当您从池中请求连接时,您将收到当前未使用的连接或新连接.如果您已经达到连接限制,它将等待连接可用后再继续.这些合并的连接不需要手动关闭,它们可以保持打开状态并易于重用.

A pool is a place where connections get stored. When you request a connection from a pool, you will receive a connection that is not currently being used, or a new connection. If you're already at the connection limit, it will wait until a connection is available before it continues. These pooled connections do not need to be manually closed, they can remain open and be easily reused.

您使用哪种方式完全取决于您,因为它们都以两种不同的方式实现相同的目标.

Which you use is entirely up to you, as they both accomplish the same goal, just in two different ways.

这篇关于Node.js MySQL模块中的mysql.createConnection和mysql.createPool有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆