KnexUser
KnexUser

Reputation: 11

"mysql_clear_password" plugin error when using knex but not when using mysql2

I am trying to connect to MySQL server using the mysql_clear_password plugin. I set up the connection configuration for node-mysql2 as follows:

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

Then to support the mysql_clear_password plugin I added the following (link of the reference i used: 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);
  }
};

This works when I attempt to connect to my database.

Now I try to do something similar using knexjs. I use the following configuration object:

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

The connectionConfig I passed in as the value for connection is the same object I used for connecting with node-mysql2. Then I create a knex connection:

const rwConnection = knex(knexConfig);

This for some reason throws this error:

{ 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' }

I don't know why this is giving me the error. In the knex documentation (http://knexjs.org/) it says:

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

Based on that I thought it would just pass the configuration through and make the connection using node-mysql2. Any help would be appreciated.

Upvotes: 0

Views: 1155

Answers (1)

KnexUser
KnexUser

Reputation: 11

I figured out the issue and it works now. Knex was not passing on all the attributes to the msyql2 client.

Here is what I did for anyone else who might run into this problem. I modified the configurations that knex was passing to mysql2 in the node_modules/knex/lib/dialect/mysql2 and added authSwitchHandler to the configOptions array.

Upvotes: 1

Related Questions