I am building A MERN stack app on linux. The internet only work when we use proxy server of college. I am using mongoose for creating connection with MongoDB Atlas. The issue is that mongoose is unable to connect to MongoDB although i already set the proxy. How can I create connection in mongoose through university proxy server with MongoDB Atlas.
Currently mongoose connection is setup like this:
const CONNECTION_URL =process.env.CLUSTER_URL;
const PORT = process.env.PORT || 3000;
mongoose
.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true })
.then(() =>
app.listen(PORT, () => {
console.log(`server running at PORT ${PORT}`);
}),
)
.catch(error => console.log(error));
Currently university is using HTTP_PROXY=http://172.16.100.7:3022.
I am trying to connect to cloud mongo db using mongoose with the connection string
const mongoose = require("mongoose");
mongoose.connect(
"mongodb+srv://<user>:<pass>#<cluster>/<db>?retryWrites=true&w=majority", {
useNewUrlParser: true,
useUnifiedTopology: true
}
).then(res => {
console.log("successful" + res);
})
.catch(err => {
console.log("connection error:", err);
})
module.exports = mongoose;
This works fine on my local machine but when I upload and run it on my production server it doesn't connect.
I have to set it to allow connections from all IPs and I have also add my server IP to it but still it shows me the same error
I have a node.js express api which I host on heroku. It connects to mongodb atlas via mongoose as follows
mongoose.connect(
`mongodb+srv://${process.env.MONGO_USER}:${process.env.MONGO_PWD}#${process.env.MONGO_HOST}/${process.env.MONGO_DEFAULT_DB}?retryWrites=true`, {
useNewUrlParser: true,
autoReconnect: true,
keepAlive: 300000,
connectTimeoutMS: 300000,
socketTimeoutMS: 300000
}
)
.then(result => {
console.log('Connected and listening to requests!');
app.listen(process.env.PORT || 3000);
.catch(err => console.log(err));
I want to use Atlas MongoDB Cloud's whitelist for the Heroku app, instead of opening access up to all. Heroku doesn't have fixed a IP address but makes them possible via an add-on called Fixie Socks, which acts as a proxy for outbound traffic.
How can I use this add-on to get the connection to work? The documentation gives several examples on how to connect to other services and databases, but there is no help on mongodb. All examples use the FIXIE_SOCKS_HOST which contains the SOCKSV5 proxy URL, user, pass and port, but I'm at a loss on how to use it in conjunction with the mongoose.connect method.
This question has been asked before, but when a different add-on was used (Fixie vs Fixie Socks), which didn't work.
I tried Fixie Socks but couldn't get it to work. But managed to successfully connect to Mongodb Atlas database with Quota Guard's static IP addresses following this documentation:
https://support.quotaguard.com/support/solutions/articles/12000066411-how-to-connect-to-mongodb-using-quotaguard
The trick is to use the use mongodb atlas' connection string without +srv command. To do that use an older driver version and it will provide you a connection string with 3 replica servers (each with 27017 port). Then create 3 tunnels at the Quota Guard dashboard.
Make sure you download the gqtunnel package and extract it your app's root folder. Then you just have to add the bin/qgtunnel to your procfile.
Example:
web: bin/qgtunnel your-application your arguments
I manage to connect from Heroku + node.js + Fixie SOCK add-on to MongoDB Atlas. A few parameters in MongoClient need to be set to direct MongoDB traffic to Fixie proxy. This is the snippet:
const {MongoClient, ServerApiVersion} = require('mongodb');
const username = process.env.USERNAME;
const password = process.env.PASSWORD;
const host = process.env.MONGO_DB_HOST;
const uri = "mongodb+srv://" + username + ":" + password + "#" + host + "/?retryWrites=true&w=majority";
const fixieData = process.env.FIXIE_SOCKS_HOST.split(new RegExp('[/(:\\/#/]+'));
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverApi: ServerApiVersion.v1,
proxyUsername: fixieData[0];
proxyPassword: fixieData[1];
proxyHost: fixieData[2];
proxyPort: fixieData[3];
});
Some of the MongoDB client connection options can be found here:
https://mongodb.github.io/node-mongodb-native/4.5/interfaces/ConnectionOptions.html
With modern versions of Mongoose, you can include the proxy-related driver options in the options object on mongoose.connect, and this works both with and without the +srv URL modifier.
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')
}
}
)
I've checked Procfile, package.json. Nothing seems to solve the problem.
My code for mongoose connection:
mongoose
.connect(process.env.uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
})
.then(() => {
console.log("DB connected");
})
.catch((err) => {
console.log(`DB connection error: ${err.message}`);
});
after heroku logs --tail
I had a similar problem, and I solved the issue by changing networking access to 'ALLOW ACCESS FROM ANYWHERE' on MongoDB.
I am trying to connect a MongoDB atlas instance to a nodejs/express server :
const mongoose = require("mongoose");
const dbURI =
"mongodb+srv://(url copied from atlas connect)";
const options = {
useNewUrlParser: true,
dbName: "data"
};
mongoose.connect(dbURI, options).then(
() => {
console.log("Database connection established!");
},
err => {
console.log("Error connecting Database instance due to: ", err);
}
);
But I keep get the following error:
MongoNetworkError: connection 5 to cluster ... 27017 closes at TLSSocket. ...
How can I fix this?
Resolved -- Check IP whitelist!