<p>Starting MongoDB with this connection string <code>mongodb://mongo-service:27017</code>  is giving me this error.</p>
<p>The issue only happens for me when running on Kubernetes. Localhost is working fine.</p>
<pre><code>ERROR:src.app:Exception on /api/polls [GET]
Traceback (most recent call last):
  File &quot;/.venv/lib/python3.10/site-packages/flask/app.py&quot;, line 2077, in wsgi_app
    response = self.full_dispatch_request()
  File &quot;/.venv/lib/python3.10/site-packages/flask/app.py&quot;, line 1525, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File &quot;/.venv/lib/python3.10/site-packages/flask/app.py&quot;, line 1523, in full_dispatch_request
    rv = self.dispatch_request()
  File &quot;/.venv/lib/python3.10/site-packages/flask/app.py&quot;, line 1509, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File &quot;/home/appuser/src/app.py&quot;, line 28, in get_all
    polls = list(repository.get_all_polls())
  File &quot;/home/appuser/src/repository.py&quot;, line 11, in get_all_polls
    return _get_collection().find()
  File &quot;/home/appuser/src/repository.py&quot;, line 28, in _get_collection
    return mongo.get_collection(DATABASE, COLLECTION)
  File &quot;/home/appuser/src/mongo.py&quot;, line 15, in get_collection
    return MongoClient(connection_string).get_database(database).get_collection(collection)
  File &quot;/.venv/lib/python3.10/site-packages/pymongo/mongo_client.py&quot;, line 704, in __init__
    res = uri_parser.parse_uri(
  File &quot;/.venv/lib/python3.10/site-packages/pymongo/uri_parser.py&quot;, line 568, in parse_uri
    nodes = split_hosts(hosts, default_port=default_port)
  File &quot;/.venv/lib/python3.10/site-packages/pymongo/uri_parser.py&quot;, line 376, in split_hosts
    nodes.append(parse_host(entity, port))
  File &quot;/.venv/lib/python3.10/site-packages/pymongo/uri_parser.py&quot;, line 137, in parse_host
    raise ValueError(&quot;Port must be an integer between 0 and 65535: %s&quot; % (port,))
ValueError: Port must be an integer between 0 and 65535: 27017
</code></pre>
<p>I couldn't find the reason in the <a href="https://www.mongodb.com/docs/manual/reference/connection-string/#standard-connection-string-format" rel="nofollow noreferrer">docs</a>.</p>

Evandro Pomatti
<p>As it is required by the Kubernetes manifest schema, Secret values must be encoded in Base 64, which I was doing like this:</p>
<pre class="lang-sh prettyprint-override"><code>$ echo 'mongodb://mongo-service:27017' | base64
bW9uZ29kYjovL21vbmdvLXNlcnZpY2U6MjcwMTcK
</code></pre>
<p>This was adding a line-break at the end, modifying the original string content, and breaking pymongo interpretation of the connection string. Tricky.</p>
<pre class="lang-sh prettyprint-override"><code>mongodb://mongo-service:27017

</code></pre>
<p>In my case the solution was to change to <code>printf</code> as pointed out in <a href="https://stackoverflow.com/a/51143063/3231778">this answer</a>:</p>
<pre class="lang-sh prettyprint-override"><code>$ printf mongodb://mongo-service:27017 | base64
bW9uZ29kYjovL21vbmdvLXNlcnZpY2U6MjcwMTc=
</code></pre>
<p>A new <a href="https://github.com/mongodb/mongo-python-driver/pull/937" rel="nofollow noreferrer">PR937</a> was created proposing a new message format which can outline this behavior.</p>
