Skip to content
Advertisement

nodejs MySQL – Server requests authentication using unknown plugin

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: Server requests authentication using unknown plugin sha256_password I know that mysqljs and mysql2 do not support sha256, so I confirmed my user was setup for mysql_native_password:

JavaScript

And have confirmed that default_authentication_plugin is set as mysql_native_password.

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.

app.js

JavaScript

dbQuery.js

JavaScript

dbPool.js

JavaScript

Terminal Output – Running the simplified app now returns the row as expected

JavaScript

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.

dbQuery.test.js – currently setup for Mocha, but Jest exposed the same issue

JavaScript

Terminal Output

JavaScript

Thanks in advance for any help, please let me know if any additional information is needed.

Advertisement

Answer

When executing the tests, my env variables were not being populated. The fix was as simple as adding require('dotenv').config({ path: 'path/to/.env' }); to my test file. I was thrown off by the error message returned by MySQL. I’m still not sure why MySQL responds stating sha256_password is requested when no credentials are provided, even when the default_auth_plugin is set to mysql_native_password, but once valid credentials were provided everything works as expected.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement