Mongodump with tunnel-ssh throwing ECONNRESET error in nodejs - node.js

I am creating a backup using mongodump from a remote mongo server. The remote mongo requires a private key so I cannot directly run the mongodump command directly. I need to create a ssh tunnel to the server first. My ssh tunneling works fine in the code below since I have tested it by querying on remote mongo.
The issue is when I run the mongodump query, the dump is created fine but just as the query finish I get this ECONNRESET error below
Error: read ECONNRESET
at TCP.onStreamRead (internal/stream_base_commons.js:208:20)
Emitted 'error' event on Server instance at:
at Socket.emit (events.js:326:22)
at Socket.EventEmitter.emit (domain.js:483:12)
at emitErrorNT (internal/streams/destroy.js:92:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read'
}
My code snippet looks like this:
const sshConfig = {
username: process.env.BACKUP_MONGO_USER,
port: 22,
dstPort: 27017,
localPort: 27018,
host: process.env.BACKUP_MONGO_HOST,
dstHost: '127.0.0.1',
localHost: '127.0.0.1',
privateKey: fs.readFileSync(process.env.BACKUP_MONGO_FILE),
keepAlive: true,
};
const connection = new Promise((resolve, _) => {
// eslint-disable-next-line
tunnel(sshConfig,
async (err, server) => {
resolve({ server });
},
);
});
async function dbDump() {
const { server } = await connection;
console.log('Connection Successful');
const command = `mongodump --db <dbName> --collection <collectionName> --host localhost --port 27018`;
exec(command,
(err, stdout, stderr) => {
if(err) { console.log('err', err); }
if(stderr) { console.log('stderr', stderr); }
if(stdout) { console.log('stdout', stdout); }
server.close();
});
}
dbDump();

Related

Connect SQL Server with Next.js

I am having a problem (ConnectionError) while trying to connect Next.js with SQL Server with mssql, I have enabled TCP/IP, and SQL Server Browser is running.
this is my code:
// db.js
import sql from 'mssql'
// connection configs
const config = {
user: 'test',
password: '1000',
server: '.\sqlexpress',
database: 'DATABASE_NAME',
port: 1433,
options: {
instancename: 'SQLEXPRESS',
trustedconnection: true,
trustServerCertificate: true
},
}
export default async function ExcuteQuery(query, options) {
try {
let pool = await sql.connect(config);
let products = await pool.request().query(query);
return products.recordsets;
}
catch (error) {
console.log(error);
}
}
// api/hello.js
import ExcuteQuery from '../../utils/db';
export default async function handler(req, res) {
console.log(await ExcuteQuery('select * from tbl_category'));
res.status(200).json({})
}
this is the error:
ConnectionError: getaddrinfo ENOTFOUND .
at E:\0 - WEB\pos\node_modules\mssql\lib\tedious\connection-pool.js:70:17
at Connection.onConnect (E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1012:9)
at Object.onceWrapper (node:events:628:26)
at Connection.emit (node:events:513:28)
at Connection.emit (E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1040:18)
at E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1081:16
at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
code: 'EINSTLOOKUP',
originalError: ConnectionError: getaddrinfo ENOTFOUND .
at E:\0 - WEB\pos\node_modules\tedious\lib\connection.js:1081:32
at process.processTicksAndRejections (node:internal/process/task_queues:77:11) {
code: 'EINSTLOOKUP',
isTransient: undefined
}
}
also these are the settings changed:
I tried setting the server to MY_DESKTOP_NAME\SQLEXPRESS but it didn't work, but after disabling and reenabling all the settings it works now, it was kinda like a glitch in the system

ELOGIN Error with Node.js and SQL Server database Connection

I am trying to connect my SQL Database which is in sql server management studio to Node.js. Currently, the connection is being done locally on my machine. This is the error which I keep on getting:
undefined
[nodemon] clean exit - waiting for changes before restart
[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
ConnectionError: Login failed for user 'database access'.
at C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\mssql\lib\tedious\connection-pool.js:70:17
at Connection.onConnect (C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\tedious\lib\connection.js:1051:9)
at Object.onceWrapper (node:events:510:26)
at Connection.emit (node:events:390:28)
at Connection.emit (C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\tedious\lib\connection.js:1079:18)
at C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\tedious\lib\connection.js:2608:18
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ELOGIN',
originalError: ConnectionError: Login failed for user 'database access'.
at Login7TokenHandler.onErrorMessage (C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\tedious\lib\token\handler.js:245:19)
at Readable.<anonymous> (C:\Users\User\Desktop\PROGRAMMING\Aviation Calc\calc\node_modules\tedious\lib\token\token-stream-parser.js:26:33)
at Readable.emit (node:events:390:28)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Readable.push (node:internal/streams/readable:228:10)
at next (node:internal/streams/from:98:31)
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
code: 'ELOGIN',
isTransient: undefined
}
}
NOT CONNECTED
undefined
[nodemon] clean exit - waiting for changes before restart
At first I thought that this may be ocurring because I havent enabled windows firewall to allow my port: 1433 but even after doing that I kept on getting the same error message. I also made sure to enable the SQL Server Services in SQL Configuration Manager.
Here is my code:
dbConfig.js
const config = {
// dialect: 'mssql',
user: 'database access',
password: 'tronn6215',
// DESKTOP-NKHJ1GI
// SQLQuery1.sql
server: 'DESKTOP-NKHJ1GI',
database: 'Avtag Data',
options: {
trustServerCertificate: true,
trustedConnection: false,
enableArithAbort: true,
instancename: 'SQLEXPRESS'
},
// dynamic_port: 51582 (dynamic port num)
// port: 50381
port: 1433
//have enabled window firewall to allow static port 1433 for connection but still error
//Now error- code: 'ELOGIN'
}
module.exports = config;
dbOperation.js
const config = require('./dbConfig'),
sql = require('mssql');
const getEntryPoint = async() => {
try {
let pool = await sql.connect(config);
let entry = pool.request().query("SELECT * from ENTRY _POINT1")
console.log(entry);
console.log('CONNECTED')
return entry;
}
catch(error) {
console.log(error);
console.log('NOT CONNECTED')
}
}
module.exports = {
getEntryPoint
}
server.js
const express = require('express'),
dbOperation = require('./dbFiles/dbOperation'),
cors = require('cors');
dbOperation.getEntryPoint().then(res => {
console.log(res);
})

Problem connecting postgreSQL with Knex - assert.fail(`unknown message code: ${code.toString(16)}`)

I'm totally new to relational databases and I'm trying to build a node and express project with postgres using knex.
I'm getting the following error when trying to connect to postgres:
/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/parser.ts:202
assert.fail(`unknown message code: ${code.toString(16)}`)
^
AssertionError [ERR_ASSERTION]: unknown message code: 5b
at Parser.handlePacket (/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/parser.ts:202:16)
at Parser.parse (/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/parser.ts:103:30)
at Socket.<anonymous> (/home/German/Desktop/ger/code/projects/mixr/mixr-back/node_modules/pg-protocol/src/index.ts:7:48)
at Socket.emit (node:events:394:28)
at Socket.emit (node:domain:475:12)
at addChunk (node:internal/streams/readable:315:12)
at readableAddChunk (node:internal/streams/readable:289:9)
at Socket.Readable.push (node:internal/streams/readable:228:10)
at TCP.onStreamRead (node:internal/stream_base_commons:199:23) {
generatedMessage: false,
code: 'ERR_ASSERTION',
actual: undefined,
expected: undefined,
operator: 'fail'
}
I understand it's a connection problem, but I'm not sure why I'm getting this.
This is my connection code:
export const knex = require('knex')({
client: 'pg',
connection: {
host : 'localhost',
port : 3306,
user : 'notTheRealUser',
password : 'notTheRealPassword',
database : 'pgdb'
}
})
knex.raw("SELECT 1").then(() => {
console.log("PostgreSQL connected")
})
.catch((e: Error) => {
console.log("PostgreSQL not connected")
console.error(e)
})
And then I'm importing the Knex object on the different routes to make queries, like so:
import { knex } from '../db/db'
router.post('/register', async (req: Request, res: Response) => {
// Check if the email isn't already taken
try {
const emailIsTaken = await knex('users').where({ email: req.body.email })
if (emailIsTaken) return res.status(500).json('Email already used')
} catch (err) {
res.status(500).send(err)
console.error(err)
}
})
Full code can be found here: https://github.com/coccagerman/mixr-back
you are using MySQL port 3306,
PostgresQL uses port 5432

Connect to a remote server mongoDB in Docker via ssh in nodeJS using tunnel-ssh, AuthenticationFailed

I am trying to connect to a remote server MongoDB in Docker through ssh in Nodejs as below :
sshConfig = {
username: 'username',
password: 'password',
host: 'host',
port: 22,
dstHost: '172.17.0.3',
dstPort: 27017,
localPort: 5000
};
const uri = 'mongodb://admin:password#localhost:27017/admin';
tunnel(sshConfig, async error => {
if (error) {
throw new Error(`SSH connection error: ${error}`);
}
const client = new MongoClient(uri);
async function run() {
try {
// Connect the client to the server
await client.connect();
// Establish and verify connection
await client.db('admin').command({ ping: 1 });
console.log('Connected successfully to server');
} finally {
// Ensures that the client will close when you finish/error
await client.close();
}
}
await run().catch(console.dir);
});
But I am getting error as below :
MongoServerError: Authentication failed.
at MessageStream.messageHandler (/node_modules/mongodb/src/cmap/connection.ts:740:20)
at MessageStream.emit (node:events:390:28)
at MessageStream.emit (node:domain:475:12)
at processIncomingData (/node_modules/mongodb/src/cmap/message_stream.ts:167:12)
at MessageStream._write (/node_modules/mongodb/src/cmap/message_stream.ts:64:5)
at writeOrBuffer (node:internal/streams/writable:389:12)
at _write (node:internal/streams/writable:330:10)
at MessageStream.Writable.write (node:internal/streams/writable:334:10)
at Socket.ondata (node:internal/streams/readable:754:22)
at Socket.emit (node:events:390:28) {
ok: 0,
code: 18,
codeName: 'AuthenticationFailed'
},
and I open http://localhost:5000/ by browser, it shows that:
It looks like you are trying to access MongoDB over HTTP on the native driver port.
I can connect the database via:
Use MongoDB compass to connect the database via ssh
Use mongo 'mongodb://admin:password#remote-host:27017/admin' in local machine terminal
Use MongoClient(mongodb://admin:password#remote-host:27017/admin) in Nodejs without ssh-tunnel
Use mongo 'mongodb://admin:password#localhost:27017/admin' in both remote host and remote host docker contaniner
I am sure the password is correct.

How to make a SSH to postgres server with help of ssh2 library and query using pg libary?

I am trying to make a connection to Postgres remote host using the SSH tunnel from node application(library:ssh2) and query the data(lib:pg).
Before I used mysql2(node library) make a tunnel using ssh2(node library) and connected to Mysql remote host as well as able to query the data. But I want to do the same in Postgres remote host I am not able to connect to it.
pg Client obj config does not support the stream from ssh2 forwardOut..!
Or, Is there any library available to make this happen instead of that ssh2, pg
PS: In mysql2 when I try to make a connection with a mysql config with stream: stream key value.
var Clientssh = require('ssh2').Client;
var conn = new Clientssh();
conn.on('ready', function() {
console.log('Client :: ready');
conn.forwardOut(
'127.0.0.1',
5434,
'localhost',
5432,
function(err, stream) {
if (err) throw err;
let conf = {
host: 'localhost',
port: 5434,
user: 'test',
database: 'test',
password: 'test'
}
let remoteConnection = new Client(conf);
remoteConnection.connect(function(err) {
if (err) {
console.log(err);
console.log("Unable to connect to postgre");
res.send(err);
} else {
remoteConnection.query('SELECT * FROM test', function(err, testResult) {
remoteConnection.end();
if (err) {
console.log("Unable to fetch data");
res.send(err);
} else {
console.log("Succcess");
res.send(testResult);
}
});
}
});
});
}).connect({
host: 'hostaddress',
port: 'hostport',
username: 'hostusername',
privateKey: require('fs').readFileSync('path/for/key')
});
It shows the Connection terminated unexpectedly from this line
Client :: ready
Error: Connection terminated unexpectedly
at Connection.con.once (/node-postgres/node_modules/pg/lib/client.js:235:9)
at Object.onceWrapper (events.js:286:20)
at Connection.emit (events.js:198:13)
at Channel.<anonymous> (/node-postgres/node_modules/pg/lib/connection.js:131:10)
at Channel.emit (events.js:203:15)
at endReadableNT (_stream_readable.js:1129:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
Unable to connect to postgre

Resources