1



The Problem

I'm getting started with MongoDB on Python, I have a Ubuntu machine in my local network and MongoDB is installed there. When I try to connect with database using Python from Mac it throughs me an error. I searched about it and found out there is a .service called mongod.service that needs to be started along with mongodb.service. But when I try to start the mongod.service the it says the .service doesn't even exist. I tried both with IP and mongodb url, nothing works.

Ubuntu Terminal

$ sudo service mongod start
$ Failed to start mongod.service: Unit mongod.service not found.
$ sudo systemctl start mongod
$ Failed to start mongod.service: Unit mongod.service not found.



DataBase Link (a)

mongodb://user:password@192.168.0.106/database

Python Script (a)

#!/usr/bin/env python3

from pymongo import MongoClient

client = MongoClient('mongodb://user:password@192.168.0.106/database')

db = client['database']

collection = db['collection']

json = dict(message='hello world', token=0)

collection.insert_one(json)

macOS Terminal (a)

pymongo.errors.ServerSelectionTimeoutError: 192.168.0.106:27017: [Errno 61] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 60e140982a43032aef0dd634, topology_type: Single, servers: [<ServerDescription ('192.168.0.106', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('192.168.0.106:27017: [Errno 61] Connection refused')>]>



DataBase Link (b)

mongodb+srv://user:password@cluster0.h9fmz.mongodb.net/database?retryWrites=true&w=majority

Python Script (b)

#!/usr/bin/env python3

from pymongo import MongoClient

client = MongoClient('mongodb+srv://user:password@cluster0.h9fmz.mongodb.net/database?retryWrites=true&w=majority')

db = client['database']

collection = db['collection']

json = dict(message='hello world', token=0)

collection.insert_one(json)

macOS Terminal (b)

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/pymongo/pool.py", line 1278, in _get_socket
    sock_info = self.sockets.popleft()
IndexError: pop from an empty deque

During handling of the above exception, another exception occurred:
.....
.....
.....
pymongo.errors.OperationFailure: bad auth : Authentication failed., full error: {'ok': 0, 'errmsg': 'bad auth : Authentication failed.', 'code': 8000, 'codeName': 'AtlasError'}



Note That

  • I'm providing the correct username and password for the database.
  • I'm using a machine on my local network, which is not a live server.
  • I've also tried the following commands but they did not solve anything.

Ubuntu Terminal

$ mongod --auth --port 27017
$ mongod --port 27017
$ sudo rm /var/lib/mongodb/mongod.lock
$ sudo mongod --repair



1 Answers1

3

For accessing mongodb from another machine in local network. You will need to check the following:

  1. There is no firewall restriction in the server machine or client machine. In case there is a firewall, you will need to add rule exceptions to allow this port to be accessible. Both incoming and outgoing. (Ubuntu firewall)

  2. You will have to add bindIp config to the mongodb config in server machine. Refer to docs here. You will need to add something like this:

    net:
       bindIp: 0.0.0.0
       port: 27017
    
  3. Make sure you are able to connect using this ip: 192.168.0.106(server in local network) from the server machine itself. This will make sure the server is listening in this ip.

$ Failed to start mongod.service: Unit mongod.service not found.

The solution for this error could be found here

The mongo atlas error might be due to the following reasons:

  1. You will have to create an database user in order to connect to mongodb. you can find it under the left panel -> Database access -> Add user

  2. This will be because of a mismatch with username and password. In case you have any special characters in your password you will have to url encode them.

Ashwin R
  • 744
  • 7
  • 14