3

All - i've been learning node. I'm familiar more recently with php and mysql so have been using mysql as the database. Having an issue with the connection to mysql. I am using the mysql and dotenv packages. I had it working fine yesterday. However when i try today i am getting an error from mysql saying it is denying access for ''@local host which looks like an anonymous user. Error below:

Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: YES)
    at Handshake.Sequence._packetToError (C:\Websites\quotebuilder\node_modules\mysql\lib\protocol\sequences\Sequence.js:47:14)
    at Handshake.ErrorPacket (C:\Websites\quotebuilder\node_modules\mysql\lib\protocol\sequences\Handshake.js:123:18)
    at Protocol._parsePacket (C:\Websites\quotebuilder\node_modules\mysql\lib\protocol\Protocol.js:291:23)
    at Parser._parsePacket (C:\Websites\quotebuilder\node_modules\mysql\lib\protocol\Parser.js:433:10)
    at Parser.write (C:\Websites\quotebuilder\node_modules\mysql\lib\protocol\Parser.js:43:10)
    at Protocol.write (C:\Websites\quotebuilder\node_modules\mysql\lib\protocol\Protocol.js:38:16)
    at Socket.<anonymous> (C:\Websites\quotebuilder\node_modules\mysql\lib\Connection.js:88:28)
    at Socket.<anonymous> (C:\Websites\quotebuilder\node_modules\mysql\lib\Connection.js:526:10)
    at Socket.emit (events.js:223:5)
    at addChunk (_stream_readable.js:309:12)
    --------------------
    at Protocol._enqueue (C:\Websites\quotebuilder\node_modules\mysql\lib\protocol\Protocol.js:144:48)
    at Protocol.handshake (C:\Websites\quotebuilder\node_modules\mysql\lib\protocol\Protocol.js:51:23)
    at Connection.connect (C:\Websites\quotebuilder\node_modules\mysql\lib\Connection.js:116:18)
    at Object.<anonymous> (C:\Websites\quotebuilder\db.js:16:12)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18) {
  code: 'ER_ACCESS_DENIED_ERROR',
  errno: 1045,
  sqlMessage: "Access denied for user ''@'localhost' (using password: YES)",
  sqlState: '28000',
  fatal: true

To make sure i am definitely pulling in the relevant variables i am printing them to the console so i can see them and they all look as expected. My code is below:

// initialize database connection
const mysql = require('mysql');

console.log(process.env.DB_HOST);
console.log(process.env.DB_USER);
console.log(process.env.DB_PASS);
console.log(process.env.DB_DATABASE);

const connection = mysql.createConnection({
  host: process.env.DB_HOST,
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_DATABASE
});

connection.connect((err) => {
  if (err) throw err;
  console.log('Connected!');
});

When i simply hardcode the credentials i still get the same error as if blanks are being passed in.

Is there anything in the way node works that would cause this issue i am having? Or in the connection variables (eg missing quotes) that would cause this?

Thanks

1
  • 2
    Access denied for user ''@'localhost' (using password: YES) means that username (at least) is not transferred into the connection parameters. Commented Feb 6, 2020 at 9:34

1 Answer 1

3

I believe you need to use user, not username in your connection configuration, (see mysql docs here) e.g.:

const connection = mysql.createConnection({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_DATABASE
});

This should resolve the issue!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I actually just found the solution myself by having a look at some simple githib projects using mysql. I feel like such a moron. But appreciate you taking time to help me.
I've made similar errors a million times, it's extremely easy to do! JavaScript can be a little unforgiving though, in another language you'd probably catch this at compile time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.