Nodejs mysql2 not throwing connection or query error - mysql

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
}
```

Related

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");

Node.js Mysql: connection failed

I'm trying to connect node.js to mysql and it's failed, I already install mysql. Can anyone help me ?
const express = require("express");
const mysql = require('mysql');
var app = express();
var mysqlConnection = mysql.createConnection({
host: "localhost",
user: "root",
password: "******",
database: "otablo",
});
mysqlConnection.connect((err)=> {
if(!err)
{
console.log("Connected");
}
else
{
console.log("Connection Failed");
}
})
app.listen(3000);
const mysql = require('mysql');
var app = express();
var mysqlConnection = mysql.createConnection({
host: "localhost",
user: "root",
password: "****",
database: "****",
});
mysqlConnection.connect((err)=> {
if(!err)
{
console.log("Connected");
}
else
{
console.log("Connection Failed");
}
})
app.listen(3000);
Your code is working fine, make sure you have everything installed and the values for the database are right. It worked for me.
I found the problem, I get the error message: ER_NOT_SUPPORTED_AUTH_MODE: Client does not support auth
entication protocol requested by server; consider upgrading MyS
QL client
The solution:
ALTER USER 'root'#'localhost' IDENTIFIED WITH mysql_native_password BY 'password'
I had the same issue. The problem was in password. I tried to use same password that I saw in the tutorial, instead of using my own password that I took for my database during mysql installation process.

Cannot connect to mysql using Node js

I am trying to connect to mysql but I am getting the following error :
"Error: ER_ACCESS_DENIED_ERROR: Access denied for user ' root'#'localhost' (using password: YES)"
But when I connect using mysql command line client I am able to connect to mysql.
Here is my config.js file where I have defined the connectivity settings :
//config.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
port : '3306',
user : ' root',
password : 'root',
database : 'world'
});
module.exports = connection;
I am trying to connect to the db in this file :
// userController.js
var jwt = require('jwt-simple');
var connection = require("../config.js");
var auth = {
getAll: function(req, res) {
console.log("in user comtroller");
console.log(connection);
connection.connect(function(err) {
// connected! (unless `err` is set)
if(err){
console.log("Failed to connect to mysql. Error : "+err);
}
else{
console.log("connection Successful..!!");
}
});
res.send("going good");
}
}
module.exports = auth;

node.js can't connect to mysql DB

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"
});