Connect to MongoDB using mongoose with proxy - node.js

Goal : Connect to a public facing mongodb instance in my local development environment, which is connected to my corporate network.
Background : There is a proxy available for bypassing this corporate firewall. I am developing a Next JS application with mongo as the backend.
Ask : How do I use this proxy to connect to the mongo instance in my node js application?
const db = await mongoose
.connect(process.env.NEXT_PUBLIC_MONGO_URI, {
dbName: "cusum",
useCreateIndex: true,
useFindAndModify: false,
useNewUrlParser: true,
useUnifiedTopology: true,
})
.catch((err) => console.log(err.reason));
This is how I connect to my mongo db instance using mongoose, in my node js application. It works when not connected to my corporate network, but times out when connected to my corporate network.
I was able to connect to the instance using Studio 3T with its built in proxy support, however this is just a GUI client for mongo db.
Attempts
I have attempted to use the global-tunnel package to route all http connections to the proxy but it didn't work.
globalTunnel.initialize({
host: "proxy-xx.xxx.com",
port: 8080,
protocol: "http:",
connect: "both",
});

I hope this link can help on a corporate proxy issue
Connect to MongoDB database using mongoose behind a proxy
Also, you can look into Nginx as a proxy, depending on your company configuration, though I see you're working wit Nextjs, most of it will depend on the configuration used at your company

Related

VPS not respond to request MongoDB-NodeJS

I bought a VPS server and installed Nodejs and Mongodb in it. Then I transferred my existing database file to mongodb, which I set up on VPS. I made the connection via NodeJS. The database seems to be working, but when I make a request, for example http://serverip:port/fetchData, I get the following error
'It looks like you are trying to access MongoDB over HTTP on the native driver port.'
could you please help me to solve this problem?
Here is NodeJs connection code
const URL = "mongodb://serverIp:port/dbname";
const connection = mongoose
.connect(URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 5000,
family:4
})
.then(() => console.log("Successfully Connected DB"))
.catch((e) => console.log("DB CONNECTION ERROR: "+e));
module.export = connection;
Note --> I added my desktop ip to access mongodb database and I can easily manage with my own desktop
HTTP interface for MongoDB Deprecated since version 3.2 :)
Check Mongo Docs: HTTP Status Interface

Why does local Robo3t connect to a remote Mongo DB instance, but NodeJs fails with IP whitelist error?

I am having issues connecting to MongoDb running remotely, and the connection error response I am getting from the server is somewhat weird.
My network access whitelist is set to allow all (0.0.0.0/0). Hence, my local robo3t installation was able to connect. However, I could not connect from my NodeJs code. Error is: "MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas cluster's IP whitelist"
IP whitelist seems to be an unlikely error, given that my local robo3t client is able to connect remotely to the same remote Mongo Atlas instance, as IP whitelist is allow-all.
How do I debug this kind of thing, please?
UPDATE: this is how I connect to MongoDb. Works well on local, too.
try {
const connectionString =
process.env.APP_ENV == "test"
? await getInMemoryMongoDbAdapter()
: `mongodb://${process.env.MONGODB_HOSTNAME}:${process.env.MONGODB_PORT}/${process.env.CBT_DATABASE_NAME}`;
logger.info(`Connecting to MongoDB service: ${connectionString}`);
await mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
} catch (error) {
reject(error);
}
The logger line correctly shows: Connecting to MongoDB service: mongodb://<user>:<password>#cluster0-xxx.yyy.zzz.net:<port>/<database>
UPDATE 2:
My localhost also does not connect via this node app; whereas my robo3t (local MongoDb client) connects. I guess that means Heroku-specific issues can now be comfortably ruled out
A decade later, I found that for the connection parameters, I needed to supply the authSource and ssl options, as below:
{
useNewUrlParser: true,
useUnifiedTopology: true,
authSource: "admin",
ssl: true,
}
Neither one works without the other. Big shout-out to #darklightcode for all the insights he gave, leading me to dig deeper. Thanks man!

MongoNetworkError: failed to connect to server on first connect [MongoNetworkError: connect ECONNREFUSED ]

I got this error as soon as I tried to run the nodejs app in Godaddy shared hosting cPanel terminal while trying to connect to mongo atlas cluster.
I have whitelisted both the IP of the server and set access to all still not working.
Same code is working fine in local system
My node.js code goes as:
Here is the DB url
If anyone has experienced this issue and able to solve it, then please provide me the solution. Thanks :) in advance.
I am pretty sure, that you IP is a provider's IP, not you own. According to a console, problem is in whitelisting.
So make sure that you are enable in your driver, all connections options, like this:
(if you are using mongoose)
connect(`mongodb+srv://${process.env.login}:${process.env.password}#${process.env.hostname}/${process.env.auth_db}`, {
useNewUrlParser: true,
useFindAndModify: false,
useUnifiedTopology: true,
w: "majority",
family: 4
});
And then white-list every IP in your Atlas console, like that:

How to recover nodejs service from MongoError: server instance pool was destroyed

I have build node js backend service using mongoose(v 5.6.9). Service is connected to Mongo Atlas DB(Version 4.2.6). Service is deployed to AWS Elastic Beanstalk. The service is working fine for a couple of weeks.
But then I am getting "MongoError: server instance pool was destroyed". After that I do any DB operation then it errors with "MongoTimeoutError: Server selection timed out after 30000 ms".
Below the option, I have used for mongoose connect. My connection URL looks like mongodb+srv://...
await mongoose.connect(URL, {
useNewUrlParser: true,
useFindAndModify: false,
useCreateIndex: true,
useUnifiedTopology: true,
});
Is there any way that I should use and prevent this error or recover from this situation?
Also, I am not sure how to reproduce this error.

Connecting to mongodb using mongoose and Fixie (Heroku add-on)

I have a mongodb database hosted on an Atlas MongoDB Cloud cluster. I'm currently accessing the database in my node.js application using mongoose:
mongoose.connect("mongodb://user:pw#cluster0-shard-00-00-***.mongodb.net:***,cluster0-shard-00-01-***.mongodb.net:***,cluster0-shard-00-02-***.mongodb.net:***/admin?ssl=true&replicaSet=Cluster0-shard-0&authSource=admin");
Because Atlas MongoDB Cloud have a whitelist, and Heroku doesn't provide the possibility to obtain a fixed IP address for my application, I'm using Fixie add-on. Basically Fixie acts as a proxy for outbound traffic.
This way, I can request resources via HTTP, which are tunneled through a fixed IP address provided by Fixie. But what I do need is to connect to the Atlas Cloud cluster using Fixie's proxy, in order to retrieve and modify data from the database.
Can it be done using mongoose?
The mongoose.connect function accepts an option parameter, but I couldn't find any option regarding the establishment of a connection through a proxy.
Just got a reply from Fixie's team:
Fixie is an http/https proxy, so it won't work for lower-level TCP connections like you'd need for your mongodb connection
When I asked about the possibility of using SOCKS for this case, they replied:
It looks like mongoose does not natively support socks proxies, and it does not accept a custom socket (which is how Node database drivers for MySQL and Postgres support it).
So apparently, in my case, there is no way to establish a connection to MongoDB Atlas cluster using mongoose through the proxy solution offered by Fixie (Heroku Add-on).
A lot has changed since this question was originally asked in 2017. Since then, Mongoose has added support for proxy options, and Fixie released Fixie Socks, a SOCKS5 proxy designed for proxying database connections.
To connect to a MongoDB server (including MondoDB Atlas) through Fixie Socks, you can do the following:
const mongoose = require('mongoose');
const fixieData = process.env.FIXIE_SOCKS_HOST.split(new RegExp('[/(:\\/#/]+'));
mongoose.connect(process.env.DB_CONNECTION,
{
proxyUsername: fixieData[0],
proxyPassword: fixieData[1],
proxyHost: fixieData[2],
proxyPort: fixieData[3]
},
(error) => {
if(error){
console.log(error);
} else {
console.log('Connected to database');
}
}
);

Resources