Javascript nodejs MySQL中连接池中的交换机数据库
使用连接池连接到nodejs中的MySQL后,如何切换数据库 我以前使用MySQL的普通连接,因为它有一些连接,现在我想使用连接池。但是,在与MySQL建立连接之后,如何更改数据库 以下是我如何更改数据库:Javascript nodejs MySQL中连接池中的交换机数据库,javascript,mysql,node.js,express,connection-pooling,Javascript,Mysql,Node.js,Express,Connection Pooling,使用连接池连接到nodejs中的MySQL后,如何切换数据库 我以前使用MySQL的普通连接,因为它有一些连接,现在我想使用连接池。但是,在与MySQL建立连接之后,如何更改数据库 以下是我如何更改数据库: conn.changeUser({ database: req.session.dbname }, function (err) { if (err) { console.log(err); } else { next(); }
conn.changeUser({
database: req.session.dbname
}, function (err) {
if (err) {
console.log(err);
} else {
next();
}
});
但是现在它显示了conn.changeUser
不是一个函数
下面是连接mysql的方法:
const conn = mysql.createPool({
connectionLimit: 10,
host: config.host,
user: config.user,
password: config.password,
database: 'shaw_elc_gst_13032019'
});
这是当Iconsole.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 }
我建议仔细阅读一下这本书
你说你在使用conn.changeUser(/*…*/)
,但你说你在使用const conn=mysql.createPool(/*…*/)
初始化该conn
常量。这意味着conn
是一个池,而不是一个连接;它没有changeUser
方法也就不足为奇了
如果要更改数据库,则需要在连接上而不是池上进行更改。不要使用速记pool.query
表单,而是使用pool.getConnection
/conn.changeUser
/conn.query
/conn.release
。首先,调用变量pool
,而不是conn
:
const pool = mysql.createPool({
然后
也就是说,如果是我,我会更愿意为每个数据库设置一个连接池,而不是在公共池中更改数据库。对我来说,这可能是纯粹的偏执,但我会这么做。但是如果您不使用单独的池,我建议您始终执行
changeUser
,这样您就可以确定使用的是什么数据库,或者彻底测试mysql
模块如何处理这个问题(并在模块的每个dot版本上重复该测试,除非维护人员记录了该行为).您能试着调试一下,看看conn的值是多少吗?因为像这样的错误不是一个函数,通常只有当它不是必需的对象时才会发生,这意味着conn可能不是一个连接对象。@KiranMathewMohan,但当我使用conn.query
执行查询时,它工作得很好!!您使用的是哪个版本的mysql?您是否也尝试过调试,看看对象中是否有一个名为change user的函数,它可能会给您一些启示。mysql 5.7 i amusing@KiranMathewMohan我已经更新了问题!请查收
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();
});
});