title: nodejs MySQL - Server requests authentication using unknown plugin
body: <p>When attempting to connect to MySQL 8.0.21 server running Ubuntu 20.04 using NodeJS and mysql2 package, I receive the common error below: <code>Server requests authentication using unknown plugin sha256_password</code> I know that mysqljs and mysql2 do not support sha256, so I confirmed my user was setup for mysql_native_password:</p>
  <pre><code>ALTER USER 'userName'@'%' IDENTIFIED WITH mysql_native_password BY 'password';
  </code></pre>
  <p>And have confirmed that default_authentication_plugin is set as mysql_native_password.</p>
  <p>What makes this a strange issue, is that it only occurs when attempting to unit test the function in Mocha or Jest. When running the app normally, I am able to connect and make DB calls with no issues. To simplify troubleshooting, I created a new app.js file that only calls the dbQuery.getRow() function. Contents of those files and the output is given below.</p>
  <p>app.js</p>
  <pre><code>(async function main () {
      require('dotenv').config({ path: __dirname + '/config/.env' });
      const dbQuery = require('./src/js/dbQuery');
      let result = await dbQuery.getRow('table', 'c9024a7aead711eab20be6a68ff5219c');
      console.log(result);
  })();
  </code></pre>
  <p>dbQuery.js</p>
  <pre><code>const dbPool = require('./dbPool');
  async function getRow(tableName, guid) {
      try {
          let sql = `
                  SELECT *
                  FROM \`${tableName}\`
                  WHERE guid='${guid}'`;
          let [rows] = await dbPool.execute(sql);
          return rows[0];
      } catch (ex) {
          console.log('dbQuery getRow failed with error: ' + ex);
          return { error: true, message: ex };
      }
  }
  </code></pre>
  <p>dbPool.js</p>
  <pre><code>const { env } = require('process');
  const mysql = require('mysql2/promise');
  const dbPool = mysql.createPool({
      host: env.DB_HOST,
      port: env.DB_PORT,
      database: env.DB_NAME,
      user: env.DB_USER,
      password: env.DB_PW,
      // waitForConnections: env.WAIT_FOR_CONNECTIONS.toUpperCase() == 'TRUE' ? true : false,
      connectTimeout: 10000,
      connectionLimit: parseInt(env.CONNECTION_LIMIT),
      queueLimit: parseInt(env.QUEUE_LIMIT)
  });
  
  module.exports = dbPool;
  </code></pre>
  <p>Terminal Output - Running the simplified app now returns the row as expected</p>
  <pre><code>node app.js
  BinaryRow {
    guid: 'c9024a7aead711eab20be6a68ff5219c',
    name: 'spiffyRow',
    displayValue: 'Spiffy Display Value'
  }
  
  </code></pre>
  <p>However, when I attempt to do the same DB call in either Jest or Mocha, I run into the issue again, where it appears mysql2 is attempting to use the wrong authentication plugin.</p>
  <p>dbQuery.test.js - currently setup for Mocha, but Jest exposed the same issue</p>
  <pre><code>const dbQuery = require('../src/js/dbQuery');
  
  describe('MySQL DB Operations', function () {
    describe('#getRow()', function () {
      it('Should return row with guid specified', async function (done) {
        let result = await dbQuery.getRow('table', 'c9024a7aead711eab20be6a68ff5219c');
        if (result.guid == 'c9024a7aead711eab20be6a68ff5219c') done();
        else done(result.error);
      });
    });
  });
  </code></pre>
  <p>Terminal Output</p>
  <pre><code>npm test
  
  MySQL DB Operations
      #getRow()
  dbQuery getRow failed with error: Error: Server requests authentication using unknown plugin sha256_password. See TODO: add plugins doco here on how to configure or author authentication plugins.
        1) Should return row with guid specified
  
  
    0 passing (49ms)
    1 failing
  </code></pre>
  <p>Thanks in advance for any help, please let me know if any additional information is needed.</p>