mssql in nodejs not connecting - sql

Trying to connect to my database to create a local website but having connection issues.
Running in node.js, this is my server.js file:
let sql = require('mssql');
let config = {
user: 'sa',
password: 'password',
server: '10.0.1.130\\SQLSERVER',
database: 'MY_DB',
};
function connect() {
let dbConn = new sql.ConnectionPool(config);
dbConn.connect()
}
connect();
Upon running this I get the following error:
ConnectionError: Failed to connect to 10.0.1.130:undefined - self signed certificate
code: 'ESOCKET'
Not sure why it is removing \SQLSERVER but that seems to be my issue.
I know the credentials are all correct as I connect to this on another computer (ubuntu) but its being removed and we want to move it to windows, have not been able to connect to this point.
Any suggestions are appreciated.

Related

Encountering "Error: connect ECONNREFUSED 127.0.0.1:6379..." when running Google Cloud Function connecting to a Redis Instance

Im running a function to ingest data to a Redis instance. However, I am encountering an issue where I can't seem to connect to the client.
Here is the part of the code where it connects to the server.
const redis = require('redis');
require('dotenv').config;
const REDISHOST = process.env.REDISHOST;
const REDISPORT = 6379;
const REDISAUTH = process.env.AUTHSTRING;
const client = redis.createClient({
port: REDISPORT,
host: REDISHOST,
password: REDISAUTH
});
await client.connect();
Here is the whole error message:
"Error: connect ECONNREFUSED 127.0.0.1:6379
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1278:16)
at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17)"
Any help would be appreciated. Thanks
I have tried using ioredis instead of redis but it shows a different error altogether.
It's trying to connect to localhost and Redis doesn't exist there. IF not given a hostname, this is the default behavior of Redis.
Looking at your code, this is probably because the REDISHOST environment variable isn't defined.

Trying to connect to on-prem SQL Server using ntlm and nodejs

looking for an example or code that allows to connect a very simple node.js console application to a SQL Server Instance (2019) using ntlm and no name and password, using tedious and/or mssql.
For tedious, the code below works fine:
but I cannot seem to get the code to use ntlm without providing a name and password.
//
var Connection = require('tedious').Connection;
var config = {
server: '(local)',
authentication: {
type: 'default',
options: {
userName: 'sa',
password: 'your_sa_password' //update me
}
},
options: {
// If you are on Microsoft Azure, you need encryption:
encrypt: true,
database: 'master',
trustServerCertificate: true
}
};
var connection = new Connection(config);
connection.on('connect', function(err) {
// If no error, then good to proceed.
console.log("Connected");
});
connection.connect();
//
(I have tried the above with and without the trustServerCertificate, it seems necessary)
For mssql, I am using the first example on this page:
https://tediousjs.github.io/node-mssql/#connect-callback
And it returns nothing - nothing at all, whether I use sql auth or ntlm. Not sure what is going on there - I have edited the catch block to have an error output, nothing happens.
The Instance is up, the code above runs, I have nothing special or unusual about the SQL Server that I know of at all.
Thoughts? Pointers? Any help deeply appreciated. I

Timeout error while connecting to SQL Server: Connection Error: Failed to connect to servername\instancename in 15000ms

The error code is code ETIMEOUT. I am using SQL Server 2014, NodeJs version v12.16.2 and I am running this code in Visual Studio Code.
I have created the database and the table is also created with some records. For the server name, I have also tried giving the FQDN, but it didn't work.
This is the code snippet:
const express = require('express');
const app = express();
var Connection = require('tedious').Connection;
var Request = require('tedious').Request;
app.get('/', function(req, res) {
res.send('<hHii</h');
});
const sql = require('mssql');
var config = {
user: 'domain\username',
password: 'mypwd',
server: 'servername',
host: 'hostname',
port: '1433',
driver: 'tedious',
database: 'DBname',
options: {
instanceName: 'instancename'
}
};
sql.connect(config, function(err) {
if (err)
console.log(err);
let sqlRequest = new sql.Request();
//var sqlRequest = new sql.Request(connection)
let sqlQuery = 'SELECT * from TestTable';
sqlRequest.query(sqlQuery, function(err, data) {
if (err) console.log(err);
console.log(data);
//console.table(data.recordset);
// console.log(data.rowAffected);
//console.log(data.recordset[0]);
sql.close()
});
});
const webserver = app.listen(5000, function() {
console.log('server is up and running....');
});
Error:
tedious deprecated The default value for `config.options.enableArithAbort` will change from `false` to `true` in the next major version of `tedious`. Set the value to `true` or`false` explicitly to silence this message.
node_modules\mssql\lib\tedious\connection-pool.js:61:23
server is up and running....
ConnectionError: Failed to connect to servername\instantname in 15000ms
at Connection.<anonymous(..\SQL\Sample\sample\node_modules\mssql\lib\tedious\connection-pool.js:68:17)
at Object.onceWrapper (events.js:417:26)
at Connection.emit (events.js:310:20)
at Connection.connectTimeout (..\SQL\Sample\sample\node_modules\tedious\lib\connection.js:1195:10)
at Timeout._onTimeout (..\SQL\Sample\sample\node_modules\tedious\lib\connection.js:1157:12)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7) {
code: 'ETIMEOUT',
originalError: ConnectionError: Failed to connect to INPUNPSURWADE\DA in 15000ms
at ConnectionError (..\SQL\Sample\sample\node_modules\tedious\lib\errors.js:13:12)
at Connection.connectTimeout (..\SQL\Sample\sample\node_modules\tedious\lib\connection.js:1195:54)
at Timeout._onTimeout (..\SQL\Sample\sample\node_modules\tedious\lib\connection.js:1157:12)
at listOnTimeout (internal/timers.js:549:17)
at processTimers (internal/timers.js:492:7) {
message: 'Failed to connect to servername\instantname in 15000ms',
code: 'ETIMEOUT'
},
name: 'ConnectionError'
}
ConnectionError: Connection is closed.
at Request._query (..\SQL\Sample\sample\node_modules\mssql\lib\base\request.js:462:37)
at Request._query (..\SQL\Sample\sample\node_modules\mssql\lib\tedious\request.js:346:11)
at Request.query (..\SQL\Sample\sample\node_modules\mssql\lib\base\request.js:398:12)
at Immediate.<anonymous(..\SQL\Sample\sample\index.js:43:12)
at processImmediate (internal/timers.js:458:21) {
code: 'ECONNCLOSED',
name: 'ConnectionError'
}
Go to Start menu in Windows, search for "Services" and open it.
Look for "SQL Server Browser"
Right click on it and go to Properties.
Switch "Start up type" to "Automatic"
Click Ok
Right click on it again and Start the service.
It should work after that!
You might make a typo in host/port, or the server doesn't listen external interfaces (it might be configured to liten 127.0.0.1 only)
to check that the server is listening to incoming connections, run in terminal the following:
telnet <hostname> <port>
(mac/linux only)
If it says "Unable to connect to remote host: Connection refused" it means the host is there but it doesn't listen the port.
If it says "Unable to connect to remote host: Connection timed out" then the host might not be there, ot doesn't listen to the port.
You may also want to check if your firewall allows connection to that server.

Issues with output while connecting using nodejs mssql

I'm new to node.js and I'm trying to connect to a intersystems-cache database. Here is what I have, based off the sample code in the docs:
var sql = require('mssql');
console.log("Connecting...");
sql.connect("mssql://username:password#server:1234/DB").then(function(){
console.log("connected");
}).catch(function(err) {
console.log(err)
});
If I put in the wrong server or port, I get a Failed to connect error, but no matter what else I enter incorrectly (username, pass, db) I get zero output. More importantly, when all the data is correct I never get the connected output.
Is this a compatibility issue with Cache and the mssql library? Or am I doing something wrong?
why do you use mssql, instead of cache, while you have to connect to cache.
If you look at the documentation, you may find and example
var globals = require('cache');
var mydata = new globals.Cache();
mydata.open(
{ path:"/cache20102/mgr",
username: "_SYSTEM",
password: "SYS",
namespace: "USER"
},
function(error, result){}
);

Gulp task to SSH and then mysqldump

So I've got this scenario where I have separate Web server and MySQL server, and I can only connect to the MySQL server from the web server.
So basically everytime I have to go like:
step 1: 'ssh -i ~/somecert.pem ubuntu#1.2.3.4'
step 2: 'mysqldump -u root -p'password' -h 6.7.8.9 database_name > output.sql'
I'm new to gulp and my aim was to create a task that could automate all this, so running one gulp task would automatically deliver me the SQL file.
This would make the developer life a lot easier since it would just take a command to download the latest db dump.
This is where I got so far (gulpfile.js):
////////////////////////////////////////////////////////////////////
// Run: 'gulp download-db' to get latest SQL dump from production //
// File will be put under the 'dumps' folder //
////////////////////////////////////////////////////////////////////
// Load stuff
'use strict'
var gulp = require('gulp')
var GulpSSH = require('gulp-ssh')
var fs = require('fs');
// Function to get home path
function getUserHome() {
return process.env.HOME || process.env.USERPROFILE;
}
var homepath = getUserHome();
///////////////////////////////////////
// SETTINGS (change if needed) //
///////////////////////////////////////
var config = {
// SSH connection
host: '1.2.3.4',
port: 22,
username: 'ubuntu',
//password: '1337p4ssw0rd', // Uncomment if needed
privateKey: fs.readFileSync( homepath + '/certs/somecert.pem'), // Uncomment if needed
// MySQL connection
db_host: 'localhost',
db_name: 'clients_db',
db_username: 'root',
db_password: 'dbp4ssw0rd',
}
////////////////////////////////////////////////
// Core script, don't need to touch from here //
////////////////////////////////////////////////
// Set up SSH connector
var gulpSSH = new GulpSSH({
ignoreErrors: true,
sshConfig: config
})
// Run the mysqldump
gulp.task('download-db', function(){
return gulpSSH
// runs the mysql dump
.exec(['mysqldump -u '+config.db_username+' -p\''+config.db_password+'\' -h '+config.db_host+' '+config.db_name+''], {filePath: 'dump.sql'})
// pipes output into local folder
.pipe(gulp.dest('dumps'))
})
// Run search/replace "optional"
SSH into the web server runs fine, but I have an issue when trying to get the mysqldump, I'm getting this message:
events.js:85
throw er; // Unhandled 'error' event
^
Error: Warning:
If I try the same mysqldump command manually from the server SSH, I get:
Warning: mysqldump: unknown variable 'loose-local-infile=1'
Followed by the correct mylsql dump info.
So I think this warning message is messing up my script, I would like to ignore warnings in cases like this, but don't know how to do it or if it's possible.
Also I read that using the password directly in the command line is not really good practice.
Ideally, I would like to have all the config vars loaded from another file, but this is my first gulp task and not really familiar with how I would do that.
Can someone with experience in Gulp orient me towards a good way of getting this thing done? Or do you think I shouldn't be using Gulp for this at all?
Thanks!
As I suspected, that warning message was preventing the gulp task from finalizing, I got rid of it by commenting the: loose-local-infile=1 From /etc/mysql/my.cnf