I'm trying to connect to SQL Server:
var sql = require("mssql");
var dbConfig = {
server: "LAP12\\INSTANCE1",
database: "SampleDb",
port: 1433,
options: {
trustedConnection: true
}
};
// connect to your database
sql.connect(dbConfig, function (err) {
if (err) console.log(err);
// create Request object
var request = new sql.Request();
// query to the database and get the records
request.query('select * from SampleTable', function (err, recordset) {
if (err) console.log(err)
// send records as a response
res.send(recordset);
});
});
But I receive this error:
ConnectionError: Port for INSTANCE1 not found in ServerName...
I've tried to follow instructions here https://github.com/patriksimek/node-mssql/issues/130 , but that didn't help. TCP is enabled.
Changing config to this didn't help either:
var dbConfig = {
server: "LAP12",
port: 1433,
options: {
instanceName: 'INSTANCE1',
database: 'SampleDb',
trustedConnection: true,
}
};
Ok, I had the same issue, will try to help.
this is my config exemple
const config = {
user: 'sa',
password: '****',
server: 'DESKTOP-Q5TO47P',
database: 'dbname',
options: {
encrypt: false
}
};
You need to turn on the SQL Server Browser. Go to start up menu or the search and look for SQL Server Configuration Manager. Run it! (Im using 2018 version)
In the left Tab click on SQL Server Services
now in the right tab double click on SQL Server Browser
will open a window, you will see 3 tabs, go for the Service tab
change start mode to Automatic and apply
left click on SQL Server Browser and click restart
Back to the right tab click on SQL Server Network Configuration
then Client Protocols
change TCP/IP to enable
Let me know if it works.
after adding this encrypt: false, application not showing any error neither responding, Its not connecting to DB
In my case the "SQL Browser" is not reachable because the firewall port is not open.
Solution for me is to open the port or change dbConfig to (no instance name, no port):
var dbConfig = {
server: "LAP12",
database: "SampleDb",
options: {
trustedConnection: true
}
};
The port is still closed to SQL-Browser, with that change I can connect to my DB.
My nodejs Package 'mssql'
Related
I'm trying to connect node js script to MsSQL database server, but fails.
My Microsoft SQL Server Management is however able to connect to the database.
node.js code
var Connection = require('tedious').Connection;
var config = {
server: "172.168.200.35",
dialect: "mssql",
port:51433,
authentication: {
type: 'default',
options: {
userName: 'testuser',
password: 'abc123'
}
},
options: {
database: 'IoTDb'
}
};
var connection = new Connection(config);
// Attempt to connect and execute queries if connection goes through
connection.on("connect", err => {
if (err) {
console.error(err.message);
} else {
executeStatement();
}
});
When running the code, I get this error
Failed to connect to 172.168.200.35:51433:1433 - getaddrinfo ENOTFOUND 172.168.200.35:51433
I don't understand why its fails to connect port 1433, as the port is defined as 51433
However, when I try to connect to the database via this login info (same as node config variable)
It connects and I get this view over the database
For info about the database, it is running via docker on another pc, but is reachable
I moved the port to options and now the error is this
Failed to connect to 172.168.200.35:51433:51433 - getaddrinfo ENOTFOUND 172.168.200.35:51433
As this issue shows the port should be specified in options:
var config = {
server: "172.168.200.35",
dialect: "mssql",
...
options: {
database: 'IoTDb',
port:51433,
}
};
I'm trying to create a connection with SQL Server and NodeJS as shown below (SQL Server installed locally -> localhost):
It's throwing the following error: ERROR: Failed to connect to MY_SERVER:1433 - Could not connect (sequence)
This didn't work either.
I thought it was a problem with the SQL Server installation, but via Python It works well on the same database.
I'm using SQL Server 2019 and latest NodeJS version.
REFERENCE:
https://learn.microsoft.com/en-us/sql/connect/node-js/step-3-proof-of-concept-connecting-to-sql-using-node-js?view=sql-server-ver15
How to fix this error?
var config = {
server: 'MY_SERVER',
authentication: {
type: 'default',
options: {
userName: 'sa',
password: 'admin'
}
},
options: {
encrypt: false, // I've already tried with "true" also.
database: 'MyDataBase'
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
if (err) {
console.log("Error: ", err.message);
} else {
console.log("Connected");
}
});
connection.connect();
I got it!! Basically, for NodeJS get connection to SQLServer, the TCP/IP Protocol must be enabled for the MSSQLSERVER Instance, in the SQL Server network settings.
Ref.:
Nodejs connection with mssql showing error
https://store.oceansystems.com/knowledgebase/quickdme-faqs/sql-server-sql-express/configure-sql-express-server-host-enable-tcp-ip-firewall-settings/
I'm trying to connect to a SQL Server 2017. I've tried many different solutions.
the server is set to mixed mode.
The server states in the log files:
Login failed for user
Reason: An attempt to login using SQL authentication failed. Server is configured for Integrated authentication only
I checked the server and the settings are set to mixed mode.
Here is my code:
var sql = require('mssql');
var config = {
server: "server",
authenticationType: "Integrated",
user: "",
password: "",
options: {
encrypt: true,
database: 'TEST',
tdsVersion: '7_1'
}
};
sql.connect(config, function(err) {
var request = new sql.Request();
request.query("SELECT * FROM tablee", function(err, recordset) {
console.log(recordset);
});
});
I tried it with a username and password but also without it. The connection to the SQL Server database is not established. I set the encryption to true or false.
In the terminal of my IDE it says: "undefined".
What am I doing wrong, can someone help me please?
I use this script to connect node.js with Azure Postgresql.
But the ssl verification of our firewall blocks the connection, so in the past I need to use a proxy. Where in the code can I add the proxy settings as like host and port?
Means when I start the code, vscode should connect through the proxy to postgresql.
const pg = require('pg');
const config = {
host: '<your-db-server-name>.postgres.database.azure.com',
// Do not hard code your username and password.
// Consider using Node environment variables.
user: '<your-db-username>',
password: '<your-password>',
database: '<name-of-database>',
port: 5432,
ssl: true
};
const client = new pg.Client(config);
client.connect(err => {
if (err) throw err;
else { queryDatabase(); }
});
function queryDatabase() {
console.log(`Running query to PostgreSQL server: ${config.host}`);
const query = 'SELECT * FROM inventory;';
client.query(query)
.then(res => {
const rows = res.rows;
rows.map(row => {
console.log(`Read: ${JSON.stringify(row)}`);
});
process.exit();
})
.catch(err => {
console.log(err);
});
}
To configure proxy for Visual Studio Code
Edit the settings.json file
Depending on your platform, the user settings file is located here:
Windows: %APPDATA%\Code\User\settings.json
macOS: $HOME/Library/Application Support/Code/User/settings.json
Linux: $HOME/.config/Code/User/settings.json
Modify and Add the below lines to configure your proxy
"http.proxy": "http://user:pass#proxy.com:portnumber",
"https.proxy": "http://user:pass#proxy.com:portnumber",
"http.proxyStrictSSL": false
If your proxy doesn't require authentication, you could simply use
"http.proxy": "http://proxy.com:portnumber",
"https.proxy": "http://proxy.com:portnumber"
"http.proxyStrictSSL": false
Restart VS Code
The documentation related to settings and schema of the settings.json file is here for reference
I've a node application which do have a connection to SQL Server.
Also, I'm using database as a service from Azure.
Code Snippet :
import { Connection } from 'tedious';
import { Request } from 'tedious';
var config = {
userName: 'dbuser',
password: 'dbpassword',
server: 'mydatabase.database.windows.net',
options: {
instanceName: 'SQLEXPRESS', // Removed this line while deploying it on server as it has no instance.
database: 'dbname'
}
};
connection = new Connection(config);
connection.on('connect', function(err) {
if (err) {
console.log('error : '+err);
} else {
console.log("Connected to Database");
}
});
It has a successful connection, if done, locally.
Console Output => Connected to Database.
Deep dive done using console log :
-> Connection object is being created, but, the event ".on" is not being able to establish.
-> The connection gets established when deployed locally, while, when deployed on server, it doesn't works.
Based on the documentation here, you need to provide an additional option for encrypted connection.
Please try the following for config:
var config = {
userName: 'dbuser',
password: 'dbpassword',
server: 'mydatabase.database.windows.net',
options: {
database: 'dbname',
encrypt: true //Need to add this for connecting to Azure DB
}
};
Using this configuration, I was able to connect to my database hosted in Azure.