With pymongo >= 4 the document way to initiate a replica set does not work anymore.
When starting a fresh single-member MongoDB replica set using the docker container:
podman run --rm -p 27777:27017 docker.io/mongo:4.1 mongod --replSet rep0
With pymongo 3.12.3 the replica set can successfully initiated with the following code:
In [1]: from pymongo import MongoClient In [2]: client = MongoClient("localhost", 27777) In [3]: client.admin.command("replSetInitiate") Out[3]: {'info2': 'no configuration specified. Using a default configuration for the set', 'me': '97b8f1356c65:27017', 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1639392790, 1), 'signature': {'hash': b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00', 'keyId': 0}}, 'operationTime': Timestamp(1639392790, 1)} In [4]: import pymongo In [5]: pymongo.__version__ Out[5]: '3.12.3'
This happens with pymongo 4.0.1:
In [1]: from pymongo import MongoClient In [2]: client = MongoClient("localhost", 27777) In [3]: client.admin.command("replSetInitiate") --------------------------------------------------------------------------- ServerSelectionTimeoutError Traceback (most recent call last)<ipython-input-3-5b449124b342> in <module>----> 1 client.admin.command("replSetInitiate") /tmp/env/lib/python3.10/site-packages/pymongo/database.py in command(self, command, value, check, allowable_errors, read_preference, codec_options, session, **kwargs) 592 read_preference = ((session and session._txn_read_preference()) 593 or ReadPreference.PRIMARY)--> 594 with self.__client._socket_for_reads( 595 read_preference, session) as (sock_info, secondary_ok): 596 return self._command(sock_info, command, secondary_ok, value, /usr/lib/python3.10/contextlib.py in __enter__(self) 133 del self.args, self.kwds, self.func 134 try:--> 135 return next(self.gen) 136 except StopIteration: 137 raise RuntimeError("generator didn't yield") from None /tmp/env/lib/python3.10/site-packages/pymongo/mongo_client.py in _socket_for_reads(self, read_preference, session) 1175 # Thread safe: if the type is single it cannot change. 1176 topology = self._get_topology()-> 1177 server = self._select_server(read_preference, session) 1178 single = topology.description.topology_type == TOPOLOGY_TYPE.Single 1179 /tmp/env/lib/python3.10/site-packages/pymongo/mongo_client.py in _select_server(self, server_selector, session, address) 1135 % address) 1136 else:-> 1137 server = topology.select_server(server_selector) 1138 return server 1139 except PyMongoError as exc: /tmp/env/lib/python3.10/site-packages/pymongo/topology.py in select_server(self, selector, server_selection_timeout, address) 240 address=None): 241 """Like select_servers, but choose a random server if several match."""--> 242 servers = self.select_servers( 243 selector, server_selection_timeout, address) 244 if len(servers) == 1: /tmp/env/lib/python3.10/site-packages/pymongo/topology.py in select_servers(self, selector, server_selection_timeout, address) 198 199 with self._lock:--> 200 server_descriptions = self._select_servers_loop( 201 selector, server_timeout, address) 202 /tmp/env/lib/python3.10/site-packages/pymongo/topology.py in _select_servers_loop(self, selector, timeout, address) 214 # No suitable servers. 215 if timeout == 0 or now > end_time:--> 216 raise ServerSelectionTimeoutError( 217 "%s, Timeout: %ss, Topology Description: %r" % 218 (self._error_message(selector), timeout, self.description)) ServerSelectionTimeoutError: No servers match selector "Primary()", Timeout: 30s, Topology Description: <TopologyDescription id: 61b725c1757d60fe06ed33ef, topology_type: Unknown, servers: [<ServerDescription ('localhost', 27777) server_type: RSGhost, rtt: 0.0013525948277739105>]> In [4]: import pymongo In [5]: pymongo.__version__ Out[5]: '4.0.1'