node-mysql2 - :zap: fast node-mysql compatible mysql driver for node.js

How to override how Dates are escaped?

I'm using node-mysql2. I want to override how JavaScript Date objects are escaped.

By default, they are converted to 'YYYY-mm-dd HH:ii:ss' strings, but I want to store them as integers instead.

How can I do this without reimplementing the entire queryFormat function (which parses out ? and :namedPlaceholders)?

Source: (StackOverflow)

Nodejs - mysql2 stream connection and remote host

I need to query a mysql database from one server. The mysql database is located on another network. I can only connect to this network from a specific server.

I need to create a tunnel from server 1, into server 2 and use this tunnel to connect to my mysql database.

This is my current code. This allows me to connect to server 2's local mysql server, but not to a remote mysql server.

var Client = require('ssh2').Client,
mysql = require('mysql2'),

var client = new Client();
var ssh = client.connect({
  host: config.server_tunnel.dstHost,
  port: 22,
  username: config.server_tunnel.username,
  privateKey: config.server_tunnel.privateKey

ssh.forwardOut('', 12345, '', 3306, function (err, stream) {
    user: config.database.username,
    password: config.database.password,
    database: config.database.database,
    stream: stream,
    host: 'remote.host.com'

Source: (StackOverflow)

Encoding not recognized in jest.js

I have a problem testing a project using node-mysql2, react, sequelize and jest. This problem only occurs during testing.

Encoding not recognized: 'cesu8' (searched as: 'cesu8')
    at Object.getCodec (project/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:106:23)
    at Object.getDecoder (project/node_modules/mysql2/node_modules/iconv-lite/lib/index.js:122:23)
    at Object.<anonymous>.exports.decode (project/node_modules/mysql2/lib/parsers/string.js:9:23)
    at Packet.Object.<anonymous>.Packet.readNullTerminatedString (project/node_modules/mysql2/lib/packets/packet.js:373:23)
    at Function.Object.<anonymous>.Handshake.fromPacket (project/node_modules/mysql2/lib/packets/handshake.js:18:31)
    at ClientHandshake.Object.<anonymous>.ClientHandshake.handshakeInit (project/node_modules/mysql2/lib/commands/client_handshake.js:98:38)
    at ClientHandshake.Object.<anonymous>.Command.execute (project/node_modules/mysql2/lib/commands/command.js:40:20)
    at Connection.Object.<anonymous>.Connection.handlePacket (project/node_modules/mysql2/lib/connection.js:515:28)
    at PacketParser.onPacket (project/node_modules/mysql2/lib/connection.js:94:16)
    at PacketParser.executeStart (project/node_modules/mysql2/lib/packet_parser.js:77:14)
    at Socket.<anonymous> (project/node_modules/mysql2/lib/connection.js:102:29)

Source: (StackOverflow)

"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

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('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)
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.

Source: (StackOverflow)

Containerized server application failing to connect to MySQL databases

I'm trying to connect my server code running as a Docker container in our Kubernetes cluster (hosted on Google Container Engine) to a Google Cloud SQL managed MySQL 5.7 instance. The issue I'm running into is that every connection is being rejected by the database server with Access denied for user 'USER'@'IP' (using password: YES). The database credentials (username, password, database name, and SSL certificates) are all correct and work when connecting via other MySQL clients or the same application running as a container on a local instance.

I've verified that all credentials are the same on the local and the server-hosted versions of the app and that the user I'm connecting with has the wildcard % host specified. Not really sure what to check next here, to be honest...

An edited version of the connection code is below:

let connectionCreds = {
    host: Config.SQL.HOST,
    user: Config.SQL.USER,
    password: Config.SQL.PASSWORD,
    database: Config.SQL.DATABASE,
    charset: 'utf8mb4',

if (Config.SQL.SSL_ENABLE) {
    connectionCreds['ssl'] = {
        key: fs.readFileSync(Config.SQL.SSL_CLIENT_KEY_PATH),
        cert: fs.readFileSync(Config.SQL.SSL_CLIENT_CERT_PATH),
        ca: fs.readFileSync(Config.SQL.SSL_SERVER_CA_PATH)

this.connection = MySQL.createConnection(connectionCreds);

Additional information: the server application is built in Node using the mysql2 library to connect to the database. There are no special firewall rules in place that are causing network issues, and that's confirmed by the fact that the library IS connecting, but failing to authenticate.

Source: (StackOverflow)