Recently I was working on Node.js application. In my application, I faced connection issue with MySQL server. Everything was good still I was getting following error.
In this article, I will show you why this error occured and how to solve this error.
While going through Googling the issue, I came to know that MySQL 8.0 is using caching_sha2_password
authentication plugin rather than mysql_native_password
and Node.js Mysql package uses mysql_native_password
authentication plugin.
Solution:
In this situation, either you can change current user password according to mysql_native_password
authentication plugin or you need to create new user with mysql_native_password
authentication plugin.
Change current user password to authentication plugin
To change current user password to mysql_native_password
authentication plugin, you need to login to MySQL command line and run the following command. Note that root
is username and password
is the password for that user.
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
Don't forget to flush privileges:
FLUSH PRIVILEGES;
And that's it, logout to MySQL command line:
quit;
Add new user with mysql_native_password authentication plugin:
To create a new user, login to MySQL command line and run the following command with username and password.
CREATE USER 'newuser'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
You may need to grant privileges to new user. If you want to give all privileges, run the following query.
GRANT ALL PRIVILEGES ON *.* TO ‘newuser’@'localhost';
Or you may grant specific privileges with following query:
GRANT CREATE, SELECT, INSERT ON *.* TO ‘newuser’@'localhost';
And reload the privileges:
FLUSH PRIVILEGES;
And close the MySQL connection:
quit;
After that, you can connect Node.js application with MySQL.
I hope this article will help you on your development.