I\'m trying to connect my NodeJs application with a mysql database but its not connecting for some reason.
I did install the mysql npm like this (typed this in comma
Use the new mysql2 library instead of mysql in order to use the new authentication method of MySQL 8.0
~ Tested in node version 13.6.0 and MySQL 8.0.21
Instead of
mysqlConnection.connect((err) => {
use
connection.connect((err) => {
if(!err)
console.log('Database is connected!');
else
console.log('Database not connected! : '+ JSON.stringify(err, undefined,2));
});
[Here the is tutorials reference for the code]1
For your mysql error:
Execute the following query in MYSQL Workbench:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
Try connecting using node after you do so.
I was having the same problem with mysql
library and I fixed that error by changing to mysql2
Library I recommend you to install mysql2
Run
npm install mysql2
Then
import mysql from "mysql2";
OR
const mysql = require("mysql2")
I hope this will work for you!! Good Luck
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourRootPassword';
-- or
CREATE USER 'foo'@'%' IDENTIFIED WITH mysql_native_password BY 'bar';
-- then
FLUSH PRIVILEGES;