[英]Switch database in a connection pool in nodejs MySQL
How can I switch the database after connecting to MySQL in nodejs using connection pool? 如何在使用连接池连接到nodejs中的MySQL后切换数据库?
I used to use the normal connection with MySQL since it has some issue now I would like to use the connection pooling. 我曾经使用与MySQL的正常连接,因为它现在有一些问题我想使用连接池。 But how can I change the database after creating a connection with MySQL?
但是如何在创建与MySQL的连接后更改数据库?
Here is how I change the database: 以下是我更改数据库的方法:
conn.changeUser({
database: req.session.dbname
}, function (err) {
if (err) {
console.log(err);
} else {
next();
}
});
But now it shows conn.changeUser
is not a function 但现在它显示
conn.changeUser
不是一个函数
Here is the method to connect with mysql: 这是连接mysql的方法:
const conn = mysql.createPool({
connectionLimit: 10,
host: config.host,
user: config.user,
password: config.password,
database: 'shaw_elc_gst_13032019'
});
This is the result when I console.log(conn)
: 这是我在
console.log(conn)
时的结果:
Pool {
_events:
[Object: null prototype] {
connection: [Function],
acquire: [Function],
enqueue: [Function],
release: [Function],
error: [Function] },
_eventsCount: 5,
_maxListeners: undefined,
config:
PoolConfig {
acquireTimeout: 10000,
connectionConfig:
ConnectionConfig {
host: 'localhost',
port: 3306,
localAddress: undefined,
socketPath: undefined,
user: 'root',
password: '****',
database: 'shaw_elc_gst_13032019',
connectTimeout: 10000,
insecureAuth: false,
supportBigNumbers: false,
bigNumberStrings: false,
dateStrings: false,
debug: undefined,
trace: true,
stringifyObjects: false,
timezone: 'local',
flags: '',
queryFormat: undefined,
pool: [Circular],
ssl: false,
multipleStatements: false,
typeCast: true,
maxPacketSize: 0,
charsetNumber: 33,
clientFlags: 455631 },
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0 },
_acquiringConnections: [],
_allConnections:
[ PoolConnection {
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 11069,
_pool: [Circular] },
PoolConnection {
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 11067,
_pool: [Circular] },
PoolConnection {
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 11070,
_pool: [Circular] },
PoolConnection {
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 11068,
_pool: [Circular] },
PoolConnection {
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 11071,
_pool: [Circular] },
PoolConnection {
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 11072,
_pool: [Circular] },
PoolConnection {
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 11073,
_pool: [Circular] },
PoolConnection {
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 11074,
_pool: [Circular] },
PoolConnection {
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 11075,
_pool: [Circular] },
PoolConnection {
_events: [Object],
_eventsCount: 2,
_maxListeners: undefined,
config: [ConnectionConfig],
_socket: [Socket],
_protocol: [Protocol],
_connectCalled: true,
state: 'authenticated',
threadId: 11076,
_pool: [Circular] } ],
_freeConnections: [],
_connectionQueue:
[ [Function],
[Function],
[Function],
[Function],
[Function],
[Function],
[Function],
[Function] ],
_closed: false }
I suggest giving the pooling documentation a thorough read. 我建议仔细阅读汇集文档 。
You've said you're using conn.changeUser(/*...*/)
, but then you've said you're using const conn = mysql.createPool(/*...*/);
你已经说过你正在使用
conn.changeUser(/*...*/)
,但是你已经说过你正在使用const conn = mysql.createPool(/*...*/);
to initialize that conn
constant. 初始化
conn
常量。 That means conn
is a pool , not a connection; 这意味着
conn
是一个池 ,而不是一个连接; it's not surprising that it doesn't have a changeUser
method. 它没有
changeUser
方法也就不足为奇了。
If you want to change database, you need to do it on the connection, not the pool. 如果要更改数据库,则需要在连接上执行此操作,而不是池。 Instead of using the shorthand
pool.query
form, you'd do pool.getConnection
/ conn.changeUser
/ conn.query
/ conn.release
. 您可以使用
pool.getConnection
/ conn.changeUser
/ conn.query
/ conn.release
而不是使用简写的pool.query
表单。 First, call your variable pool
, not conn
: 首先,调用变量
pool
,而不是conn
:
const pool = mysql.createPool({
then 然后
pool.getConnection(function(err, conn) {
if (err) {
// handle/report error
return;
}
conn.changeUser({
database: req.session.dbname
}, function (err) {
if (err) {
// handle/report error
return;
}
// Use the updated connection here, eventually
// release it:
conn.release();
});
});
That said , if it were me, I'd be more comfortable having a connection pool per database, rather than a common pool where you change the database. 也就是说 ,如果是我,我会更乐于为每个数据库建立一个连接池,而不是更改数据库的公共池。 That could be pure paranoia on my part, but it's what I'd do.
这对我来说可能是纯粹的偏执狂,但这就是我要做的事情。 But if you don't use separate pools, I suggest always do the
changeUser
so you're sure what database you're using, or test thoroughly to see what the mysql
module does to handle this (and repeat that testing on every dot release of the module, unless the maintainer documents the behavior). 但是如果你不使用单独的池,我建议你总是使用
changeUser
这样你就可以确定你正在使用什么数据库,或者彻底测试看看mysql
模块处理这个问题的方法(并在每个dot版本上重复测试)该模块,除非维护者记录该行为)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.