我正在尝试使用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建立连接即可。任何帮助都将不胜感激。
发布于 2017-08-15 08:01:46
我找出了问题所在,现在它起作用了。Knex未将所有属性传递给msyql2客户端。
以下是我为其他可能遇到此问题的人所做的工作。我修改了knex在node_modules/knex/lib/dialect/mysql2中传递给mysql2的配置,并将authSwitchHandler添加到configOptions阵列。
https://stackoverflow.com/questions/45681300
复制相似问题