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\
Login into your mysql using mysql -u root -p password
Create new user z (MySQL console)
CREATE USER 'z'@'localhost' IDENTIFIED BY '';
GRANT ALL PRIVILEGES ON * . * TO 'z'@'localhost';
Node.js script
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "z",
password: ""
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
con.query("use mysql;", function(err, result) {
if (err) throw err;
console.log(result);
});
con.query("select * from user limit 1;", function(err, result) {
if (err) throw err;
console.log(result);
});
});
For me the problem was having inside the SQL connection object pass: 'mypassword'
instead of password: 'mypassword'
.
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: ''
});
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.
I was facing same error on mac, however whenever I run same code on windows machine it was working absolutely good, without any error. After spending 2 days I found solution.
Below are the steps I followed.
e.g. Avi-MBP:projectDirectory avisurya$ sudo su -
it will as password e.g. Password: enter your mac user password
Now you will be in root e.g. Avi-MBP:~ root#
now again go to project directory e.g. Avi-MBP:~ root# cd /Users/avisurya/projectDirectory
now start node application e.g. in my case "node server.js"