title: pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] when trying to do basic MongoDB query
body: <p>I'm currently trying to implement a user creation &amp; login system in my Flask webapp using MongoDB Atlas (so it's a cloud server which I think makes a big difference for this error), mongoengine, and pymongo (all latest versions as of this post). Whenever I submit the username and password on the create-user form and try to validate that the username doesn't already exist by querying the database, I get this error message:</p>
  <pre><code>pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it
  </code></pre>
  <p>And here is the full traceback:</p>
  <pre><code>    File &quot;C:\Programming\Python\Lib\site-packages\flask\app.py&quot;, line 2463, in __call__
      return self.wsgi_app(environ, start_response)
    File &quot;C:\Programming\Python\Lib\site-packages\flask\app.py&quot;, line 2449, in wsgi_app
      response = self.handle_exception(e)
    ...
      rv = self.dispatch_request()
    File &quot;C:\Programming\Python\Lib\site-packages\flask\app.py&quot;, line 1935, in dispatch_request
      return self.view_functions[rule.endpoint](**req.view_args)
    File &quot;C:\Programming\Khimbus\src\client\views\user_views.py&quot;, line 27, in create_user
      account_exists = find_user_by_username(username)
    File &quot;C:\Programming\Khimbus\src\services\user_service.py&quot;, line 20, in find_all_users
      users = User.objects()
    File &quot;C:\Programming\Python\Lib\site-packages\mongoengine\queryset\manager.py&quot;, line 37, in __get__
      queryset = queryset_class(owner, owner._get_collection())
    File &quot;C:\Programming\Python\Lib\site-packages\mongoengine\document.py&quot;, line 207, in _get_collection
      db.client.is_primary:
    File &quot;C:\Programming\Python\Lib\site-packages\pymongo\mongo_client.py&quot;, line 1006, in is_primary
      return self._server_property('is_writable')
    File &quot;C:\Programming\Python\Lib\site-packages\pymongo\mongo_client.py&quot;, line 830, in _server_property
      server = self._topology.select_server(
    File &quot;C:\Programming\Python\Lib\site-packages\pymongo\topology.py&quot;, line 229, in select_server
      return random.choice(self.select_servers(selector,
    File &quot;C:\Programming\Python\Lib\site-packages\pymongo\topology.py&quot;, line 188, in select_servers
      server_descriptions = self._select_servers_loop(
    File &quot;C:\Programming\Python\Lib\site-packages\pymongo\topology.py&quot;, line 204, in _select_servers_loop
  </code></pre>
  <p>The REST endpoint:</p>
  <pre><code>    @user_views.route('/create-user', methods=['GET', 'POST'])
      def create_user():
      if request.method == 'POST':
          print('Creating user...')
          username = request.form['username']
          password = request.form['password']
  
          account_exists = find_user_by_username(username)
          if account_exists:
              print(f'Error: Account with {username} already exists.')
              return render_template('error_page.html')
  
          hashpass = generate_password_hash(password)
          user_state.active_account = create_user(username, hashpass)
          print(user_state.active_account.username)
          session['username'] = user_state.active_account.username
          resp = jsonify('Created new account!')
          resp.status_code = 200
          return resp
  
      return render_template('create_user.html')
  </code></pre>
  <p>Find user method:</p>
  <pre><code>def find_user_by_username(username: str) -&gt; User:
      print('Finding user...')
      user = User.objects(username=username)
      print('Found user' + user)
      return user
  </code></pre>
  <p>I'm not sure what's going on here, and other related questions cover a different error code or are too outdated to help me with MongoDB Atlas. I would appreciate any and all information!</p>
  <h2>Update: Solved</h2>
  <p>When I installed MongoDB, I didn't install it as a Windows service. I reinstalled it as such and user creation is now working fine.</p>