0

I am fairly new to MongoDB, and I wondering how can I establish multiple connections to a single Mongo instance without specifying ports or making a new config file for each user. I am running the Mongo instance in a singularity container on a remote server.

Here is my sample config file:

# mongod.conf

# for documentation of all options, see:
#  https://docs.mongodb.com/manual/reference/configuration-options/

# where to write logging data for debugging and such.
systemLog:
  destination: file
  logAppend: true
  path: /path-to-log/

# network interfaces
net:
  port: 27017
  bindIp: 127.0.0.1
  maxIncomingConnections: 65536

#security
security:
  authorization: 'enabled'

Do I need to use replica set? If so, can someone explain the concept behind a replica set?

Do I need to change my config file? If so, what changes do I need to make to allow for multiple connections?

Here is my code that I use to connect to the server (leaving out import statements for clarity):

    PWD = "/path-to-singularity-container/"
    os.chdir(PWD)
    self.p = subprocess.Popen(f"singularity run --bind {PWD}/data:/data/db mongo.sif --auth --config {PWD}/mongod.conf", shell=True, preexec_fn=os.setpgrp)

    connection_string = "mongodb://user:[email protected]:27017/"
    client = pymongo.MongoClient(connection_string, serverSelectionTimeoutMS=60_000)

EDIT: I am trying to have multiple people connect to MongoDB using pymongo at the same time given the same connection string. I am not sure how I can achieve this without giving each user a separate config. file.

Thank you for your help!

2
  • 1
    as many times you call this code. You'll connect with mongo. Make sure, you have enough ulimit settings to make more connections. For replica set, if you require HA solutions and want to split read from primary then you must go with replica set, Commented Jul 9, 2021 at 1:39
  • How do I make sure that I have ulimit settings? In the config file? What are HA solutions?. If you could answer these two questions and post an answer to this question then I will mark it as the accepted solution. Commented Jul 9, 2021 at 11:39

2 Answers 2

1

you can enough value of ulimit. Mongod tracks each incoming connection with a file descriptor and a thread.

you can go through the below link which will explain each component of ulimit parameters and their values.

https://docs.mongodb.com/manual/reference/ulimit/

For HA solutions, if you don't want downtime in your environment then you need to go with HA solution which means 3 nodes replica set which can afford one node down at a time.

If the primary node goes down, there will be internal voting and the new node will promote as new primary within seconds. So your application will be less impacted. Another benefit, if your node got crashed, you have another copy of data.

Hope this will answer your question.

Sign up to request clarification or add additional context in comments.

Comments

0

No special work is required, you simply create a client and execute queries.

1 Comment

I edited my post to be more specific. Do you know how can I have multiple users connecting to MongoDB using pymongo at the same time given the same connection string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.