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

前端 未结 29 4258
北恋
北恋 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:41

    I had the same problem and changing password of database user worked for me. Follow these steps :

    1. Open MySQL Workbench

    2. Open Local instance MySQL57 using old password

    3. Go to Server > Users and Privileges

    4. Change password, and login to MySQL again. OR Create a newuser and set privileges. (If changing password do not work.)

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

    I have solved this by adding socket path to connection configuration. For more details you can see here

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

    You had to add the new user with an IP of the allowed host section not of the name of "localhost"

    // Do the mySQL Stuff
    var con = mysql.createConnection({
      host: 'localhost',
      user: 'user',
      password: 'mypwd',
      database: 'database',
      port: 3306,
      debug: true
    });
    
    //MYSQL Statement
    
    RENAME USER 'myuser'@'localhost' TO 'myuser'@'127.0.0.1';
    
    0 讨论(0)
  • 2020-12-13 13:48

    Try adding a port field:

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

    Try n make sure that you use the credentials that you use to login your database are correct

    const Sequelize = require('sequelize')
    const db = {}
    const sequelize = new Sequelize('ochiengsDatabase', 'ochienguser', ' 
    mydbpassword', {
    host: 'localhost',
    dialect: 'mysql',
    operatorsAliases: false,
    
    0 讨论(0)
  • 2020-12-13 13:48

    I was getting the same issue, but using require('mariadb'), which is essentially the same. So my answer should apply to both drivers.

    The problem was 2 fold:

    • host: 'localhost', user: 'user' is always resolving as 'user'@'127.0.0.1' on the database. So don't use localhost but 127.0.0.1 instead!

    • The password encryption scheme was incompatible between client and server, apparently the Node client is using mysql_native_password.

    Here's the solution: (from the mysql command-line client)

    # If you don't have a 127.0.0.1 equivalent user:
    CREATE USER 'root'@'127.0.0.1' IDENTIFIED WITH mysql_native_password BY 'password';
    
    # If you already have the user, reset its password:         
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
    
    # Redo your grants on the 127.0.0.1 user:
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'127.0.0.1';
    FLUSH PRIVILEGES;
    

    Voilà, now connect from Node and everything works:

    const mariadb = require('mariadb'); // or require('mysql');
    
    const pool = mariadb.createPool({
        host: 'localhost',  // or host: '127.0.0.1'
        user: 'root',
        password: 'password',
        database: 'mydatabase',  // don't forget the database
        port: 3306,
        connectionLimit: 5
    });
    

    References:

    Here's where I got the solution from, read for more info

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