I have setup a MySQL database. When I try to access it through my Node JS server, I am getting the error
\"ER_NOT_SUPPORTED_AUTH_MODE: Client does no
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.
For now 8.0 version is way to change plugin : Use this example:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
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.
Do Reconfigure Authentication Method,there you select legacy type password support for 5.1
Reason for the error
*You Installed latest Mysql 8.0 ,it has deferent authentication method. degrading 5.6 or 5.1 is simple solution
The top rated answers in this Q/A thread are for the most part accurate/valid, but unorganized to say the least. A soluion is here, however; the solutions is pieces and bits found amongest three answers. To offer an answer and solution that exist in a single post, is easier to read, inturpret, and all around more helpfull and a time saver, I will make an attempt to answer the question with a clear, concise and orderly set of instructions that covers the whole Promblem that Ubuntu users are experiancing. In addition I will add information neccassary, not included in other answers, for understanding the issue being solved. Like that the problem is not a SQL problem it is an Ubuntu. To bring clarity to readers, of what the problem in presistence is; 'ROOT' user doesn't have a password in Ubuntu. Ubuntu users use the sudo command, and organize authority through permission of use of the sudo cmd for pretty much everything, there are exceptions and there is a Ubuntu Root User option, but it doesn't help to discuss those things right here, right now. The important thing to note is the following:
Ubuntu lacks a 'ROOT PASSWORD' and this is why everyone experiancing the issue that we are discussing runs a Distrabution of the Ubuntu OS/SHELL. We cannot give Ubuntu SHELL a 'ROOT PASSWORD', however, we can, and we are, going to give a 'ROOT PWD' to our 'MYSQL-COMMUNITY-SERVER'.
Firstly you need NPM, NodeJS, MySql, and NPM's MySql Driver. If you don't already have them you shouldn't even be reading this, becuase your cant even know 100% if the issue you are solving is this...
...If you do have everything on the list, and are running a Ubuntu dist., and are getting error authentication messages and Connection err messages that look like the following
ERROR: (28000): Access denied for user 'ajc'@'localhost'
Then you are likley in the right place.
To solve the problem start be creating an Empty 'dot JS' file for testing, call it 'sqltest.js' or whatever you like.
Add the CODE bellow to it.
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."
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.');
});
Now open a terminal window, and do your typical updates & upgrades, this is important, which is why every tut asks you to do them.
~$: sudo apt update
~$: sudo apt upgrade
let connection = mysql.createConnection({ host : 'localhost', user : 'root', password : '********', database : 'DB_App_00', });
Now type the following cmd
~$: sudo mysql -u root
It sould prompt you for your Ubuntu Password, type it and [ENTER].
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 commandline. Within the CMDL copy & paste this:
mysql> ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'ChoosePassword';
mysql> FLUSH PRIVILEGES;
The next part is obvious, change 'ChoosePassword' to a password you will remember while leaving the password within single quotation marks. Change absolutley nothing else, press [ENTER]
If you followed the steps correctly, you now have a MySQL 'ROOT USER' with its own password now. Test it by copy and pasting the following at the Ubuntu CMDL:
~$: mysql -u root -p
...you should be in, now exit.
mysql>exit
let connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '********',
database : 'DB_App_00',
});
now use node to run the node test file
~$: node testsql.js
If it doesn't say connected you did somthing 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.