mysql 收到消息“忽略传递给连接的无效配置选项”,如何解决?

ttygqcqt  于 2023-02-03  发布在  Mysql
关注(0)|答案(1)|浏览(218)

这是实际消息:

忽略传递给Connection的无效配置选项:createDatabaseTable。当前这是一个警告,但在MySQL 2的未来版本中,如果向连接传递无效的配置选项,则会引发错误。

应用程序在收到此消息后没有响应。
我使用sequelize &作为mysql 2的连接驱动程序,使用express-session &作为express-mysql-session的会话存储包。
会话存储配置代码为:

const session = require('express-session');
const MySQLStore = require('express-mysql-session')(session);
const mysql = require('mysql2/promise')

const options = {
    host: config.get('db_host'),
    port: config.get('db_port'),
    user: config.get('db_user'),
    password: config.get('db_password'),
    database: config.get('db'),
    createDatabaseTable: false,
    schema: {
        tableName: 'sessions',
        columnNames: {
            session_id: 'sessionId',
            expires: 'sessionExpire',
            data: 'info'
        }
    }
}
const connection = mysql.createPool(options);
const sessionStore = new MySQLStore({}, connection);

session({
    secret: config.get('secret'),
    resave: false,
    saveUninitialized: false,
    store: sessionStore
  }),

如果使用更多的属性,如clearExpired,expiration等,我会收到每个属性的警告消息。因此,我从选项中删除createDatabaseTable属性,应用程序也不会运行。在这种情况下,我应该怎么做?

s8vozzvw

s8vozzvw1#

你需要把mysql 2的选项和MySQLStore的选项分开。没有测试过,但是应该像这样-

const express = require('express');
const app = express();
const session = require('express-session');
const mysql2 = require('mysql2/promise');
const MySQLStore = require('express-mysql-session')(session);

const dbOptions = {
    host: config.get('db_host'),
    port: config.get('db_port'),
    user: config.get('db_user'),
    password: config.get('db_password'),
    database: config.get('db')
};

const storeOptions = {
    createDatabaseTable: false,
    schema: {
        tableName: 'sessions',
        columnNames: {
            session_id: 'sessionId',
            expires: 'sessionExpire',
            data: 'info'
        }
    }
};

const connection = mysql2.createPool(dbOptions);
const sessionStore = new MySQLStore(storeOptions, connection);

app.use(session({
    secret: 'keyboard cat',
    store: sessionStore,
    resave: false,
    saveUninitialized: true,
    cookie: { secure: false }
}));

// simple test
app.get('/', (req, res) => {
    if (req.session.views) {
        req.session.views++
    }
    else {
        req.session.views = 1
    }
    res.send('Session views: ' + req.session.views)
});

app.listen(3000, () => {
  console.log(`Running`)
});

这是会话表的create语句-

CREATE TABLE IF NOT EXISTS `sessions` (
    `sessionId` varchar(128) NOT NULL,
    `sessionExpire` int unsigned NOT NULL,
    `info` text,
    PRIMARY KEY (`sessionId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

相关问题