我目前正在尝试使用MongoDB Atlas(因此我认为这是一个云服务器,对于该错误起很大的作用),mongoengine和pymongo(所有最新版本为)在Flask Web应用程序中实现用户创建和登录系统的帖子)。每当我在创建用户表单上提交用户名和密码并尝试通过查询数据库来验证用户名不存在时,都会收到以下错误消息:
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [WinError 10061] No connection could be made because the target machine actively refused it
这是完整的追溯:
File "C:\Programming\Python\Lib\site-packages\flask\app.py", line 2463, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Programming\Python\Lib\site-packages\flask\app.py", line 2449, in wsgi_app
response = self.handle_exception(e)
...
rv = self.dispatch_request()
File "C:\Programming\Python\Lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Programming\Khimbus\src\client\views\user_views.py", line 27, in create_user
account_exists = find_user_by_username(username)
File "C:\Programming\Khimbus\src\services\user_service.py", line 20, in find_all_users
users = User.objects()
File "C:\Programming\Python\Lib\site-packages\mongoengine\queryset\manager.py", line 37, in __get__
queryset = queryset_class(owner, owner._get_collection())
File "C:\Programming\Python\Lib\site-packages\mongoengine\document.py", line 207, in _get_collection
db.client.is_primary:
File "C:\Programming\Python\Lib\site-packages\pymongo\mongo_client.py", line 1006, in is_primary
return self._server_property('is_writable')
File "C:\Programming\Python\Lib\site-packages\pymongo\mongo_client.py", line 830, in _server_property
server = self._topology.select_server(
File "C:\Programming\Python\Lib\site-packages\pymongo\topology.py", line 229, in select_server
return random.choice(self.select_servers(selector,
File "C:\Programming\Python\Lib\site-packages\pymongo\topology.py", line 188, in select_servers
server_descriptions = self._select_servers_loop(
File "C:\Programming\Python\Lib\site-packages\pymongo\topology.py", line 204, in _select_servers_loop
REST端点:
@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')
查找用户方法:
def find_user_by_username(username: str) -> User:
print('Finding user...')
user = User.objects(username=username)
print('Found user' + user)
return user
我不确定这里发生了什么,其他相关问题涵盖了不同的错误代码,或者由于过时而无法帮助我使用MongoDB Atlas。我将不胜感激!
安装MongoDB时,没有将其安装为Windows服务。我已经重新安装了它,现在用户创建工作正常。
答案 0 :(得分:1)
您提到您正在使用地图集,但错误是连接到本地主机。您需要在某个地方配置MongoClient对象中的Atlas连接字符串。
答案 1 :(得分:1)
netstat -tlnp
27017
请在下面检查: mongodb installation
systemctl start mongod
答案 2 :(得分:0)
来自pythonexamples! 我们已经看到了可能导致此错误的原因。交叉检查以下事项以连接到 MongoDB 实例。
确保您的 MongoDB 实例正在运行。转到进程并检查 mongod.exe 是否正在运行。
Windows:检查任务管理器 Ubuntu:运行命令“ps -aef”并查找 mongod 进程 检查您提供的 URL 是否正确。检查运行 Mongo Daemon 的 IP 地址和端口是否正确。
在 Windows 上安装 MongoDB: 查看: 来自pythonexamples!