【问题标题】:"mysql_clear_password" plugin error when using knex but not when using mysql2使用 knex 时出现“mysql_clear_password”插件错误,但使用 mysql2 时没有
【发布时间】:2018-01-22 16:37:18
【问题描述】:

我正在尝试使用mysql_clear_password 插件连接到 MySQL 服务器。我为node-mysql2设置连接配置如下:

const connectionConfig = {
  host: rwConfig.host,
  user: rwConfig.user,
  database: rwConfig.database,
  ssl: {
    ca: sslContent
  },
  password
}

然后为了支持mysql_clear_password 插件,我添加了以下内容(我使用的参考链接:https://github.com/sidorares/node-mysql2/issues/438#issuecomment-255343793):

connectionConfig.authSwitchHandler = (data, cb) => {
  console.log('In auth switch handler');
  if (data.pluginName === 'mysql_clear_password') {
    console.log(data);
    console.log(data.pluginData.toString('utf8'));
    console.log('In mysql clear password');
    var tmppassword = connectionConfig.password + '\0';
    var buffer = Buffer.from(tmppassword, 'base64');
    cb(null, buffer);
  }
};

这在我尝试连接到我的数据库时有效。

现在我尝试使用 knexjs 做类似的事情。我使用以下配置对象:

const knexConfig = {
  client: 'mysql2',
  connection: connectionConfig,
  pool: {
    min: 0,
    max: 20
  },
  acquireConnectionTimeout: 10000
};

我作为connection 的值传入的connectionConfig 与我用于连接node-mysql2 的对象相同。然后我创建一个 knex 连接:

const rwConnection = knex(knexConfig);

这出于某种原因会引发此错误:

{ Error: Client does not support authentication protocol requested by server; consider upgrading MySQL client
at Packet.asError (node_modules/mysql2/lib/packets/packet.js:703:13)
at ClientHandshake.Command.execute (node_modules/mysql2/lib/commands/command.js:28:22)
at Connection.handlePacket (node_modules/mysql2/lib/connection.js:515:28)
at PacketParser.onPacket (node_modules/mysql2/lib/connection.js:94:16)
at PacketParser.executeStart (node_modules/mysql2/lib/packet_parser.js:77:14)
at TLSSocket.<anonymous> (node_modules/mysql2/lib/connection.js:386:31)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
at TLSWrap.onread (net.js:547:20)
code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlState: '#08004' }

我不知道为什么这给了我错误。在 knex 文档 (http://knexjs.org/) 中它说:

The connection options are passed directly to the appropriate database client to create the connection, and may be either an object, or a connection string

基于此,我认为它只会通过配置并使用node-mysql2 建立连接。任何帮助将不胜感激。

【问题讨论】:

    标签: mysql node.js mysql2 knex.js node-mysql2


    【解决方案1】:

    我发现了这个问题,它现在可以工作了。 Knex 没有将所有属性传递给 msyql2 客户端。

    这是我为可能遇到此问题的其他人所做的。我在node_modules/knex/lib/dialect/mysql2 中修改了knex 传递给mysql2 的配置,并将authSwitchHandler 添加到configOptions 数组中。

    【讨论】:

    • 这已经在master中修复了(下一个版本将是0.14),但我不记得它是否已经发布了。您使用的是哪个 knex 版本?
    猜你喜欢
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2020-05-20
    • 2018-12-24
    • 2014-03-20
    相关资源
    最近更新 更多