Cannot log in to mariadb from node.js app - mysql

I can log in with the same credentials from Windows Command Line and from MariaDB admin.
But trying to log in from node.js app fails...
const mariadb = require('mariadb');
const pool = mariadb.createPool({
host: 'localhost',
user:'nikol',
password: 'nbuser',
database: 'nodedb'
});
I get that error:
SqlError: Error during pool initialization: (conn=197, no: 1045, SQLState: 28000) Access denied for user 'nikol'#'localhost' (using password: YES)
I can also log in as root user from command line, but not from node js app.
Any suggestions?

Resolved. When installing MariaDB, I changed port number from 3306 to 3300 and that caused the problem. Now I have reinstalled MariaDB and everything works ok.

Related

errno: 1251, sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client

I'm creating the simple api project in nodejs.
For that I have installed mySql workbench with version 8.0.11.
I have created config file, dbConnection.js file and in the dbconnection.js file I have written the following code :
var mysql = require('mysql');
var path = require('path');
var config = require(path.resolve('./', 'config'))
var connection = mysql.createConnection({
host: config.databaseHost,
user: config.databaseUser,
password: config.databasePassword,
database: config.databaseDatabaseName,
multipleStatements: true
});
connection.connect(function (err) {
if (err) {
console.log('Error connecting to Database',err);
return;
}
console.log('Connection established');
});
module.exports = connection;
And I'm facing following error :
code: 'ER_NOT_SUPPORTED_AUTH_MODE',
errno: 1251,
sqlMessage: 'Client does not support authentication protocol requested by server; consider upgrading MySQL client',
sqlState: '08004',
fatal: true
Note : I kept my congif as simple as it can be, user = root , password = root, host = localhost.
once you establish your host, root, password &, etc inside your MySQL connection function.. paste the following inside of your database you are trying to connect within workbench with the following:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'your_current_password';
Go back to the terminal and run node <file name.js> again
Open terminal and login:
mysql -u root -p
(then type your password)
After that:
USE mysql;
UPDATE user SET authentication_string=password(''), plugin='mysql_native_password' WHERE user='root';
And the last important thing is to reset mysql service:
sudo service mysql restart
Install mysql2 instead of mysql
npm install mysql2
Then add this to index.js
const mysql = require('mysql2')
You have to reconfigure MySQL Server. for that follow these steps.
1) Open mysql intsaller.
2) Click on Reconfigure (to the left mysql server)
3) Go to Authentication method tab (by clicking on Next twice.)
4) Then select the radio button Use Legacy Authentication Method. Click Next.
5) Enter your password. Click on Check. Go to Apply Configuration (by clicking on Next twice).
6) Click on Execute. Then Finish.
That's it. Enjoy Hacking!!
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'current password';
Was getting ERROR 1251 in node v17 with mysql
❌ // didn't worked: errno 1251 ER_NOT_SUPPORTED_AUTH_MODE
import mysql from 'mysql';
Try to remove mysql from node and replace it for mysql2
yarn remove 'mysql'
yarn add 'mysql2'
✅ import mysql2 from 'mysql2';
This fixed the issue for me!
I also had the same problem. It did not work for the root user. I created a new user (using mySQL workbench) and gave privileges to the new user. Then I tried with the new user and it worked fine. see workbench settings
Try to remove mysql
yarn remove mysql
yarn add mysql2
import mysql2 from "mysql2"
This fixed issue for me.
code
:
"ER_NOT_SUPPORTED_AUTH_MODE"
errno
:
1251
fatal
:
true
sqlMessage
:
"Client does not support authentication protocol requested by server; consider upgrading MySQL client"
sqlState
:
"08004"
Try this with your password and user name root if your password was not root try with your password.
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'root';

A hook (`orm`) failed to load! sailsjs

I have been trying since yesterday and somehow it's not working.
i dont know where am I going wrong with this.
here's the code in connections.js
module.exports.connections = {
mysql: {
adapter: 'sails-mysql',
host: 'localhost',
user: 'root', //optional
password: 'root', //optional
database: 'mysql' //optional
}
}
and here's the code in models.js
module.exports.models = {
connection:'mysql',
migrate:'alter'
}
could you guys tell me what's wrong here? and what's the reason for this error?
Here's the detailed error
error: A hook (`orm`) failed to load!
error: Error (E_UNKNOWN) :: Encountered an unexpected error
: Could not connect to MySQL:
Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'#'localhost' (using password: YES)
at afterwards (/Users/faraz/code-playground/sailsapi/node_modules/sails-mysql/lib/connections/spawn.js:72:13)
at /Users/faraz/code-playground/sailsapi/node_modules/sails-mysql/lib/connections/spawn.js:40:7
at Handshake.onConnect [as _callback] (/Users/faraz/code-playground/sailsapi/node_modules/sails-mysql/node_modules/mysql/lib/Pool.js:54:9)
just wanted to show you the main page when I start mamp.
i think there's something wrong there
because it actually opens on http://localhost:8888/MAMP/?language=English but config shows port as 3306
could that be the problem??
here's a screenshot
I see that your configuration in sails is OK, so the reason why you cannot connect to the MySQL server is elsewhere. Here are some steps you may want to try:
Use phpMyAdmin to make sure you can connect to your server, but my guess is you will not be able to do that. If you do however, create a user for your DB there and grant all privileges for the DB to that user, and for host try to use "any host". Then adjust your connection according to that user.
In order to check your connection for the new user, the most convenient way is to use the MySQL Workbench tool from here: https://dev.mysql.com/downloads/workbench/
If all of that doesn't help, check this answer: Can't connect to MySQL server on 'localhost' (10061)

Can't access MySQL from Glassfish pool

I have a connection pool created by following a tutorial. Also there is MySQL created from XAMPP, running on port 3036.
Connection pool attributes are:
user admin
password admin
URL jdbc:mysql://localhost:3036/test2
In my.ini of MySQL I added
[client]
user=admin
database=test2
password = admin
port = 3036
But when I try to ping, I catch an error:
Ping Connection Pool failed for Test2. The connection could not be
allocated: Access denied for user 'admin'#'localhost' (using password:
YES)
What's wrong with settings?
Run your mssql shell as root and try to execute:
GRANT ALL ON test2.* TO 'admin'#'localhost';
maybe this would help.

node.js mysql error: ECONNREFUSED

Why can't I connect to the mysql server?
On the same server an Apache/PHP server is running and it connects without problems!?
var mysql_link = {
host : 'localhost',
port : 3308,
database: 'nodetest',
user : 'root',
password : 'xxx'
};
var connection = mysql.createConnection(mysql_link);
connection.connect(function(err){
console.log(err);
if(err != null){
response.write('Error connecting to mysql:' + err+'\n');
}
});
connection.end();
error
{ [Error: connect ECONNREFUSED]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
fatal: true }
update
root#dyntest-amd-6000-8gb /var/www/node/dyntest # ps ax | grep mysqld
7928 pts/0 S+ 0:00 grep mysqld
28942 ? S 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/var/lib/mysql --pid-file=/var/run/mysqld/mysqld.pid
29800 ? Sl 17:31 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/var/lib/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/var/lib/mysql/mysql-error.log --open-files-limit=65535 --pid-file=/var/run/mysqld/mysqld.pid --socket=/var/run/mysqld/mysqld.sock --port=3306
I know this question has been answered, but for me the problem was that the mysql server listens on a Unix socket not on a tcp socket. So the solution was to add:
port: '/var/run/mysqld/mysqld.sock'
to the connection options.
If this has worked before, my first guess would be that you've already got a copy of your node.js script running in the background which is holding the connection.
I believe connection refused is a tcp/ip error message, rather than something from MySQL which suggests that it is either not running or is running on another port or with sockets.
Could you try telnet'ing to port 3308? To see if the server is running on that port?
telnet localhost 3308
Can you also try:
mysql -hlocalhost -uroot -pxxx
Overview
For anyone else having this problem and is running mamp. I suspected the problem had to do with the network and not MySQL or Node.js.
Solution
If you open MAMP and click MySQL in the left navigation panel it will pull up the MySQL options page. In the center of the page you will see a checkbox that says,
"Allow network access to MySQL".
Check this box and then restart your MAMP. At this point you can now test your connection to MySQL with telnet or a node.js script.
Hint
Remember you can check which port your MySQL is running on by opening MAMP and clicking the ports link on the left navigation panel.
Visual Aid
For some very odd reason, my computer only allowed me to have port 3306 as default port for my connection in order for it to work.
Mac OS on M1 MacBook Pro here with MySQL installation via brew:
Changing the host from 'localhost' to '127.0.0.1' and creating a different user with a password solved this issue for me.
For some reason I cannot connect to my DB with root user and no password.
I created the new user on MySQL Workbench, but you can create a new user with admin privileges via the mysql CLI also.
Just google it.
This is how my backend looks:
const express = require('express');
const mysql = require('mysql');
const app = express();
const port = 3500;
const db = mysql.createConnection({
host: '127.0.0.1',
user: 'admin',
password: 'admin',
});
db.connect((err) => {
if (err) throw err;
console.log('connected to database');
});
app.listen(port, () => {
console.log('server listening on port 3500');
});
I wanted to comment my solution here, just in case there were people as newbie as me in databases.
I was getting this error because I had installed the mysql NPM package correctly but I hadn't installed any implementation of MySQL on my computer (I didn't know I had to).
I'm using Arch Linux so, in my case, with the NPM package already installed in my project, I did pacman -Syu mariadb (MariaDB is the default implementation of MySQL in Arch Linux) and then configured it following the guide.
Then, you can use the root user you just configured or create a new one to use in your project. For the latter:
Enter mysql CLI by running mysql -u root -p.
Enter the password for root user.
Create a new database with CREATE DATABASE mydatabase;.
Create a new user with CREATE USER test IDENTIFIED BY "testpass";.
Grant privileges to test user to use your new database with GRANT ALL PRIVILEGES ON mydatabase.* TO test#localhost IDENTIFIED BY "testpass";. See for more information on this.
And then, in my project, I would have:
let connection = mysql.createConnection({
host: "localhost",
user: "test",
password: "testpass",
database: "mydatabase"
});
If you are using MAMP please note that mysql default db_port is set to 8889 so you for this purpose I had to change it to 3306 (or whatever your app mysql db_port is set to).
My issue was that node server was connecting using port 3306, so it was giving the error below then crashing but mysql was up and seemed to establishing a connection through localhost, although I couldnt test it because node server was down.
errno: -61,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 3306,
fatal: true
Once I changed the port on MAMP mysql from 8889 to 3306, node server established connection through port 3000 and giving the statement below:
server running on port 3000
The solution is: 2
I use Windows 10 and I have Windows Subsystem For Linux (WSFL). I can execute my project from the WSFL console, but my MySQL is installed in Windows and they can not connect, however when I execute my project from a Windows console then it works without any problems.

Heroku db:push Sequel::DatabaseConnectionError -> Mysql::Error: Access denied for user 'reg'#'localhost ' (using password: NO)

I have a problem using the heroku db:push command to transfer a MySQL database to heroku. I've tried using the same command for another app with a sqlite3 database and everything went fine.
C:\Users\reg\Team-Omni>heroku db:push
Loaded Taps v0.3.9
Auto-detected local database: mysql://127.0.0.1/omni_dev?encoding=utf8
Warning: Data in the app 'growing-mist-42' will be overwritten and will not be recoverable
.
Are you sure you wish to continue? (y/n)? y
Failed to connect to database:
Sequel::DatabaseConnectionError -> Mysql::Error: Access denied for user 'reg'#'localhost
' (using password: NO)
if i remove the password for the mysql anonymous account, the error changes to
Sequel::DatabaseConnectionError -> Mysql::Error: Access denied for user ''#'localhost' to database 'omni_dev'
--additional information--
my .gems file:
rails -v 2.3.8
andre-geokit --version 1.5.0 --source http://gems.github.com
You are getting that error because you are not specifying the password for the local database connection. Apparently the username is configured as 'reg' (maybe in the MySQL config file). This doesn't have anything to do with the Heroku PostgreSQL database, it's a problem connecting to your local MySQL database (named omni_dev).
Try the following connection string:
mysql://USERNAME:PASSWORD#127.0.0.1/omni_dev?encoding=utf8
for the appropriate values of USERNAME and PASSWORD.
I've had this problem as well, but I was passing a password. My local app worked fine, but when using heroku db:push, I got the same error as redge.
Adding a port in the database.yml fixed the problem. So, a working database.yml has the following entries:
database: DATABASE
username: USER
password: PASSWORD
adapter: mysql
encoding: utf8
socket: /Applications/MAMP/tmp/mysql/mysql.sock
port: 8889