NodeJS/mySQL - ER_ECCESS_DENIED_ERROR Access denied for user 'root'@'localhost' (using password: YES)

NodeJS/mySQL - ER_ECCESS_DENIED_ERROR Access denied for user 'root'@'localhost' (using password: YES)

node js access denied
sequelizeaccessdeniederror: access denied for user
mysql access denied for user
access denied for user 'root'@'localhost'
failed to connect to mysql access denied for user
fivem er_access_denied_error access denied for user
node js mysql web application
access denied for user ''@'localhost'

I am attempting to connect to mySQL through a NodeJS file, but I receive the following error:

{ Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost' (using password: YES)
    at Handshake.Sequence._packetToError (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/sequences/Sequence.js:30:14)
    at Handshake.ErrorPacket (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/sequences/Handshake.js:67:18)
    at Protocol._parsePacket (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/Protocol.js:197:24)
    at Parser.write (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/Parser.js:62:12)
    at Protocol.write (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/Protocol.js:37:16)
    at Socket.ondata (_stream_readable.js:555:20)
    at emitOne (events.js:101:20)
    at Socket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at Socket.Readable.push (_stream_readable.js:134:10)
    --------------------
    at Protocol._enqueue (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/Protocol.js:110:26)
    at Protocol.handshake (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/protocol/Protocol.js:42:41)
    at Connection.connect (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/Connection.js:81:18)
    at Connection._implyConnect (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/Connection.js:222:10)
    at Connection.query (/home/matthew/Node/mySqlTest/node_modules/mysql/lib/Connection.js:137:8)
    at Object.<anonymous> (/home/matthew/Node/mySqlTest/index.js:11:12)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
  code: 'ER_ACCESS_DENIED_ERROR',
  errno: 1045,
  sqlState: '28000',
  fatal: true }

The weird thing is that I can connect fine through the terminal by running mysql -u root -p. I only get this error when running my javascript. I have been all over Google and StackOverflow, but still have not found a solution that works. I am using MySQL 5.7.16 on Ubuntu 16.04.1 on a VIRTUAL MACHINE. Not sure if a VM makes a difference here. My Javascript code is below:

'use strict';                                                                                                                                      

var mysql = require('mysql');

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password'
});

connection.query(
    'SELECT "foo" AS first_field, "bar" AS second_field',
    function(err, results, fields) {
        console.log(err);
        console.log(results);
        connection.end();
    }
);

I have tried using 'locahost' as well as '127.0.0.1' in my javascript. I have a 'root' user for both 'localhost' and '127.0.0.1' in mySql.user table and I am able to see this by executing SELECT user, host FROM mysql.user WHERE user='root';

I have added privileges to 'root' user by executing this:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' IDENTIFIED BY 'password';

I ran the above on 127.0.0.1 as well. I have also tried this:

GRANT ALL PRIVILEGES ON *.* TO 'root'@'localhost' WITH GRANT OPTION

I have attempted to reset the root password like this: https://help.ubuntu.com/community/MysqlPasswordReset

I have run FLUSH PRIVILEGES after each attempt. I've stopped and restarted mySQL. I have uninstalled mySQL completely and reinstalled.

All to no avail. I receive the access denied error every time I try to run the javascript, but I have absolutely no issues when I connect to mySQL via the terminal.

Any ideas?


I have the same problem, I solved it by changing the password to empty string.

var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: ''
});

NodeJS/mySQL, NodeJS/mySQL – ER_ECCESS_DENIED_ERROR Access denied for user 'root'​@'localhost' (using password: YES). Posted by: admin  First, I want to thank Neo (LOL) for this post from 2018, Node.js and mysql - ER_ACCESS_DENIED_ERROR I could not get the Node-RED mysql module to work and searched Google until all my links were purple! I kept getting ER_ACCESS_DENIED_ERROR with the right credentials. Nothing on the web was (0 Replies)


I had a similar problem. I was running mysql in a Docker container and had the same error when trying to connect to it from my node app.

It appeared, that I had run the Docker container without exposing the port, hence it was 3306 inside the container, but would not have been accessible through localhost:3306. Why I got ER_ACCESS_DENIED_ERROR error was because I actually had some other mysql server running on the port 3306, with different username and password.

To see if or what you have running on the specific port type:

ps axu | grep 3306

Since I already had something on port 3306, to make the server accessible to my app I changed a port to 3307 and run my docker mysql container with the command:

docker run --name=<name> -e MYSQL_ROOT_PASSWORD=<password> -p 3307:3306 -d mysql

After starting mysql client inside Docker with command:

docker exec -it <name> mysql -u root -p

And after creating a database to connect to, I was able to connect to my mysql db from my node app with these lines:

 const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'database',
    port: 3307
 });

 connection.connect();

Hopefully helps someone new to docker and mysql :)

Node.js MySQL, To access a MySQL database with Node.js, you need a MySQL driver. This tutorial will use the "mysql" module, downloaded from NPM. To download and install  I can log in to the mysql server just fine on CLI. The issue appears to be that for whatever reason the user name and password aren't being used by mysql (Access denied for user ''@'localhost' (using password: NO)) even though they are specified above.


Try adding a port field:

var connection = mysql.createConnection({
   host: 'localhost',
   user: 'root',
   password: 'password',
   port: 3307
});

javascript - NodeJS/MySQL, que estoy tratando de conectar a MySQL a través de un archivo de NodeJS, pero recibo el siguiente error: { Error: ER_ACCESS_DENIED_ERROR: Access  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.


I had the same problem and changing password of database user worked for me. Follow these steps :

  1. Open MySQL Workbench

  2. Open Local instance MySQL57 using old password

  3. Go to Server>Users and Privileges
  4. Change password, and login to MySQL again. OR Create a newuser and set privileges. (If changing password do not work.)

javascript, NodeJS/mySQL - ER_ECCESS_DENIED_ERROR Acceso denegado para el ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'localhost'  Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'magento'@'me.localtestdomain' (using password: YES) But with the same connection details I can connect using mysql workbench. And with the same connection details but using this module and change host to 'localhost' and port to a local ssh-tunnel port, i can connect.


For mysql version 2.16.0 (Sept 2018):

just create a new user on mysql.

GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';

replace username and password.

NodeJS+mySQL+ER+ECCESS+DENIED+ERROR+Access+denied , Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'myuser '@'localhost' (using password: YES) when i run my node script from root (su sudo on ubuntu 14.04) the script works when i run as myuser (anyway it explicitly tells node-mysql to use myuser)


I am trying to run a simple node sample for access a mysql db and am getting the following error-Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'@'ubuntu.local' (using password: YES) var


Access denied for user 'user'@'127.0.0.1' (using password: YES). But when I tried to connect to this DB from PHP script and from PhpMyadmin all worked correct. I was confused .


Node.js & MySQL - 3.2. ER ACCESS DENIED ERROR 오류 처리 Learn Sequelize ORM on Node.js with MySQL From Scratch in One Video How to solve mysql_connect(): Access denied for user 'root