[SOLVED] ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO)

Issue

I have this problem, I already researched and I could not solve it, I imagine it has something to do with database permissions, but I can not fix it:

if (error) throw error;
       ^

Error: ER_ACCESS_DENIED_ERROR: Access denied for user ''@'localhost' (using password: NO)
at Handshake.Sequence._packetToError (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/sequences/Sequence.js:52:14)
at Handshake.ErrorPacket (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/sequences/Handshake.js:103:18)
at Protocol._parsePacket (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/Protocol.js:279:23)
at Parser.write (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/Parser.js:76:12)
at Protocol.write (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/Protocol.js:39:16)
at Socket.<anonymous> (/home/carlos/www/express-cc/node_modules/mysql/lib/Connection.js:103:28)
at emitOne (events.js:115:13)
at Socket.emit (events.js:210:7)
at addChunk (_stream_readable.js:266:12)
at readableAddChunk (_stream_readable.js:253:11)
--------------------
at Protocol._enqueue (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/Protocol.js:145:48)
at Protocol.handshake (/home/carlos/www/express-cc/node_modules/mysql/lib/protocol/Protocol.js:52:23)
at Connection.connect (/home/carlos/www/express-
cc/node_modules/mysql/lib/Connection.js:130:18)
at Object.<anonymous> (/home/carlos/www/express-cc/db.js:10:12)
at Module._compile (module.js:573:30)
at Object.Module._extensions..js (module.js:584:10)
at Module.load (module.js:507:32)
at tryModuleLoad (module.js:470:12)
at Function.Module._load (module.js:462:3)
at Module.require (module.js:517:17)

—- EDIT —–

my env.default file:

NODE_ENV=DEVELOPMENT

DB_HOST=localhost
DB_USER=user
DB_PASSWORD=userpass
DB_NAME=teste

so this my db.js file:

 var mysql = require('mysql')
  var connection = mysql.createConnection({
  host: process.env.DB_HOST,
  user: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database : process.env.DB_NAME,
  sockertPath: '/var/run/mysqld/mysqld.sock'
})

 connection.connect()

 connection.query('SELECT 1 + 1 AS solution',
 function (error, results, fields) {
if (error) throw error;
console.log('The solution is: ', results[0].
  solution)
});

 module.exports = connection;

Solution

You must create a MySQL user specifically to access the app. Create a user and set a password (do not leave the password null). Do not forget to give this user permission to be able to access the database and tables of app. I hope I have helped.

Answered By – Gladimir Ceroni Catarino

Answer Checked By – Jay B. (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *