Csv file to MOngoDB, with `pymongo.errors.ServerSelectionTimeoutError: Connection refused`

I’m running the script wish to save the csv file to MOngoDB, and face pymongo.errors.ServerSelectionTimeoutError: Connection refused

(ps. ‘1xx.xx.xx.1:27017’ is correct mongoDB ip)

  • the python script


import pandas as pd

from pymongo import MongoClient

client = MongoClient('1xx.xx.xx.1:27017')

db = client ['(practice_12_29)img_to_text-001_new']

collection = db ['img_to_text_listOfElems']

data = pd.read_csv('file_listOfElems.csv',encoding = 'UTF-8')

data_json = json.loads(data.to_json(orient='records'))


  • this is the output

jetson@jetson-desktop:~/Desktop/test_12.26$ python3 csv_to_mongoDB.py

Traceback (most recent call last):

  File "csv_to_mongoDB.py", line 13, in <module>


  File "/home/jetson/.local/lib/python3.6/site-packages/pymongo/collection.py", line 3182, in insert

    check_keys, manipulate, write_concern)

  File "/home/jetson/.local/lib/python3.6/site-packages/pymongo/collection.py", line 646, in _insert

    blk.execute(write_concern, session=session)

  File "/home/jetson/.local/lib/python3.6/site-packages/pymongo/bulk.py", line 511, in execute

    return self.execute_command(generator, write_concern, session)

  File "/home/jetson/.local/lib/python3.6/site-packages/pymongo/bulk.py", line 344, in execute_command

    with client._tmp_session(session) as s:

  File "/usr/lib/python3.6/contextlib.py", line 81, in __enter__

    return next(self.gen)

  File "/home/jetson/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1820, in _tmp_session

    s = self._ensure_session(session)

  File "/home/jetson/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1807, in _ensure_session

    return self.__start_session(True, causal_consistency=False)

  File "/home/jetson/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1760, in __start_session

    server_session = self._get_server_session()

  File "/home/jetson/.local/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1793, in _get_server_session

    return self._topology.get_server_session()

  File "/home/jetson/.local/lib/python3.6/site-packages/pymongo/topology.py", line 477, in get_server_session


  File "/home/jetson/.local/lib/python3.6/site-packages/pymongo/topology.py", line 205, in _select_servers_loop


pymongo.errors.ServerSelectionTimeoutError: 1xx.xx.xx.1:27017: [Errno 111] Connection refused

  • (tried 01)

I tried one of the similar issue’s solution,but is not work too

jetson@jetson-desktop:~/Desktop/test_12.26$ sudo rm /var/lib/mongodb/mongod.lock

rm: cannot remove '/var/lib/mongodb/mongod.lock': No such file or directory

  • (tried 02)

I also find this python - Mongodb pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused, Timeout: 30s, - Stack Overflow

but how do I know the <username> , <password> and cluster-details , my last time experience with other computer can just upload excel with below code

# this worked fine, that I don't remember I put user name and password 

import pandas as pd

from pymongo import MongoClient

aaa = pd.read_excel("T1_new.xls")


client = MongoClient('1xx.xx.xx.1:27017')

db = client['sample_data_in_DB']

collection = db['sample_collection']


if any idea just let me know,thanks

The error Connection Refused means one and only one thing. It means your client, that is your python code, cannot reach the server specified by the URI.

If your client cannot reach the server then
1 - there is not server listening at the given address port
2 - you have a firewall, vpn or other security measures stopping you from accessing the server

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.