node.js can't connect to mysql DB - mysql

I use CentOS on my server and node.js throws error
CONNECTION error: { [Error: ER_ACCESS_DENIED_ERROR: Access denied for user 'root'#'localhost' (using password: YES)]
code: 'ER_ACCESS_DENIED_ERROR',
errno: 1045,
sqlState: '28000',
fatal: true }
my local version on Ubuntu doesn't throw this error and php can connect to this DB with same credentials.
I use this code to connect to DB
var connect_params = require('./connect');
var express = require('express'),
app = express(),
mysql = require('mysql'),
connectionpool = mysql.createPool(connect_params.connection);
and my connect.js
var connection = {
host: 'localhost',
user: 'user',
password: 'password',
database: 'mydb'
};
exports.connection = connection;

Specifying the portnumber in my case worked, as I previously used hostname:portname (courtesy from the JDBC API) to connect with my local db.
So by specifying the portnumber as its own variable, instead of appending it on hostname, you might be able to connect to it.
Let me know if it doesn't work.
var connection = mysql.createConnection({
host : "hostname",
port : "portnumber",
user : "username",
password : "password",
database : "dbname"
});

Related

Using Environment Variables to initialise MySQL connection in NextJS leads to 'ER_NOT_SUPPORTED_AUTH_MODE' error

I am trying to fetch data from a MySQL table. It works perfectly when I hardcode the configuration. But when I use env variables to configure it, I get the 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
This is my .env.local file:
MYSQL_HOST: localhost
MYSQL_USER: root
MYSQL_PASSWORD:************
MYSQL_DB:***
this is my db connections code:
const mysql = require("mysql");
var db = mysql.createConnection({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DB,
});
db.connect((err) => {
if (err) {
console.log(err);
} else {
console.log("connected to db");
}
});
module.exports = db;
The issue is resolved. I used ':' instead of '='.

can't access to database in mySql workbench?

when i try to access workbench database i get this error
SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 60500
this is code
Future connectToDb() async {
// var rng = Random();
final conn = await MySqlConnection.connect(ConnectionSettings(
host: 'localhost',
port: 3306,
user: 'root',
db: 'sql_workbench',
password: '12345678a#',
),
).then((value) {
print("value");
});
}

Error connecting nodejs app to heroku clear DB

I have a nodeJS + mysql app that works fine on local, but when I try to connect it to an Heroku clear DB database I'm getting error. Database credentials are ok because I'm using them same on another web app.
This is my connection code:
var mysql = require('mysql2');
var cnx = mysql.createConnection({
host: 'us-cdbr-east-xx.cleardb.com',
user: 'bb69add9159975',
password: 'fb0xxxxx',
database: 'heroku_65f44b5a5fxxxx',
multipleStatements: true
});
cnx.connect(err => {
if(!err){
console.log(`Database connection successful`);
}else{
console.log(`Databse connection failed: ${err}`);
}
});
module.exports = cnx;
This is the error on console:
Databse connection failed: Error: Access denied for user 'bb69add9159975'#'ip-10-0-125-110.ec2.internal' (using password: YES)
Thanks.

MySQL error: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client

Could someone tell what error "ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server; consider upgrading MySQL client" means?
My code:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "user",
password: "password",
database: 'database'
});
con.connect(function(err) {
if (err) throw 'THE ERROR IS: ' + err;
console.log("Connected!");
});
In the link that I sent for you, was added this property: insecureAuth : true.
var con = mysql.createConnection({
host: "localhost",
user: "user",
password: "password",
database: 'database',
insecureAuth : true
});
I also faced the same issue but the error got resolved by using mysql2 instead of mysql.
so type
npm install mysql2
in your terminal.
and update your code with
const mysql = require("mysql2");

Nodejs mysql2 not throwing connection or query error

I am trying to capture connection error on nodejs while using mysql or mysql2 node module.
My code is
console.log("Connecting to database")
try{
var connection = mysql.createConnection({
host : process.env.DB_HOST,
user : process.env.DB_USERNAME,
password : process.env.DB_PASSWORD,
database : process.env.DB_NAME
});
connection.connect();
console.log("Connected")
}
catch(exce)
{
console.log(exce)
}
The mysql server is not running and all details are wrong here. I am expecting this code to throw a connection error but this does not happen. Instead, I am receiving the following console output.
Connecting to database
Connected
I don`t know why there is no connection error thrown by mysql module.
The following solution suggested by #Anatoly worked for me.
var mysql=require("mysql2/promise")
var connected=true
console.log("Connecting to database")
try{
var connection =await mysql.createConnection({
host : process.env.DB_HOST,
user : process.env.DB_USERNAME,
password : process.env.DB_PASSWORD,
database : process.env.DB_NAME
});
// connection.connect();
console.log("Connected")
}
catch(exce)
{
console.log(exce)
connected=false
}
```