0

I am building an Expo App in ' MacOS Mojave 10.14.6 ' using mysql database.I want to create forgot password page which needs to send email notification to user to reset password.So I am writing a Node.js program which will connect to Mysql.But I am getting error.

Below is the Node.js Database program:

database.js:

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

connection.connect((err) => {

if(!err)
    console.log('Database is connected!');
else
    console.log('Database not connected! : '+ JSON.stringify(err, undefined,2));
});
module.exports = connection;

After running the above program,I am getting the below error:

Database not connected! : {
"code": "ER_ACCESS_DENIED_ERROR",
"errno": 1045,
"sqlState": "28000",
"sqlMessage": "Access denied for user 'root'@'localhost' (using password: NO)"
}

I tried to alter the password in Mysql Workbench using the below command :

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'Test123'; But it gives an error as :

Incorrect usage of ALTER and SYSTEM USER.

I am able to connect to database from Mysql Workbench with a username and password.Can anyone say where I am going wrong.Thanks in Advance.

  • Have you checked [this](https://stackoverflow.com/questions/40477625/nodejs-mysql-er-access-denied-error-access-denied-for-user-rootlocalhost)? – Apoorva Chikara Jan 11 '22 at 14:59

1 Answers1

0

Actually I have my mysql database in Google Cloud Platform with separate hostname, username and password.I could connect with that database by giving correct credentials.

Below is the database.js code:

var mysql=require('mysql2');
var connection=mysql.createConnection({
host:'78.89.***.**',
user:'Poornima',
password:'test123',
database:'testdb'
});

connection.connect((err) => {

if(!err)
console.log('Database is connected!');
else
console.log('Database not connected! : '+ JSON.stringify(err, 
undefined,2));
});
module.exports = connection;

And the output is as below:

Database is connected!