-1

In my App I want to connect to my database but every time i wanted to do it this error came

Error: Access denied for user 'root'@'localhost' (using password: YES)
    at PromisePool.query (/Users/user/Development/NodeJS/MySQL/node_modules/mysql2/promise.js:351:22)
    at getNotes (file:///Users/user/Development/NodeJS/MySQL/config/database.js:11:31)
    at file:///Users/user/Development/NodeJS/MySQL/config/database.js:14:21
    at ModuleJob.run (node:internal/modules/esm/module_job:194:25) {
  code: 'ER_ACCESS_DENIED_ERROR',
  errno: 1045,
  sql: undefined,
  sqlState: '28000',
  sqlMessage: "Access denied for user 'root'@'localhost' (using password: YES)"
}

This is my JS file

import mysql from "mysql2"

const pool = mysql.createPool({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'notes_app'
}).promise()

async function getNotes() {
    const [rows] = await pool.query("SELECT * FROM notes")
    return rows
}
const notes = await getNotes()
console.log(notes);
Noah
  • 19
  • 1
  • 3
  • 1. Wrong password 2. Wrong user – lezhni May 18 '23 at 21:37
  • Does this answer your question? [NodeJS/mySQL - ER\_ACCESS\_DENIED\_ERROR Access denied for user 'root'@'localhost' (using password: YES)](https://stackoverflow.com/questions/40477625/nodejs-mysql-er-access-denied-error-access-denied-for-user-rootlocalhost) – Yogendra Jul 04 '23 at 12:22

2 Answers2

0

You are using await within global scope with no async! More specifically I am refering to const notes = await getNotes();

Now if I want to write this code I would smply usen then chain to promisify the action, see below:

pool.query('SELECT * FROM notes')
  .then((result) => {
    console.log(result[0]);
  }).catch((err) => {
    console.log(err);
  });

for more info and examples, you can check the official documentation of mysql2 package via link below:

https://www.npmjs.com/package/mysql2

I really hope you find my text helpful. Please write me if you still have the same issue :)

0

I was conflicted about this problem the whole day. at last, I knew I should add my IP address to valid IP list of "remote MySQL".

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 04 '23 at 12:19
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34630704) – Yogendra Jul 06 '23 at 09:27