Sequelize connection to AWS RDS SQL Express Database in AWS Beanstalk - node.js

I have an elastic beanstalk website and a separate RDS database (SQL Express). When I ssh into the beanstalk instance, I can connect to the database on the server so the security groups are setup up correctly. However, when I use sequelize in my app, I cannot connect to the database.
I receive the below error in sequelize:
[SequelizeConnectionError]: Failed to lookup instance on web.cluster-qwertyuiop.us-west-2.rds.amazonaws.com - getaddrinfo ENOTFOUND web.cluster-qwertyuiop.us-west-2.rds.amazonaws.com
at ConnectionManager.connect (/var/app/current/node_modules/sequelize/lib/dialects/mssql/connection-manager.js:145:17) {
parent: ConnectionError: Failed to lookup instance on web.cluster-qwertyuiop.us-west-2.rds.amazonaws.com - getaddrinfo ENOTFOUND web.cluster-qwertyuiop.us-west-2.rds.amazonaws.com
at ConnectionError (/var/app/current/node_modules/tedious/lib/errors.js:13:12)
at /var/app/current/node_modules/tedious/lib/connection.js:1077:60
at /var/app/current/node_modules/tedious/lib/instance-lookup.js:116:20
at GetAddrInfoReqWrap.callback (/var/app/current/node_modules/tedious/lib/sender.js:140:16)
at GetAddrInfoReqWrap.onlookupall [as oncomplete] (dns.js:74:17) {
code: 'EINSTLOOKUP'
},
This is my connection string:
new Sequelize('DATABASE', username, password, {
dialect: 'mssql',
dialectOptions: {
requestTimeout: 300000,
options: {
driver: 'SQL Server Native Client 11.0',
instanceName: 'SQLEXPRESS',
trustedConnection: true,
},
},
logging: false,
port: 1433,
host: 'web.cluster-qwertyuiop.us-west-2.rds.amazonaws.com',
pool: {
max: 20,
min: 3,
idle: 20000,
evict: 0,
acquire: 20000,
handleDisconnects: true,
},
There's very little to find online when trying to connect to MS SQL instances. I've been stuck on this one for a few days now.

Related

How to use mssql connection using ip/instance?

I'm trying to connect to a production SQL Server instance but it fails to connect.
Could anyone help?
I'm grateful for the community's attention.
.env:
MSSQL_SERVER=ip/instance
MSSQL_PORT=1433
MSSQL_USER=sa
MSSQL_PASSWORD=password
MSSQL_DB_NAME=database
Error:
Failed to connect to ip/instance:1433 - getaddrinfo
ENOTFOUND ip/instance
adonisjs#lucid
mssql: {
client: "mssql",
connection: {
user: Env.get("MSSQL_USER"),
port: parseInt(Env.get("MSSQL_PORT")),
server: Env.get("MSSQL_SERVER"),
password: Env.get("MSSQL_PASSWORD", ""),
database: Env.get("MSSQL_DB_NAME")
},
healthCheck: false,
debug: false,
},
Code:
Database.connection('mssql').from('dbo.EMP').select('*')
Thank you. Its work with -
options: { encrypt: false, // for azure trustServerCertificate: true, // change to true for local dev / self-signed certs

Nodejs mssql server connection throws an error

I want to connect my mssql database from nodejs.
I am using sequelize and tedious module for this.
My connection configs like this
const sequelize = new Sequelize(
"mydb",
"username", "password", {
host:config_data.host,
"dialect":"mssql",
"port":1433
dialectOptions: {
instanceName: "MSSQLSERVER"
},
},
);
When I tried to run script, it throws an error.
(node:14584) UnhandledPromiseRejectionWarning: SequelizeConnectionError: Failed to connect to 192.168.10.220:1433 - Could not connect (sequence)
I apply all steps in configuration manager.
Enabled TCP/IP
Started Server SQL browser
Added 1433 port to Firewall
There is also additional error when host is "localhost".(Currently writed IP address)
(node:13552) UnhandledPromiseRejectionWarning: SequelizeConnectionError: Failed to connect to localhost:1433
- 4292:error:1425F102:SSL routines:ssl_choose_client_version:unsupported protocol:c:\ws\deps\openssl\openssl\ssl\statem\statem_lib.c:1947:
I need to help, is there someone have any idea ?
You need to set encrypt to false in your Sequelize inside options.
const sequelize = new Sequelize(
"mydb",
"username", "password", {
host:config_data.host,
"dialect":"mssql",
"port":1433,
"options": {
encrypt: false,
enableArithAbort: false
},
dialectOptions: {
instanceName: "MSSQLSERVER"
},
},
);
The issue is because of TLS protocol mismatch between Source & Destination server. In my case my App server was Ubuntu 20.04 & my SQL Server(Express 2012) was on Windows server. I also tried to downgrade TLS protocols on Ubuntu but nothing worked. So finally I disabled tls encryption in Sequelize.
Make sure options are given as expected by Sequelize
const sequelize = new Sequelize(dbConfig.DB, dbConfig.USER, dbConfig.PASSWORD, {
host: dbConfig.HOST,
port: dbConfig.PORT,
dialect: dbConfig.dialect,
pool: {
max: dbConfig.pool.max,
min: dbConfig.pool.min,
acquire: dbConfig.pool.acquire,
idle: dbConfig.pool.idle,
},
// The below options are important to supress ssl issue on
// AWS EC2 ubuntu when db server is on windows. There is TLS protocol issue
// Which by using these options we disable tls encryption
dialectOptions: {
// Observe the need for this nested `options` field for MSSQL
options: {
encrypt: false,
enableArithAbort: false
}
}
});

SQL Server host not found error with sequelize

I am trying to use SQL Server with node.js using sequelize orm. But I keep getting host not found error:
HostNotFoundError [SequelizeHostNotFoundError]: Failed to connect to
localhost//SQLEXPRESS:1433 - getaddrinfo ENOTFOUND localhost//SQLEXPRESS
Failed to connect to localhost//SQLEXPRESS:1433 - getaddrinfo ENOTFOUND localhost//SQLEXPRESS', code: 'ESOCKET
Code:
var db = new Sequelize('test', 'root', 'secret', {
host: 'localhost\\SQLEXPRESS',
dialect: 'mssql',
pool: {
max: 5,
min: 0,
idle: 10000
},
});
db.authenticate()
.then(()=>console.log('Database connected'))
.catch(err=> console.log(err))
Please help me!
trying
options.port The port of the relational database.
var db = new Sequelize('test', 'root', 'secret', {
host: 'localhost\\SQLEXPRESS',
dialect: 'mssql',
pool: {
max: 5,
min: 0,
idle: 10000
},
port:56198,
});

Connect NodeJS to MongoDB Droplet

So I've decided to set up my project with two droplets, a MongoDB image droplet and a NodeJS image droplet. I did so to make it easier to scale both droplets in the future, and other applications may be connecting to the DB in the future, both on Ubuntu 18.04.
I'm getting the error of:
Could not connect to the database: { MongooseServerSelectionError: connection timed out
at new MongooseServerSelectionError (/root/eternal-peace-code/node_modules/mongoose/lib/error/serverSelection.js:22:11)
at NativeConnection.Connection.openUri (/root/eternal-peace-code/node_modules/mongoose/lib/connection.js:808:32)
at Mongoose.connect (/root/eternal-peace-code/node_modules/mongoose/lib/index.js:333:15)
at Timeout.setTimeout [as _onTimeout] (/root/eternal-peace-code/app.js:60:22)
at ontimeout (timers.js:482:11)
at tryOnTimeout (timers.js:317:5)
at Timer.listOnTimeout (timers.js:277:5)
message: 'connection timed out',
name: 'MongooseServerSelectionError',
reason:
TopologyDescription {
type: 'Single',
setName: null,
maxSetVersion: null,
maxElectionId: null,
servers: Map { 'mongo_droplet_ip:27017' => [Object] },
stale: false,
compatible: true,
compatibilityError: null,
logicalSessionTimeoutMinutes: null,
heartbeatFrequencyMS: 10000,
localThresholdMS: 15,
commonWireVersion: null },
[Symbol(mongoErrorContextSymbol)]: {} }
I have set up both servers correctly (so I believe), my MongoDB has two users (both are me but different permissions, I have followed several walkthroughs and so it just happened). My /etc/mongod.conf file has been edited accordingly:
net:
port: 27017
bindIp: 127.0.0.1,mongodb_droplet_ip
security:
authorization: "enabled"
The UFW firewall has HTTPS, SSH and my NodeJS_ip:27017 enabled.
My NodeJS droplet is set up with a domain name pointing to the right IP address, letsencrypt is set up for secure connection at the domain name. The issue I have now is that I can not connect to my Mongo droplet from my NodeJS application, and I would also like to make sure that it is all done securely too.
My NodeJS connect code is using Mongoose and a variables environment file, I also have the whole connect in a timeout function as per another suggestion I saw elsewhere:
setTimeout(async () => {
console.log('Connecting to database...');
const options = {
user: process.env.DBUSER,
pass: process.env.USERPASS,
keepAlive: true,
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
sslCA: cert,
connectTimeoutMS: 5000,
}
await mongoose.connect(process.env.LIVEDB, options)
.then(() => {
console.log('We are connected to the database');
})
.catch(err => {
console.log('Could not connect to the database: ', err);
connectWithTimeout();
});
}, 3000);
I have tried a multitude of things through both droplets but I have wasted around 4 days on getting the project to a staging/production stage so it can launch whenever and just be updated as time goes on.
If you need anything else, please do let me know.
All help would be hugely appreciated,
Thanks
I don't know the URI format you are using. But I got the same issue initially when I tried to connect my node application with Mongo instance of AWS.
Using the long URI sting with all the cluster name like this
mongoURI = 'mongodb://username:password#mongo-instance-shard-00-00-a4iv8.mongodb.net:27017,mongo-instance-shard-00-01-a4iv8.mongodb.net:27017,mongo-instance-shard-00-02-a4iv8.mongodb.net:27017/test?ssl=true&replicaSet=mongo-instance-shard-0&authSource=admin&retryWrites=true&w=majority'
Instead of something like this:
mongodb+srv://server.example.com/
This worked for me. It might help you as well.
Also, found this for digital ocean link
and mongo documentation for the connection string

sequelize connection timeout with cloud sql proxy in node.js

Running a local config, the connection works fine. I've installed and have a cloud_sql_proxy running and ready for new connections.
Here's my sequelize connection code:
const sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASS, {
dialect: 'mysql',
host: `/cloudsql/${CLOUD_SQL_CONNECTION_NAME}`,
pool: {
max: 30,
min: 0,
idle: 10000,
acquire: 1000000,
},
dialectOptions: {
connectTimeout: 100000
}
});
At some point I set my max connection, acquire, and connectTimeout variables to absurdly high numbers, but still no luck, but I started getting a specific IP address located in London on port 3306.
I was able to connect to the cloud db by modifying my config to the following:
const sequelize = new Sequelize(DB_NAME, DB_USER, DB_PASS, {
dialect: 'mysql',
host: DB_HOST,
timestamps: false,
pool: {
max: 5,
min: 0,
idle: 10000
},
});
The difference is I pointed the host to the actual db IP address instead of the /cloudsql/${CLOUD_SQL_CONNECTION_NAME} path.

Resources