ER_NOT_SUPPORTED_AUTH_MODE - MySQL server
Asked Answered
M

14

86

Failed at Connecting Node.js Server to MySQL-Database


        I had MariaDB" installed on a "Node.js Server", but decided that I wanted to use a SQL Database instead. I uninstalled, and completely removed "MariaDB", after which, I proceeded to install the "Community Ed." "MySQL Database". After going through the entire *'MySQL Setup Process'**, I made several attempts to connect to the database via a JavaScript document that implemented the de facto code snippet for a JS DB Connection — my DB-connection document is posted as a code snippet bellow — shown in the code snippet bellow. Disappointingly, the JS/SQL connection failed at each attempt.



Here is the Failed Connection Error Message that I received:



"ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication
protocol requested by server. Consider upgrading MariaDB client."



JS/SQL Connection Snippet that I am using:



    var mysql = require('mysql');
    
    var connection = mysql.createConnection({
        host     : 'localhost',
        user     : 'root',
        password : '********',
        database : 'foobarDb'
    }); 

Mollescent answered 6/7, 2017 at 10:23 Comment(3)
Downgrading MYSQL ver 8.0.13 to ver 5.7.24.0 solved it for me.Cushman
similar to question #50093644Monophagous
detailed explanation for this error when using mysqljs/mysql, see post https://mcmap.net/q/53951/-mysql-8-0-client-does-not-support-authentication-protocol-requested-by-server-consider-upgrading-mysql-clientMonophagous
M
7

I figure that some MySQL versions have the authentication for the establishment of a connection a bit messed up. All I had to do was add the line "insecureAuth" : true to the CreateConnection(...) credentials.

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '********',
  database : 'vod_bill_database',
  insecureAuth : true
}); 

The error is gone now, and the client is successfully connecting.

Mollescent answered 6/7, 2017 at 14:19 Comment(2)
Doesn't work with mysql 8.0 on ubuntu, because it's (at this comment's date) still unsupported by mysql.js.Goldman
DO NOT USE THIS CONFIGURATION!!! This method is to maintain compatibility for versions that require for the old, and very insecure, method of authentication. Where this hack may, or may not work, it is an INSECURE method to authenticate, and should be avoided whenever possible. This should be blatantly obvious considering the name. Here is the link to the API, and connection options if you want to see for your self. npmjs.com/package/mysql#connection-optionsHoggish
R
144

For MySQL v8.0 use the following:

ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
Retaretable answered 26/5, 2018 at 21:23 Comment(5)
seems to reset the schema level credentials for your database.Waverly
should disable skip-grant-tables firstFrenchman
This answer doesn't quite make sense because it was taken from the context of this post: #50093644. I recommend just looking at it instead of this post.Ir
I see this post for the first time, do something practical ...Retaretable
We should replace 'root' with our username and 'password' with the password we want to assign. Once done please FLUSH PRIVILEGES; in order to activate it.Encompass
G
52

You can use the package mysql2 instead of mysql. I ran into the same issue and using mysql2 worked for me.

You can install this package using npm i mysql2

Gazo answered 10/3, 2021 at 8:51 Comment(4)
totally perfect solution - thxLeonoreleonsis
This is the right answer! Changing the database server is a workaround not solution. MySQL 8 uses new authentication protocol. mysql2 supports the new protocol but mysqljs/mysql does not.Tiebold
This worked like magic !! Thank you Just installed mysql2 and only changed this import mysql=require("mysql2")Sheeree
such an underrated answer, works like a charmHotchpotch
B
33

enter image description here

You need to reconfigure the Quick Action Settings by clicking the "Reconfigure Link" as shown in the screenshot below. From there, select "Legacy password" for v5.1.


The cause of the error:


You installed the latest "MySQL Version", v8.0. The latest version has a different encryption plugin for authenticating users at login. 5.6, and 5.1 revert to the prior encryption algorithms. Please note, 5.6 & 5.1 have several security vulnerabilities reported by oracle.

Bengurion answered 9/10, 2018 at 17:35 Comment(2)
I was using the terminal to run and manage my database, so this was not an option. Thanks though ;)Padus
this worked for me, but only after I created a new user and connection in the MySql workbench. I cannot get it working with an existing connection. But good enough for now. Thanks for the tip.Enzymology
S
25

The top-rated answers in this Q/A thread are for the most part valid, but they are unorganized, which is to say the least. A solution is here, however, the solution is bits and pieces amongst three other answers. To offer an answer that is a single solution, more helpfull, and is a time saver, I'll make an attempt to write an answer myself in a way that is clear, concise, and orderly. I will cover the whole problem that Ubuntu users experiance, and in addition, I will add information that's helpfull, and not included in any other answer, that will help readers understand the issue that persist for them.


To Start: The Issue is not a SQL Problem, it is an Ubuntu Problem

The issue that persist for you, has to do with the fact (a fact most software developers/I.T. professionals are probably all already aware of) the 'ROOT' user doesn't have a password in Ubuntu, and is accessible by anyone with $ sudo privileges. To offer clarity for anyone experiencing this issue who might be new to some of the semantics that I am throwing out there; Ubuntu users use the sudo -i command to register as the Root-user, whereas, every other Linux distribution in existence uses a User-ID w/ a Password. In truth, I cannot remember ever needing to be a ROOT user for anything other than Database Management, and always only when I am first installing a Database to a server, though my experience is probably far limited in comparison to some IT professionals out there. My point is, typically using sudo for everything does the Job, but in this case it is problematic, so the important thing to note is the following:


PROBLEM:


Ubuntu lacks a 'ROOT PASSWORD' and this is why everyone experiencing the issue that we are discussing runs a Distribution of the Ubuntu OS/SHELL. And unless we rewrite the Ubuntu kernel, and the practically everything else in the operating system, we cannot give Ubuntu SHELL a "root password".


SOLUTION:

We may not be able to give the Ubuntu SHELL a root password, but we can, and we will, give MySQL a 'ROOT PASSWORD'.





TO EXECUTE THE SOLUTION YOU NEED TO HAVE THE FOLLOWING:

  1. Node.js v12+
  2. NPM (Probably need v5+ but don't quote me on that)
  3. MySQL v8.0+ (obviously)
  4. The MySQL Driver (from npm)



CONFIRMATION:

If you don't already have everything on the list you honestly can't say that this is the issue your dealing with.
If you do have everything on the list

and you are running an Ubuntu distro, then you should be getting an error message that probably looks somthing like the one I got when I had to fix this issue.

My error message read:
ERROR: (28000): Access denied for user 'ajc'@'localhost' 
ERROR: ERROR_NOT_SUPPORTED_AUTH_MODE: Client does not support
authentication protocol requested by server; consider upgrading 
MySQL client

'Client does not support authentication protocol requested by server; consider upgrading MySQL client

If your still reading then your likely in the right place.
  1. To start fixing the problem create an empty Node.js project, and install the MySQL driver as a dependency to it using NPM (you should know how to do this, as you had to do that to have this issue). Add a JavaScript .js file. Call the file, sqltest.js or whatever something like that.

  2. Add the code below to the file you just created.

let mysql = require('mysql');

let connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '********',
    database : 'DB_App_00',
});

connection.connect(function(err) {
    if (err) {
      return console.error('error: ' + err.message);
    }
  
    console.log('Connected to the MySQL server.');
  });

  1. In the method called 'createConnection' is a JSON OBJ parameter holding the credential values to make a valid connection to the MySQL database server. The user has to equal to 'root', and the database has to exist. Also for a later test add a testing table to the database, with some BS data."

  2. Now open a terminal window, and do your typical updates & upgrades, this is important, which is why every tutorial asks you to do them.

~$: sudo apt update
~$: sudo apt upgrade


  1. After you do your upgrades enter the following command into your terminal:
~$: sudo mysql -u root

  1. It should prompt you for your Ubuntu Password, type it and [ENTER].

The next step is critically important:
  1. Now here is the step that could be considered the medicine and/or the cure to the problem. Your terminal should be open, and you should be inside of the MYSQL Server, under the user 'root'. The terminal should have the cursor flashing at a blank mysql command-line. Within the CMDL copy & paste this:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'ChoosePassword';

mysql> FLUSH PRIVILEGES;

  1. The next part is obvious, change 'ChoosePassword' to a password you will remember while leaving the password within single quotation marks. Change absolutely nothing else, press [ENTER]

  2. If you followed the steps correctly, you now have a MySQL 'ROOT USER' with its own password now. Test it by copy and paste the following at the Ubuntu CMDL:

 ~$: mysql -u root -p 
  • It will prompt you for your new password, type it and [ENTER]

...you should be in, now exit.

mysql>exit
  1. Back to your 'testsql.js' file, alter the credentials to root for the user, password to your password, a valid database, and host to localhost, unless you have a unique need for a different hostname.

let connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : '********',
    database : 'DB_App_00',
});


  1. now use node to run the node test file
~$: node testsql.js

Final Thought:

If it doesn't say connected you did something wrong, but if all went well, you should connect. It took some effort before I got it to work, but this answer should save you some time from reading all the other half written answers.

Sneed answered 28/5, 2020 at 3:16 Comment(2)
Is it still/really an UBUNTU problem if I am getting it on MacOS?Lcm
@IanMbae I should have been more clear. This issue can pop-up for others, but typically it is easily resolved by logging into the "ROOT USER", however, Ubuntu doesn't ship with an accessible Root user account. I believe MacOS has a root user though. Now, I am far from a unix specialist, especially when it comes to mac. I guess I would have a couple questions at this point, which would be... Are you able to login to ROOT on MacOS, and if so can you fix the problem as the root user? If you can't login to root, or fix the issue as root, then the answer needs to be edited.Hoggish
T
10

You can either alter an existing user to use mysql_native_password, or create a new user,

CREATE USER 'new_user'@'%' IDENTIFIED WITH mysql_native_password BY '***';
GRANT USAGE ON *.* TO 'new_user'@'%';
ALTER USER 'new_user'@'%' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
GRANT ALL PRIVILEGES ON `new_user`.* TO 'new_user'@'%';
FLUSH PRIVILEGES;

replace new_user with your new user name, and set your password.

Now you can access mysql from node using the mysql package,

 npm install mysql

recommended to use pool connection for this package.

Tyrant answered 13/5, 2020 at 14:47 Comment(0)
M
7

I figure that some MySQL versions have the authentication for the establishment of a connection a bit messed up. All I had to do was add the line "insecureAuth" : true to the CreateConnection(...) credentials.

var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '********',
  database : 'vod_bill_database',
  insecureAuth : true
}); 

The error is gone now, and the client is successfully connecting.

Mollescent answered 6/7, 2017 at 14:19 Comment(2)
Doesn't work with mysql 8.0 on ubuntu, because it's (at this comment's date) still unsupported by mysql.js.Goldman
DO NOT USE THIS CONFIGURATION!!! This method is to maintain compatibility for versions that require for the old, and very insecure, method of authentication. Where this hack may, or may not work, it is an INSECURE method to authenticate, and should be avoided whenever possible. This should be blatantly obvious considering the name. Here is the link to the API, and connection options if you want to see for your self. npmjs.com/package/mysql#connection-optionsHoggish
C
6

1st run this code ->

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

2nd

flush privileges;
Contestation answered 12/5, 2020 at 16:14 Comment(0)
I
0

try the older version of mysql like 5.6.40, it uses by default SHA256_password auth where new version 8.0. uses by default sha2_password auth which is more secure and throw this authentication protocol error. MYSQL installer 5.6.40

Innate answered 5/6, 2018 at 13:19 Comment(0)
S
0

yellow yow bros !

// mysql.ts
const pool = mysql.createPool({
    connectionLimit: 10,
    host: "localhost",
    user: "root",
    password: "password",
    database: "rest-resume-api",
});

and then I have a docker-compose file such as

# docker-compose.yml

version: '3.3'

services:
  db:
    image: mysql
    restart: always
    command: --default-authentication-plugin=mysql_native_password
    environment:
      MYSQL_DATABASE: 'rest-resume-api'
      MYSQL_USER: 'root'
      MYSQL_ROOT_PASSWORD: 'password'
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - my-db:/var/lib/mysql2
volumes:
  my-db:

Sofia answered 19/12, 2019 at 14:9 Comment(2)
It did help me, i appreciate it!Swum
If anyone using docker. This is correct. Worked for meRunnels
E
0

you can do it

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

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

FLUSH PRIVILEGES;

check mysql root user IDENTIFIED

select user,host,plugin from user;

+------------------+-----------+-----------------------+
| user             | host      | plugin                |
+------------------+-----------+-----------------------+
| root             | %         | mysql_native_password |
| mysql.infoschema | localhost | caching_sha2_password |
| mysql.session    | localhost | caching_sha2_password |
| mysql.sys        | localhost | caching_sha2_password |
| root             | localhost | mysql_native_password |
+------------------+-----------+-----------------------+

Endometriosis answered 20/10, 2022 at 3:6 Comment(0)
N
0

If you have phpMyAdmin in the type of password for said user, select native mysql autentication

It did the trick for me

enter image description here

Newel answered 30/5, 2024 at 17:38 Comment(0)
D
-1
var connection = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '********',
  database : 'vod_bill_database',
  port : 3308 
}); 

I had the same error and since i changed my port in phpmyadmin from 3306 to 3308 therefore here also i had to write port: 3308 and it started working.

Danitadaniyal answered 9/6, 2018 at 20:42 Comment(0)
G
-1

in order to overcome this error use the following code:

var connectionString = 'mysql://*root:*password@*localhost/*database?charset=utf8_general_ci&timezone=-0700'; 
var connection= mysql.createConnection(connectionString); 

but make sure that you changed the * marks in the connectionString based on your setup.

Germin answered 21/1, 2019 at 20:49 Comment(0)
D
-2

just create a new user on MySQL

CREATE USER 'foo'@'localhost' IDENTIFIED WITH mysql_native_password BY 'bar';
Digressive answered 14/7, 2020 at 15:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.