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\
I had the same problem and changing password of database user worked for me. Follow these steps :
Open
MySQL Workbench
Open
Local instance MySQL57
using old passwordGo to
Server
>Users and Privileges
Change password, and login to MySQL again.
OR
Create a newuser and set privileges. (If changing password do not work.)
I have solved this by adding socket path to connection configuration. For more details you can see here
You had to add the new user with an IP of the allowed host section not of the name of "localhost"
// Do the mySQL Stuff
var con = mysql.createConnection({
host: 'localhost',
user: 'user',
password: 'mypwd',
database: 'database',
port: 3306,
debug: true
});
//MYSQL Statement
RENAME USER 'myuser'@'localhost' TO 'myuser'@'127.0.0.1';
Try adding a port field:
var connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
port: 3307
});
Try n make sure that you use the credentials that you use to login your database are correct
const Sequelize = require('sequelize')
const db = {}
const sequelize = new Sequelize('ochiengsDatabase', 'ochienguser', '
mydbpassword', {
host: 'localhost',
dialect: 'mysql',
operatorsAliases: false,
I was getting the same issue, but using require('mariadb')
, which is essentially the same. So my answer should apply to both drivers.
The problem was 2 fold:
host: 'localhost', user: 'user'
is always resolving as 'user'@'127.0.0.1'
on the database. So don't use localhost but 127.0.0.1 instead!
The password encryption scheme was incompatible between client and server, apparently the Node client is using mysql_native_password
.
Here's the solution: (from the mysql
command-line client)
# If you don't have a 127.0.0.1 equivalent user:
CREATE USER 'root'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY 'password';
# If you already have the user, reset its password:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
# Redo your grants on the 127.0.0.1 user:
GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';
FLUSH PRIVILEGES;
Voilà, now connect from Node and everything works:
const mariadb = require('mariadb'); // or require('mysql');
const pool = mariadb.createPool({
host: 'localhost', // or host: '127.0.0.1'
user: 'root',
password: 'password',
database: 'mydatabase', // don't forget the database
port: 3306,
connectionLimit: 5
});
References:
Here's where I got the solution from, read for more info