i'm trying to connect to my database using MSSQL Server using NodeJS
but i have an error
{ ConnectionError: Failed to connect to 10.10.17.199:undefined in 15000ms
here is the beginning of my code:
var sql = require("mssql");
// config for your database
var config = {
user: 'mat1',
password: 'a',
server: '10.10.17.199\\MSSQLLocalDB',
database: 'master' ,
port: 1433
};
//\\MSSQLLocalDB
console.log("passed 1")
// connect to your database
sql.connect(config, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
what's wrong ?
You are getting an error trying to connect to the server. Maybe you dont have access to the server. Also can happen is timing out to connect, if thats the case you can modify the connection timeout setting in the connection configuration.
Related
I have access to a remote postgres DB from pgAdmin4 and I also could access from nodejs using a Mac. Right now I'm using the same code to access the DB in Windows. The code for my connection is the following:
const { Client } = require('pg'); //Importing the Postgres package
const hosts= require('../hosts'); //Using the file containig all hosts
const connectionData = { //Begin creating the connection settings object
host: hosts.DBHost, //DB host
port: hosts.DBPort, //DB hosts port
database: hosts.DB, //DB
user: hosts.DBUser, //DB user
password: hosts.DBPassword, //DB user password
}
My test is the following:
var client = new Client(connectionData); //New client instance using the above connection settings
client.connect(); //Open the connection to the database()
sql = "select * from myTable";
client.query(sql)
.then(response => {
console.log ({"data": response}); //This isn't shown
})
.catch(err => {
console.log({"error": err}); //This isn't shown neither
})
No error, no exception, the DB server doesn't respond!
Why isn't the server responding?
I suspect that you have the same problem like in this other post. Since it is not a 100% duplicate I will post this again:
There is a known issue in the pg module and NodeJS 14.
The proposed solution is to make sure you have pg>=8.0.3 installed.
This can be done by updating pg in the dependencies.
Also make sure, that any other library depending on the pg module, is also up to date and has the latest pg version.
If this is not possible for any reason - using Node 12 should also work.
As I am using SQL Server 18 and nodeJs version12, below code snippet I tried and I get an error shown below.
Code snippet:
var sql = require('mssql/msnodesqlv8');
var config = {
driver: 'msnodesqlv8',
connectionString: 'Driver=SQL Server Native Client 11.0;Server=Server_name;Database=DBname;Trusted_Connection=yes;'
};
sql.connect(config)
.then(function() {
console.log("done");
})
.catch(function(err) {
console.log(err);
});
Error :
ConnectionError: Error: [Microsoft][SQL Server Native Client 11.0]Named Pipes Provider: Could not open a connection to SQL Server [53]. ,Error: [Microsoft][SQL Server Native Client 11.0]Login timeout expired,Error: [Microsoft][SQL Server Native Client 11.0]A network-related or instance-specific error has occurred while establishing a connection to SQL Server. Server is not found or not accessible. Check if instance name is correct and if SQL Server is configured to allow remote connections. For more information see SQL Server Books Online.
at C:\Users\user\test-app\node_modules\mssql\lib\msnodesqlv8\connection-pool.js:56:17
at Immediate. (C:\Users\user\test-app\node_modules\msnodesqlv8\lib\connection.js:362:15)
at processImmediate (internal/timers.js:456:21) {
code: undefined,
name: 'ConnectionError'
I have been trying to establish connection between Node.js application and mysql and have tried everything and couldn't succeed.
I'm able to connect through PHP application. My port is default where 3306
Can anyone help me to resolve this?
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'root'
});
connection.connect(function(err) {
if (err) {
console.error('error connecting: ' + err.stack);
return;
}
console.log('connected as id ' + connection.threadId);
});
[This is the error message I got,I can able to connect through my php application][1]
This is the error message I got,I can able to connect through my php application
Uncomment "bind-address" and assign bind-address="0.0.0.0".
For More Information please refer this,
Solving a "communications link failure" with JDBC and MySQL
A better approach to connect to DB is to use pools to connect to DB.
Copying from here
var mysql = require('mysql');
var pool = mysql.createPool({
host : 'example.org',
user : 'bob',
password : 'secret'
});
pool.getConnection(function(err, connection) {
// connected! (unless `err` is set)
connection.release();
});
This also allow you to make parallel calls to your database.
I'm trying to connect to MongoDB with ssl option using old NodejS version. Connection fails with a timeout error.
I found that connection start works after iojs 3.0.0 release. But I must use NodeJS 0.10.40 in my environment.
It seems a data event is never emitted on a tls socket.
My original problem is there: https://github.com/thaliproject/jxcore/issues/115
Code example and output:
const mongodb = require('mongodb');
const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://****?ssl=true'; // Any cloud server with ssl
MongoClient.connect(url, function (err, db) {
if (err) return console.log(err);
console.log('Connected correctly to server');
db.close();
});
{ [MongoError: connection 0 to
aws-us-east-1-portal.28.dblayer.com:23087 timed out] name:
'MongoError', message: 'connection 0 to
aws-us-east-1-portal.28.dblayer.com:23087 timed out' }
my code below
var mongoose = require('mongoose');
for(let i = 0;i<600;i++)
{
let db1 = mongoose.createConnection('mongodb://127.0.0.1:27017/abc');
db1.on('error', function(error) {
console.log("error = "+(i)+" "+error +db1);
});
db1.on('close', function() {
console.log("close = "+(i)+" "+db1);
});
db1.once('open', function() {
"use strict";
// db1.close();
});
}
I wanted to test mongodb ,the result is
error = 364 MongoError: failed to connect to server [127.0.0.1:27017] on first connect[object Object]
error = 365 MongoError: failed to connect to server [127.0.0.1:27017] on first connect[object Object]
error = 385 MongoError: failed to connect to server [127.0.0.1:27017] on first connect[object Object].......
Another question is whether the connection needs to be closed?
thanks.
Make sure that you have Mongo running on on port 27017 of the same computer that your Node app is running. To verify that, run this in the command line:
mongo localhost:27017
You don't need to close the connection and open it multiple times. You should open the connection once in your app and close it only when you want to close your app. See those answers for more datails:
Where to initialize a new database connection in nodejs?
Using mongoDB in Express routers