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

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

    If you are connecting to an external server from localhost, you may need to add your gateway router address to MySQL's "Allowable Hosts" box.

    This address can be found after the @ sign in the error message:

    Access denied for user 'your_username'@'blah.blah.blah.yourisp.com'
    
    0 讨论(0)
  • 2020-12-13 13:50

    Using recent MySQL version in package.json solved the problem.

    I was using version 2.0.0. I changed the version to 2.10.2.

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

    Most of the time this things happen due to the misconfigurations in mySQL on your device . I had this problem myself . I have solved the problem using the link below .

    ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'db'

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

    I had a similar problem. I was running mysql in a Docker container and had the same error when trying to connect to it from my node app.

    It appeared, that I had run the Docker container without exposing the port, hence it was 3306 inside the container, but would not have been accessible through localhost:3306. Why I got ER_ACCESS_DENIED_ERROR error was because I actually had some other mysql server running on the port 3306, with different username and password.

    To see if or what you have running on the specific port type:

    ps axu | grep 3306
    

    Since I already had something on port 3306, to make the server accessible to my app I changed a port to 3307 and run my docker mysql container with the command:

    docker run --name=<name> -e MYSQL_ROOT_PASSWORD=<password> -p 3307:3306 -d mysql
    

    After starting mysql client inside Docker with command:

    docker exec -it <name> mysql -u root -p
    

    And after creating a database to connect to, I was able to connect to my mysql db from my node app with these lines:

     const connection = mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'password',
        database: 'database',
        port: 3307
     });
    
     connection.connect();
    

    Hopefully helps someone new to docker and mysql :)

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

    const pool = mysql.createPool({ host: 'localhost', user: 'root', database: 'database_name', password: 'your_pwd' });

    make sure you have spelled the the keys and the properly. I was facing similar issue, then realised that I had written username instead of user

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

    A bit late to talk about it but I guess I found the problem: special chars in password!!! I had a $ in pass. Solution: use escape Ex: te\$t

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