Node JS and mysql not connecting (client does not support authentication protocol requested by server consider upgrading mysql client)

后端 未结 4 1324
再見小時候
再見小時候 2021-01-19 08:09

I\'m trying to connect my NodeJs application with a mysql database but its not connecting for some reason.

I did install the mysql npm like this (typed this in comma

相关标签:
4条回答
  • 2021-01-19 08:18

    Use the new mysql2 library instead of mysql in order to use the new authentication method of MySQL 8.0

    ~ Tested in node version 13.6.0 and MySQL 8.0.21

    0 讨论(0)
  • 2021-01-19 08:30

    Instead of

    mysqlConnection.connect((err) => {
    

    use

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

    [Here the is tutorials reference for the code]1

    For your mysql error:

    Execute the following query in MYSQL Workbench:

    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
    

    Try connecting using node after you do so.

    0 讨论(0)
  • 2021-01-19 08:43

    I was having the same problem with mysql library and I fixed that error by changing to mysql2 Library I recommend you to install mysql2

    Run

    npm install mysql2
    

    Then

    import mysql from "mysql2";
    

    OR

    const mysql = require("mysql2")
    

    I hope this will work for you!! Good Luck

    0 讨论(0)
  • 2021-01-19 08:44
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'YourRootPassword';
    -- or
    CREATE USER 'foo'@'%' IDENTIFIED WITH mysql_native_password BY 'bar';
    -- then
    FLUSH PRIVILEGES;
    
    0 讨论(0)
提交回复
热议问题