node-mysql2 icon indicating copy to clipboard operation
node-mysql2 copied to clipboard

Not able to authenticate if tried with node-mysql2 client. It works otherwise

Open nvnvashisth opened this issue 3 years ago • 5 comments

Hello Team,

I am try to connect mysql server with node-mysql2 from remote server. Somehow I am able to connect through mysql cli, but unfortunately can not connect through the script.

Note: Authentication plugin enabled as mysql_native_password and mysql 8.0

This I get when I try to make connection through mysql2

Can't execute Error: Access denied for user 'someuser'@'someip' (using password: YES)
    at PromisePool.query (/home/airflow-user/npmPackages/db/node_modules/mysql2/promise.js:340:22)
    at MySQL.executeAny (/home/airflow-user/npmPackages/db/src/dbs/mysql.js:46:34)
    at Object.<anonymous> (/home/airflow-user/npmPackages/db/src/dbs/mysql.js:119:29)
    at Generator.next (<anonymous>) {
  code: 'ER_ACCESS_DENIED_ERROR',
  errno: 1045,
  sqlState: '28000',
  sqlMessage: "Access denied for user 'someuser'@'someip' (using password: YES)"
}

But somehow it can login without any problem from the same remote server with below command. mysql -h rds.amazon.com -P 3306 -u someuser -p

Any idea what could be the issue?

nvnvashisth avatar Apr 22 '21 14:04 nvnvashisth

Hello, same issue with an rds database. I tried to define ssl option "Amazon RDS" but without success. anyone have an working example to connect to a mysql rds instance? Thanks for your help !

emerabet avatar May 23 '21 12:05 emerabet

Similar problem here. In my case, user is being omitted in the message. The error: Access denied for user ''@'77.54.4.223' (using password: YES). I can connect successfully using Workbench or MySQL CLI. But it does not work through NodeJs using mysql2. It works fine with a local database, the problem seems to be with remote databases.

This is my code for creating the connection:

const { createConnection } = require('mysql2');

const { connections } = require('../../config');

const client = createConnection({ ...connections.database });

// This is my database object:
// {
//    host: process.env.MAIN_DB_HOST,
//    port: process.env.MAIN_DB_PORT,
//    database: process.env.APP_DATABASE,
//    user: process.env.APP_USER,
//    password: process.env.APP_PASSWORD
// }

I've placed a breakpoint in this line for debugging and realized that it was being called twice. Inspecting the value of opts.config at the first call, I could confirm that my credentials were being passed correctly. But at the second call, I realized that only the host parameter was being processed correctly. database, port, user and password are being passed as undefined, which is very odd. From this point of view, the error kinda makes sense, since the user and password are not being passed.

Now we need to find out why this is happening.

Any clues?

Extra info

  • My project is an API, developed with Restify;
  • Yes, I'm loading environment variables correctly with dotenv;
  • mysql2 version is 2.3.3;

jgtvares avatar Dec 24 '21 02:12 jgtvares

Solved my problem! After a while I decided to comment the files where I was using mysql2 and I realized that the Connection class was still being called. Then I removed mysql2 from my node_modules and a Module not found error was thrown. So I could see that an internal lib that I use also uses mysql2, and it there was a environment variable missing for that lib.

jgtvares avatar Dec 25 '21 20:12 jgtvares

A little late to the party, but I was using v3.6.1 and was still running into the same type of issue. I verified my credentials were being loaded correctly and they were. After updating to v3.6.5 where they added https://github.com/sidorares/node-mysql2/pull/2277 everything works perfectly. Let me know if this helps!

stonesha avatar Jan 08 '24 20:01 stonesha

First rds.amazon.com/docker locale/intl then printenv Try to use decodeURIComponent in your code

anhtata avatar Apr 15 '24 08:04 anhtata