Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connect to MongoDB database using mongoose behind a proxy

I am using mongoose to connect to my database in mongolab in my server.js file :

mongoose.connect('mongodb://MyUsername:[email protected]:89702/todo'); 

When i launch my server with node server.js command, i see this error in my terminal

failed to connect to [ds089702.mongolab.com:89702]

I am very sure that is just a corporate proxy problem, so I'm wondering how can I connect to my database over the corporate proxy using mongoose ?

Thank you!

like image 326
Obama Avatar asked Nov 02 '15 17:11

Obama


People also ask

How do we connect Mongoose to our MongoDB database?

You can connect to MongoDB with the mongoose.connect() method. mongoose.connect('mongodb://localhost:27017/myapp'); This is the minimum needed to connect the myapp database running locally on the default port (27017). If connecting fails on your machine, try using 127.0.0.1 instead of localhost .

What is the difference between Mongoose connect and Mongoose createConnection?

My understanding on the official documentation is that generally when there is only one connection mongoose. connect() is use, whereas if there is multiple instance of connection mongoose. createConnection() is used.

Can you use Mongoose and MongoDB together?

Connecting to MongoDBMongoose requires a connection to a MongoDB database. You can require() and connect to a locally hosted database with mongoose. connect() as shown below (for the tutorial we'll instead connect to an internet-hosted database). You can get the default Connection object with mongoose.

Can I use Mongoose without schema?

To use Mongoose without defining a schema, we can define a field with the Mixed data type. const Any = new Schema({ any: Schema. Types. Mixed });


1 Answers

I had the same problem and figured out a solution that worked for me.

Example mongodb URI:

mongodb://:@ds123456.mlab.com:37409/dbName

  1. Use a proxy client (e.g. Proxifier).
  2. Create a HTTPS proxy through port 8080 using your corporate proxy as the address.

enter image description here

  1. Create a rule for mlab using your URI's port as the target port, which uses the HTTPS proxy you created above as its action.

enter image description here

  1. Prioritize your mlab rule high in the rules list.

enter image description here

like image 136
davejlin Avatar answered Oct 10 '22 06:10

davejlin