John Washburne
John Washburne

Reputation: 23

Pymongo pymongo.errors.ServerSelectionTimeoutError when using example code

I am trying to run very straightforward code to figure out how to use pymongo with the MongoDB Atlas Cloud.

Here is the example code

import pymongo

client = pymongo.MongoClient("mongodb+srv://{myusername}:{mypassword}@cluster0-uywu8.mongodb.net/test?retryWrites=true&w=majority")
db = client.BroadwayMatch
print(db)
collection = db.Artists
print(collection)

print(collection.insert_one({'x': 1}))

BroadwayMatch and Artists are existing databases and collections that I was able to insert to last week, I am not sure what changed. It seems to be connecting to the database and collection successfully, but for some reason is unable to read or write to it. All attributes of collections can be accessed, but all methods result in a ServerSelectionTimeoutError. Here is the output from this snippet

Database(MongoClient(host=['cluster0-shard-00-01-uywu8.mongodb.net:27017', 'cluster0-shard-00-00-uywu8.mongodb.net:27017', 'cluster0-shard-00-02-uywu8.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, authsource='admin', replicaset='Cluster0-shard-0', ssl=True, retrywrites=True, w='majority'), 'BroadwayMatch')
Collection(Database(MongoClient(host=['cluster0-shard-00-01-uywu8.mongodb.net:27017', 'cluster0-shard-00-00-uywu8.mongodb.net:27017', 'cluster0-shard-00-02-uywu8.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, authsource='admin', replicaset='Cluster0-shard-0', ssl=True, retrywrites=True, w='majority'), 'BroadwayMatch'), 'Artists')
Traceback (most recent call last):
  File "C:\Python37\Spotify-Match\mongotest.py", line 10, in <module>
    print(collection.insert_one({'x': 1}))
  File "C:\Python37\lib\site-packages\pymongo\collection.py", line 700, in insert_one
    session=session),
  File "C:\Python37\lib\site-packages\pymongo\collection.py", line 614, in _insert
    bypass_doc_val, session)
  File "C:\Python37\lib\site-packages\pymongo\collection.py", line 602, in _insert_one
    acknowledged, _insert_command, session)
  File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1279, in _retryable_write
    with self._tmp_session(session) as s:
  File "C:\Python37\lib\contextlib.py", line 112, in __enter__
    return next(self.gen)
  File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1611, in _tmp_session
    s = self._ensure_session(session)
  File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1598, in _ensure_session
    return self.__start_session(True, causal_consistency=False)
  File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1551, in __start_session
    server_session = self._get_server_session()
  File "C:\Python37\lib\site-packages\pymongo\mongo_client.py", line 1584, in _get_server_session
    return self._topology.get_server_session()
  File "C:\Python37\lib\site-packages\pymongo\topology.py", line 434, in get_server_session
    None)
  File "C:\Python37\lib\site-packages\pymongo\topology.py", line 200, in _select_servers_loop
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: connection closed,connection closed,connection closed

I'm not sure what I am doing wrong, can anyone help?

Upvotes: 2

Views: 9166

Answers (2)

Joe Drumgoole
Joe Drumgoole

Reputation: 1348

Reasons you might not be able to connect to an Atlas server:

  • Your white list has not enable your current IP address
  • you are using the wrong username and/or password. In your case it looks like your fString is missing an f at the start.

When diagnosing these conditions cutting and pasting the MongoDB Atlas connection string (see below) for the MongoDB shell or MongoDB Compass can often expose username and/or password errors.

Screenhost of MongoDB Atlas Connection screen dialog for MongoDB Shell

Upvotes: 3

Swagat Panda
Swagat Panda

Reputation: 11

Either your mongo server is not exposed otherwise it is not in the default port. Try following:

import pymongo
client = pymongo.MongoClient("mongodb://uname:pass@ip:port/")
db = client['BroadwayMatch']

Upvotes: 1

Related Questions