Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node MySQL connection

Tags:

node.js

mysql

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

like image 890
EJM76 Avatar asked Jul 31 '26 18:07

EJM76


1 Answers

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!

like image 180
Terry Lennox Avatar answered Aug 02 '26 09:08

Terry Lennox