Search code examples
mysqlnode.jsnode-mysql

Connecting to MariaDB with nodejs over ssl with clear text password


I am trying to connect to mariadb instance over ssl,

var mysql = require('mysql');
var conn = mysql.createConnection({
    user: 'user',
    password: 'password',
    debug: true,
    port: '3306',
    host: "host",
    ssl: {
        "ca": ca
    }
});

conn.connect();

the ca is an array of cerificates. I am getting the following error.

Error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MariaDB client

I am able to connect to the db using python with mysql.connector.

After setting it on debug mode, I can see the client is trying to use mysql_native_password authentication. I need to use mysql_clear_password.


Solution

  • if your server supports plugin based auth and auth switch request, you can try following code:

    var mysql = require('mysql2');
    var conn = mysql.createConnection({
      user: 'user',
        password: 'password',
        debug: true,
        port: '3306',
        host: "host",
        ssl: {
            "ca": ca
        },
        authSwitchHandler: (data, cb) => {
          if (data.pluginName === 'mysql_clear_password') {
          // https://dev.mysql.com/doc/internals/en/clear-text-authentication.html
          var password = 'password\0';
          var buffer = Buffer.from(password);
          cb(null, buffer);
        }
      });
    

    unfortunately initial auth type is always mysql_native with node-mysql2 ( I hope to fix this soon ), so you need both auth_plugin and auth_switch support enabled on server

    Edit: Syntax