Sequelize Bad Handshake error - mysql

I have a new azure MySQL server and I am trying to use that with my existing Nodejs app.
When I try to connect to the Mysql instance from my terminal(using mysql command), I am able to successfully connect.
However, We I try to connect through my Nodejs app, which is using sequentialize for ORM, I am unable to connect.
It shows the following error
Unhandled rejection SequelizeConnectionError: ER_HANDSHAKE_ERROR: Bad handshake
Please find my code below
var sequelize = new Sequelize(config.database, config.username, config.password, {
"host": config.host,
"dialect": "mysql",
"dialectOptions": {
"ssl": {ca:fs.readFileSync("./MyCert.crt")},
insecureAuth: true
}
});
FYI: I am using sequelize#2.0.0
kindly help me out in this.
Thanks in advance

Issue fixed in the latest sequelize version(>4.0)

Related

ConnectionError [SequelizeConnectionError]: connect ETIMEDOUT NodeJS

I was able to connect to a remote mysql db with sequelize successfully until yesterday afternoon. I now receive the error SequelizeConnectionError: connect ETIMEDOUT. It just stopped working and we did not release an update or make any changes.
I can also connect to the same database on my localhost without any issues. Also increased the idle and acquire time.
I am using sequelize default database connection code new Sequelize(...) and the following config below:
production": {
"username": "username",
"password": "password",
"database": "database",
"host": "host",
"dialect": "mysql"
}
Any ideas as it worked 100% until yesterday afternoon?
I have already tried to create a new app service and also tried to install a new nodejs app and it still gives the same error.Also increased the idle and acquire time.

What is causing this Heroku mysql connection error? (Trying to connect frontend to backend POST request)

{Solved}. New Bug
Error from Heroku logs:
Error: connect ECONNREFUSED 127.0.0.1:3306
2021-09-23T18:24:12.236657+00:00 app[web.1]: at TCPConnectWrap.afterConnect. [as oncomplete] (node:net:1146:16) {
2021-09-23T18:24:12.236658+00:00 app[web.1]: errno: -111,
2021-09-23T18:24:12.236658+00:00 app[web.1]: code: 'ECONNREFUSED',
2021-09-23T18:24:12.236658+00:00 app[web.1]: syscall: 'connect',
2021-09-23T18:24:12.236659+00:00 app[web.1]: address: '127.0.0.1',
2021-09-23T18:24:12.236659+00:00 app[web.1]: port: 3306,
2021-09-23T18:24:12.236659+00:00 app[web.1]: fatal: true
Information / Background:
React js front end (hosted now on Netlifty)
Javascript Node backend using Express and MYSQL2 (Hosted on Heroku)
Goal:
To connect Neflifty Frontend POST request with Heroku Backend fetching POST payload data and inserting it into MYSQL table.
Update: 09/24/2021
I have done all that was suggested. I have created a new database with clearDB. Added it and tested the connection in mysql Workbench. Created the table needed. Updated the backend code for creating the connection to the new database. Checked heroko variables and made sure they reflected correctly to the new database. Now there is an authorization issue. {solved}
New Backend Code with corrections:
const express = require('express');
const app = express();
const port = process.env.Port || 8000
app.listen(port);
console.log(`server is listing on ${port}`);
Time Out
Question:
If this connects locally on workbench why wouldn't Heroku connect if they are added as variables? {Answered}
New question:
why is it trying to use port 8000 when it should be using the environment? Why is it timing out?
Any help on this would be greatly appreciated.
You can't connect to the database because you don't have any database instance running at Heroku. Once you pushed your code using Heroku CLI, Heroku sets up a NodeJS application for you, but it does not mean that it will set up a database for you aswell.
You can add a database to your app through Heroku's interface or CLI. From CLI (we are going to setup ClearDB but any MySQL database addon may work):
heroku addons:create cleardb:ignite
After done that, you wanna have your new database URL (which won't be localhost) with:
heroku config | grep CLEARDB_DATABASE_URL
The last command's output will be something like:
mysql://<user>:<password>#<host>/<database>?reconnect=true
Now, with this in hand, you shall modify your code a little bit with that new information. You don't want to expose your database credentials in your versioning control, so you may use environment variables to get it done:
const db = mysql.createConnection({
connectionaLimit: 50,
user: process.env.DB_USER,
host: process.env.DB_HOST,
password: process.env.DB_PASSWORD,
database: process.env.DATABASE,
port: 3306
});
Also, you need to set the environment variables for your running Heroku's app:
heroku config:set DB_USER=<user>
heroku config:set DB_PASSWORD=<password>
heroku config:set DB_HOST=<host>
heroku config:set DATABASE=<database>
Now, you have a database instance running at Heroku and a NodeJS app instance running that can connect to this database.
For further reading, you may wanna take a look at those links:
https://lo-victoria.com/build-a-mysql-nodejs-crud-app-4-deploying-to-heroku-finale
https://www.bezkoder.com/deploy-node-js-app-heroku-cleardb-mysql/
https://raddy.co.uk/blog/how-to-deploy-node-js-express-ejs-mysql-website-on-heroku-cleardb/ (this one uses Heroku's interface)

How to connect node.js to mysql on google compute engine?

I am running an instance on Google Compute Engine. I installed mysql and node.js on this instance.
I have put the following in the index.js file:
const mysql = require("mysql");
var mysql_connection = mysql.createConnection(
{
host: "instance external IP",
user: "my_user_name",
password: "my_pw",
database: "my_db"
}
);
mysql_connection.connect(err =>
{
if(err) throw err;
console.log("Connected to the database.");
});
I get this error:
Error: Cannot find module 'mysql'
How do I connect node.js to mysql on compute engine?
Do I need to get a different host address instead of the external IP, if so, what is it or where do I find it?
Is the require("mysql") the issue, if so, how do I fix this to find the mysql module on the compute engine instance?
Per comments conversation above, install the mysql node module using:
npm install mysql

Always show error when connecting aws mysql via node.js

I try to connect aws mysql via nodejs
var mysql = require('mysql')
var connection = mysql.createConnection({
host : 'xxxx.xxxc.ap-northeast-1.rds.amazonaws.com',
port : '3306',
user : 'dbusername',
password : 'passwords',
database : 'dbname'
})
connection.connect(function(err){
if(!err) {
console.log("connected ... ")
} else {
console.log(err)
}
});
When I run this code, and it will take some seconds, and then shows following error -
{ Error: connect ETIMEDOUT
at Connection._handleTimeoutError (/node_modules/mysql2/lib/connection.js:181:13)
at ontimeout (timers.js:466:11)
at tryOnTimeout (timers.js:304:5)
at Timer.listOnTimeout (timers.js:267:5)
errorno: 'ETIMEDOUT',
code: 'ETIMEDOUT',
syscall: 'connect',
fatal: true }
I googled a lot, and couldn't find a right answer, and I try to use node-mysql2, has the same issue. Any ideas?
Check it via connecting your mysql aws rds via any mysql client(mysql workbench, dbninja etc.), if you are able to connect it via any client then must be something wrong with the nodejs setup otherwise it will be issue with the RDS details.
Also check it if RDS has restricted access for particular VPN So your node server should be in same VPN to connect.
By default, network access to AWS RDS instances is turned off.
I highly recommend to:
Test whether your instance with Nodejs is able to connect to the
mysql instance. You can do by:
sudo apt-get install mysql-client (for Ubuntu)
Check security groups configuration of RDS instances. Follow AWS
tutorial.
Follow AWS RDS Security Best
Practices
and configure the security groups appropriately.
Check the following things:
1-RDS security group and make sure your IP address can access to your RDS,
https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/Overview.RDSSecurityGroups.html
2-Check your password

Not able to connect MySQL with Sails js

I am using sails latest version(0.10.*), I am trying to connect mysql data base, but its throwing following error.
info: Starting app...
error: A hook (orm) failed to load!
error: Error (E_UNKNOWN) :: Encountered an unexpected error
: Query inactivity timeout
at Query. >(/home/pavan/pavanwork/development/plotstoday/node_modules/sails->mysql/node_modules/mysql/lib/protocol/Protocol.js:154:17)
at emitNone (events.js:67:13)
at Query.emit (events.js:166:7)
at Query._onTimeout >(/home/pavan/pavanwork/development/plotstoday/node_modules/sails->mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:116:8)
at Timer.listOnTimeout (timers.js:9
Configuration:
config/connection.js
mysqlServer: {
adapter: 'sails-mysql',
host: 'localhost',
user: 'root',
password: 'vtiger',
database: 'plotstoday'
},
config/model.js
module.exports.models = {
connection: 'mysqlServer',
migrate: 'alter'
};
config/env/development.js
models: {
//connection: 'localMongo'
connection: 'mysqlServer'
}
OS: Ubuntu 14.04 LTS
Installed both sails-mysql adapter and node-mysql adapter.
I even tried giving host:127.0.0.1.
Mysql is working fine from command-line, I tried the suggestion given
GRANT ALL PRIVILEGES ON sails.* TO 'homestead'#'%' WITH GRANT OPTION;
(Reference : Sails can not connect to mySql)
Please suggest
There is an issue with node(v4.2.0) and mysql. I was using node(v4.2.0), the above issue was coming. I upgraded to node v4.2.1, the above mysql timeout issue is resolved, and everything seems to be working