Ad

MongoDB Server Configuration

I have a MongoDB server running on my localhost. I wrote a simple Python program that reads/writes to the database using "localhost"; however, I want to give other clients access to my MongoDB server. For now, I am not concerned about access security and would like to grant access to anyone. How should I configure the Mongo Server to do this?

Here is the simple program connecting to localhost.

    from pymongo import MongoClient
    connection = MongoClient("Localhost")
    db = connection.hockey.players
    results = db.find()
    print()
    print('+-+-+-+-+-+-+-+-+-+-+-+-+-+-')
    for record in results:
        print(record['name'] + ',',record['position'])
    connection.close()

The error message I'm getting: File "C:/Users/Peter/PycharmProjects/Test/helloWorld.py", line 8, in for record in results: File "C:\Python34\lib\site-packages\pymongo\cursor.py", line 1097, in next if len(self.__data) or self._refresh(): File "C:\Python34\lib\site-packages\pymongo\cursor.py", line 1019, in _refresh self.__read_concern)) File "C:\Python34\lib\site-packages\pymongo\cursor.py", line 850, in __send_message **kwargs) File "C:\Python34\lib\site-packages\pymongo\mongo_client.py", line 777, in _send_message_with_response server = topology.select_server(selector) File "C:\Python34\lib\site-packages\pymongo\topology.py", line 142, in select_server address)) File "C:\Python34\lib\site-packages\pymongo\topology.py", line 118, in select_servers self._error_message(selector)) pymongo.errors.ServerSelectionTimeoutError: x.y.z.w:27017: timed out

Process finished with exit code 1

Below is my current localhost setup that works fine if I run it on my client which hosts the MongoDB. Current setup

Thanks

Ad

Answer

Give them your ip address and the port number (Default:27017) to connect to your server. Also edit the bindIp in the mongod.conf file as bindIp: 0.0.0.0. Ask them to connect to your database by using something like:

from pymongo import MongoClient
connection = MongoClient("mongodb://your_ip:yourport")

If you're on windows, create the configuration file mongod.cfg. And add the entry as

systemLog:
    destination: file
    path: c:\data\log\mongod.log
storage:
    dbPath: c:\data\db
net:
    bindIp: 0.0.0.0
    port: 27017

Make sure you've already created log and db folder or change the path where your data and log folder are located. Start the mongodb by specyfying thr configuration file.

Ad
source: stackoverflow.com
Ad