0

This seems like an old solved problem here and here and here but Still I am getting this error.I create my db on Docker.And It worked only one time.Before this, I re-created db, did "connect =false",added wait, downgraded pymongo, did previous solutions etc. I stuck.

Python 3.8.0, Pymongo 3.9.0

from pymongo import MongoClient
import pprint

client = MongoClient('mongodb://192.168.1.100:27017/',
                      username='admin',
                      password='psw',
                      authSource='myappdb',
                      authMechanism='SCRAM-SHA-1',
                      connect=False)

db = client['myappdb']
serverStatusResult=db.command("serverStatus")
pprint(serverStatusResult)

and I am getting ServerSelectionTimeoutError

Traceback (most recent call last): File "C:\Users\ME\eclipse2019-workspace\exdjango\exdjango__init__.py", line 12, in serverStatusResult=db.command("serverStatus") File "C:\Users\ME\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\database.py", line 610, in command with self.client._socket_for_reads( File "C:\Users\ME\AppData\Local\Programs\Python\Python38-32\lib\contextlib.py", line 113, in __enter return next(self.gen) File "C:\Users\ME\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\mongo_client.py", line 1099, in _socket_for_reads server = topology.select_server(read_preference) File "C:\Users\ME\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\topology.py", line 222, in select_server return random.choice(self.select_servers(selector, File "C:\Users\ME\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\topology.py", line 182, in select_servers server_descriptions = self._select_servers_loop( File "C:\Users\ME\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pymongo\topology.py", line 198, in _select_servers_loop raise ServerSelectionTimeoutError( pymongo.errors.ServerSelectionTimeoutError: 192.168.1.100:27017: timed out

Belly Buster
  • 8,224
  • 2
  • 7
  • 20
yardımcı Etis
  • 140
  • 2
  • 12

1 Answers1

0

Your connection looks a little misconfigured. Firstly you have half connection string, half parameter format. I'd suggest you stick with one or the other.

Your auth database is usually seperate to your actual databases (and it's usually called admin). Check this is correct.

There's no particular need to specify the authMechanism assuming you are using MongoDB 3.0 or later.

The connect=False is likely a red herring.

So I would try one of either:

client = MongoClient('mongodb://admin:psw@192.168.1.100:27017/myappdb?authSource=admin')

or

client = MongoClient(host='192.168.1.100',
                     port=27017,
                     username='admin',
                     password='psw',
                     authSource='admin')
Belly Buster
  • 8,224
  • 2
  • 7
  • 20
  • thank you for response. I don't think parsing is the problem and I tried anyway.I search and find this. [link](https://api.mongodb.com/python/current/faq.html#is-pymongo-fork-safe).maybe I should remove from docker and try it at local. – yardımcı Etis Nov 25 '19 at 14:23
  • 1
    If you use the URL format it becomes easier to troubeshoot from the shell with `mongo mongodb://admin:psw@192.168.1.100:27017/myappdb?authSource=admin` – Belly Buster Nov 25 '19 at 16:25