Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
nvnvashisth opened this issue Apr 22, 2021 · 5 comments

Comments

@nvnvashisth
Copy link

nvnvashisth commented Apr 22, 2021

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 nvnvashisth changed the title Not able to authenticate if try with node-mysql2 client. It works otherwise Not able to authenticate if tried with node-mysql2 client. It works otherwise Apr 22, 2021
@emerabet
Copy link

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 !

@jgtvares
Copy link

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
Copy link

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.

@stonesha
Copy link

stonesha commented Jan 8, 2024

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 #2277 everything works perfectly. Let me know if this helps!

@anhtata
Copy link

anhtata commented Apr 15, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants