1

I wrote a js script and it works fine locally, however, when I deploy it to my ubuntu-server I can not connect to the mysql-database. I am getting following error-message:

Error: Access denied for user 'root'@'localhost'
    at PromisePool.query (/root/platformbackend/node_modules/mysql2/promise.js:330:22)
    at IdentificationRepositoryImpl.<anonymous> (/root/platformbackend/src/Repository/IdentificationRepositoryImpl.ts:19:14)
    at step (/root/platformbackend/src/Repository/IdentificationRepositoryImpl.ts:46:23)
    at Object.next (/root/platformbackend/src/Repository/IdentificationRepositoryImpl.ts:27:53)
    at /root/platformbackend/src/Repository/IdentificationRepositoryImpl.ts:21:71
    at new Promise (<anonymous>)
    at __awaiter (/root/platformbackend/src/Repository/IdentificationRepositoryImpl.ts:17:12)
    at IdentificationRepositoryImpl.setupTable (/root/platformbackend/src/Repository/IdentificationRepositoryImpl.ts:61:16)
    at /root/platformbackend/src/SitServer/SitServer.ts:60:24
    at Array.map (<anonymous>) {
  code: 'ER_ACCESS_DENIED_NO_PASSWORD_ERROR',
  errno: 1698,
  sqlState: '28000',
  sqlMessage: "Access denied for user 'root'@'localhost'"

The code, the error originates from is (in condensed form) following:

const connectionOptions = {
    host: process.env.DB_HOST || 'localhost',
    user: process.env.DB_USER || 'root',
    password: process.env.DB_PASSWORD || '',
    database: process.env.DB_NAME || 'test_db',
};

const connection = createPool(connectionOptions);

const insertId = await connection
    .promise()
    .query(sql)
    .then(result => result)
    .catch(error => console.error(error));

Now I am confused, what the error code ER_ACCESS_DENIED_NO_PASSWORD_ERROR actually means.

  • Does it mean, that I provided an empty password?
  • Or does it mean, that it didn't receive a password and it thinks there is no password provided (not even an empty one)?
  • Or does it mean, that it doesn't expect a password to be provided, but it got one?

I have tried...

  • ...setting the root's password to an empty string and providing an empty string as password
  • ...setting the root's password to an empty string and providing no password entry in connectionOptions
  • ...setting the root's password and providing the password in the password entry in connectionOptions

but all with the same error as outcome.

Some research and approaches I also already tried:

Thank you for any help, pointing me in the right direction. If I'm missing any debugging-details, I'm happy to provide them upon request.


Sidenote: I know I shouldn't use the root-user, it's a testing server.

Argee
  • 1,216
  • 1
  • 12
  • 22

2 Answers2

2

Oh great, having followed this tutorial the fourth time, it suddenly worked... I'm sorry for any inconvenience.

If I find out, what went different this time, I'll update this very unsatisfying answer.

Argee
  • 1,216
  • 1
  • 12
  • 22
1

I think this error means that you tried to login without a password. And yes, I think MySQL considers a password of '' (empty string) the same as no password.

Check that process.env.DB_PASSWORD is set. Maybe log to the console whether it was set:

console.info('process.env.DB_PASSWORD was ' + (!process.env.DB_PASSWORD ? 'NOT ' : '') + 'set')

I think whether or not logging in without a password is allowed is a MySQL setting that you can edit somewhere in the .ini file, but in order to prevent the hassle I just always use the password secret for my testing instances and have code like this:

const connectionOptions = {
    // ...
    password: process.env.DB_PASSWORD || 'secret',
    // ...
};
Stijn de Witt
  • 40,192
  • 13
  • 79
  • 80
  • yes, it is set... i'll do the `password: process.env.DB_PASSWORD || 'secret'` anyways, thank you – Argee Jul 23 '20 at 14:01