Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pymongo.errors.ServerSelectionTimeoutError:localhost:27017:[WinError 10061]No connection could be made because the target machine actively refused it

I am following the tutorial python from w3schools. I just started the mongoDB chapter. I installed mongoDB and checked it with:

import pymongo 

without getting an error.

But as soon as I enter the following code:

import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydict = { "name": "John", "address": "Highway 37" }

x = mycol.insert_one(mydict)

print(x.inserted_id)

I get these messages and an error message at the bottom in cmd:


    C:\Users\xxx\myname

    C:\Users\xxx\myname>python index.py
    Traceback (most recent call last):
      File "index.py", line 8, in <module>
        x = mycol.insert_one(mydict)
      File "C:\Users\path...\pymongo\collection.py", line 695, in insert_one
        self._insert(document,
      File "C:\Users\path...\pymongo\collection.py", line 610, in _insert
        return self._insert_one(
      File "C:\Users\path...\pymongo\collection.py", line 599, in _insert_one
        self.__database.client._retryable_write(
      File "C:\Users\path...\pymongo\mongo_client.py", line 1490, in _retryable_write
        with self._tmp_session(session) as s:
      File "C:\Program Files\WindowsApps\path...\lib\contextlib.py", line 113, in __enter__
        return next(self.gen)
      File "C:\Users\path...\pymongo\mongo_client.py", line 1823, in _tmp_session
        s = self._ensure_session(session)
      File "C:\Users\path...\pymongo\mongo_client.py", line 1810, in _ensure_session
        return self.__start_session(True, causal_consistency=False)
      File "C:\Users\path...\pymongo\mongo_client.py", line 1763, in __start_session
        server_session = self._get_server_session()
      File "C:\Users\path...\pymongo\mongo_client.py", line 1796, in _get_server_session
        return self._topology.get_server_session()
      File "C:\Users\path...\pymongo\topology.py", line 482, in get_server_session
        self._select_servers_loop(
      File "C:\Users\path...\pymongo\topology.py", line 208, in _select_servers_loop
        raise ServerSelectionTimeoutError(
    pymongo.errors.ServerSelectionTimeoutError: localhost: 27017: [WinError 10061] Could not connect because target computer actively refused connection

    C:\Users\xxx\myname

I also tried too disabling firewall temporarily but the error kept coming up.

I used:

"python 3.8.2
, mongoDB 4.2.5.0
, pymongo 3.10.1
, windows 10 home"

Does anyone have an idea what is going wrong?

like image 653
splintermark77 Avatar asked Mar 24 '20 21:03

splintermark77


2 Answers

There is nothing wrong with your code.

If you have disabled your firewall, the most likely reason is that the MongoDB Service is not installed or running. On windows press the windows key and type services to open the services App. Check the service MongoDB Server is listed and has a Running status.

You can test local connectivity by opening your favourite windows terminal or powershell and typing mongo. If it is working you should see:

PS> mongo
MongoDB shell version v4.2.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1b5499b8-166a-4de6-a8c9-643499f04e66") }
MongoDB server version: 4.2.3
>
like image 147
Belly Buster Avatar answered Sep 26 '22 23:09

Belly Buster


Try running this command in CLI:

mongod

If you see any error, then watch this video
https://www.youtube.com/watch?v=FwMwO8pXfq0
The video is called "How to Install MongoDB on Windows 10"
This video was helpful

like image 33
Omar Magdy Avatar answered Sep 23 '22 23:09

Omar Magdy