NodeJS/mySQL - ER_ACCESS_DENIED_ERROR Access denied for user 'root'@'localhost' (using password: YES)

前端 未结 29 4259
北恋
北恋 2020-12-13 13:26

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\         


        
相关标签:
29条回答
  • 2020-12-13 13:57

    Login into your mysql using mysql -u root -p password

    Create new user z (MySQL console)

    CREATE USER 'z'@'localhost' IDENTIFIED BY '';
    GRANT ALL PRIVILEGES ON * . * TO 'z'@'localhost';
    

    Node.js script

    var mysql = require('mysql');
    
    var con = mysql.createConnection({
        host: "localhost",
        user: "z",
        password: ""
    });
    
    con.connect(function(err) {
    
        if (err) throw err;
        console.log("Connected!");
    
        con.query("use mysql;", function(err, result) {
            if (err) throw err;
            console.log(result);
        });
    
        con.query("select * from user limit 1;", function(err, result) {
            if (err) throw err;
            console.log(result);
        });
    
    });
    
    0 讨论(0)
  • 2020-12-13 13:57

    For me the problem was having inside the SQL connection object pass: 'mypassword' instead of password: 'mypassword'.

    0 讨论(0)
  • 2020-12-13 13:58

    I have the same problem, I solved it by changing the password to empty string.

    var mysql = require('mysql');
    var connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: ''
    });
    
    0 讨论(0)
  • 2020-12-13 13:59

    For mysql version 2.16.0 (Sept 2018):

    just create a new user on mysql.

    GRANT ALL PRIVILEGES ON *.* TO 'username'@'localhost' IDENTIFIED BY 'password';
    

    replace username and password.

    0 讨论(0)
  • 2020-12-13 13:59

    I was facing same error on mac, however whenever I run same code on windows machine it was working absolutely good, without any error. After spending 2 days I found solution.

    Below are the steps I followed.

    1. to your project folder in terminal and run "sudo su -" command

    e.g. Avi-MBP:projectDirectory avisurya$ sudo su -

    1. it will as password e.g. Password: enter your mac user password

    2. Now you will be in root e.g. Avi-MBP:~ root#

    3. now again go to project directory e.g. Avi-MBP:~ root# cd /Users/avisurya/projectDirectory

    4. now start node application e.g. in my case "node server.js"

    0 讨论(0)
提交回复
热议问题