Error connecting nodejs app to heroku clear DB - mysql

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.

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 '='.

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

Error: ER_ACCESS_DENIED_ERROR: Access denied for user. Unable to Connect to Remote Database using Node JS

I have been trying to connect to remote database using NodeJS where I have also added a user and given all the permission.
I am getting this Error:
ER_ACCESS_DENIED_ERROR: Access denied for user.
Please help.
This is the code
const mySql = require("mysql");
//creating DataBase connection
const con = mySql.createConnection({
host: process.env.SQL_HOST,
user: process.env.SQL_USER,
password: process.env.SQL_PASSWORD,
database: process.env.SQL_DATABASE
});
//connect to DataBase
con.connect(function(err)
{
if(err)
{
console.log("Error Connecting to Database: "+err);
}
else
{
console.log("Connected to Database: Successful");
}
});
I Found the answer, Add to remote Access mySql in your Cpanel your local IP so that you can access it. Thanks

Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433

I'm trying to connect to my local MySql server from node.js/express application using mssql with following config:
// Database connection
var sql = require('mssql');
var config = {
user: 'db_user',
password: 'db_pass',
server: 'localhost',
database: 'project_db'
};
sql.connect(config, function (err) {
if (err) {
console.log(err);
}
});
I get this error:
{ [ConnectionError: Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433]
name: 'ConnectionError',
message: 'Failed to connect to localhost:1433 - connect ECONNREFUSED 127.0.0.1:1433',
code: 'ESOCKET' }
I know I have to enable TCP/IP connections for my local MySql server, but I can not find how to do it on OSX El Capitan. There is nothing like control panel with settings. That's all available at System Preferences > MySql:
Any ideas how to fix this, guys? Thanks for trying to help.
mssql and mysql are two very different things... You should use the node-mysql or some other mysql client library if you are going to be running mysql on your dev machine
https://github.com/felixge/node-mysql
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'me',
password : 'secret',
database : 'my_db'
});
connection.connect();
connection.query('SELECT 1 + 1 AS solution', function(err, rows, fields) {
if (err) throw err;
console.log('The solution is: ', rows[0].solution);
});
connection.end();

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