在 nodejs 的连接池中切换数据库 MySQL

Switch database in a connection pool in nodejs MySQL

在nodejs中使用连接池连接到MySQL后如何切换数据库?

我以前使用 MySQL 的正常连接,因为它有一些 issue 现在我想使用连接池。但是如何在与 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 }

我建议 pooling documentation 彻底阅读。

你说你正在使用 conn.changeUser(/*...*/),但你又说你正在使用 const conn = mysql.createPool(/*...*/); 来初始化 conn 常量。这意味着 conn 是一个 ,而不是一个连接;它没有 changeUser 方法也就不足为奇了。

如果你想改变数据库,你需要在连接上做,而不是在池上。您可以使用 pool.getConnection/conn.changeUser/conn.query/conn.release 而不是使用 shorthand pool.query 形式。首先,调用变量 pool,而不是 conn:

const pool = mysql.createPool({

然后

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();
    });
});

就是说,如果是我,我会更愿意为每个数据库配备一个连接池,而不是在您更改数据库的地方使用公共连接池。这对我来说可能是纯粹的偏执狂,但这是我会做的。但是如果你不使用单独的池,我建议总是做 changeUser 这样你就可以确定你使用的是什么数据库,或者测试 彻底 看看 mysql 模块确实会处理这个问题(并在模块的每个点版本上重复该测试,除非维护者记录了该行为)。