Как я могу переключить базу данных после подключения к MySQL в nodejs с помощью пула соединений?
Раньше я использовал обычное соединение с MySQL, поскольку у него есть проблема , теперь я хотел бы использовать пул соединений. Но как я могу изменить базу данных после создания соединения с MySQL?
Вот как я могу изменить базу данных:
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'
});
Это результат, когда я 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 }