How to use mssql connection using ip/instance? - node.js

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

Related

Failed to connect to *name_server* - getaddrinfo ENOTFOUND

When connecting to the database with sequelize-typescript, an error occurs
Failed to connect to SERVER\SQL2016:1433 - getaddrinfo ENOTFOUND SERVER\SQL2016
Connection settings
import { Sequelize } from 'sequelize-typescript'
import { environment } from '../config'
import { normalize, join } from 'path'
export default new Sequelize({
database: environment.database.database,
dialect: "mssql",
username: environment.database.username,
// port: environment.database.port,
password: environment.database.password,
host: environment.database.host,
logging: !environment.production ? console.log : false,
models: [normalize(join(__dirname, "..", "models"))],
dialectOptions: {
options: {
enableArithAbort: true,
cryptoCredentialsDetails: {
minVersion: "TLSv1",
},
},
},
})
interface DatabaseConnection {
database: string
username: string
port: number
password: string
host: string
hostAsodu: string
databaseAsodu: string
}
export const environment: Environment = {
port: process.env.PORT ? Number(process.env.PORT) : 3030,
production: process.env.NODE_ENV === "production",
database: {
database: process.env.DB_DATABASE ?? String(),
username: process.env.DB_USERNAME ?? String(),
port: process.env.DB_PORT ? Number(process.env.DB_PORT) : 0,
password: process.env.DB_PASSWORD ?? String(),
host: process.env.DB_HOST ?? String(),
hostAsodu: process.env.DB_HOST_ASODU ?? String(),
databaseAsodu: process.env.DB_DATABASE_ASODU ?? String()
},
}
I tried connectit with and without a port, the error is the same. It just connects to SERVER, but does not want to connect to the named SERVER \ SQL2016. How can I fix this error? Found nothing on the docks
ENOTFOUND is an operating-system-level error from your OS's networking code. It means you asked it to look up a hostname and it came up with nothing. In the lingo, your name "could not be resolved." (www.stackoverflow.com is a hostname, for example. https://www.stackoverflow.com is a URL, which happens to contain a hostname.)
getaddrinfo() is OS method that asks the domain name service (DNS) to look up a hostname.
It looks to me like you tried to look up the hostname SERVER\SQL2016. That's not a hostname. You probably want something like sql2016.example.com instead. Ask the person who operates that SQL Server instance for the correct hosthame.
The SQL Server instance I use has a hostname something like devdatabase.dev.example.com.
Edit The SQL2016 part of your connection string is known as the server instance or DataSource. You need to specify it separately from the hostname. See this. Error connecting to SQL Server database with sequelize You also need to make sure your SQL Server software is configured to allow TCP connections.
Try this as you connect.
export default new Sequelize({
database: environment.database.database,
dialect: "mssql",
username: environment.database.username,
// port: environment.database.port,
password: environment.database.password,
host: environment.database.host, /* should be the hostname without \SQL2016 */
logging: !environment.production ? console.log : false,
models: [normalize(join(__dirname, "..", "models"))],
dialectOptions: {
instanceName: 'SQL2016',
options: {
enableArithAbort: true,
cryptoCredentialsDetails: {
minVersion: "TLSv1",
},
},
},
})

Sequelize connection to AWS RDS SQL Express Database in AWS Beanstalk

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.

Sailsjs Heroku Postgresql Error: self signed certificate

I am new to sails.js and I deployed to heroku and also added a heroku postgreql addon. In my datastore.js file, I added the following configuration so I could migrate my models to the new database addon.
Here is a snippet of my datastore.js file.
adapter: "sails-postgresql",
// URL: my-postgresql-url
// ssl: true,
In my production.js file here's a snippet of the datastore.
adapter: "sails-postgresql",
url: process.env.DATABASE_URL,
ssl: true,
I don't know what seems to be the issue, but this error keeps getting thrown by the server
{
error: Error: self signed certificate
at TLSSocket.onConnectSecure (_tls_wrap.js:1498:34)
at TLSSocket.emit (events.js:314:20)
at TLSSocket.EventEmitter.emit (domain.js:486:12)
at TLSSocket._finishInit (_tls_wrap.js:933:8)
at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:707:12) {
code: 'DEPTH_ZERO_SELF_SIGNED_CERT'
},
meta: {
adapter: 'sails-postgresql',
url: 'my-postgresql-url',
ssl: true,
identity: 'default'
}
}
I don't know what exactly to do, any help would be welcome. Thank you.
Okay, after reading different articles and googling, to solve the issue, I just had to do this.
ssl: {
rejectUnauthorized: false
}
and that fixed the issue.
Hit this problem overnight. Some fiddling and identified that there are additional properties needed in the SSL setup:
A working datastore object for Heroku PostgreSQL is as follows:
datastores: {
default: {
adapter: 'sails-postgresql',
url: process.env.DATABASE_URL,
ssl: {
sslmode: 'require',
rejectUnauthorized: false,
}
},
},

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

How to change default port 1433 on SQL Server in a node.js connection string

I changed the default port 1433 on my SQL Server in centos 7. But in API that is created with Node.js and uses the mssql package to connect to it, I get the error shown here when I want to send or get a request. mssql knows 1433 port as default.
My connection string
var sql = require('mssql');
var DbConnectionString = {
user: 'sa',
password: 'mypassword',
server: 'serverip,newport',
database: 'databasename',
"options": {
"encrypt": false,
"enableArithAbort": true
},
};
module.exports = DbConnectionString;
And this is the error I get:
{
"code": "ESOCKET",
"originalError": {
"message": "Failed to connect to myserverip,mynewport:1433 - getaddrinfo ENOTFOUND myserverip,mynewport",
"code": "ESOCKET"
},
"name": "ConnectionError"
}
How to solve it?
The mssql docs state that port can be specified with the 'port' key:
var DbConnectionString = {
user: 'sa',
password: 'mypassword',
server: 'serverip',
port: newport,
database: 'databasename',
"options": {
"encrypt": false,
"enableArithAbort": true
},
};

Resources