找不到Mongod.service => 从空的双端弹出 => 身份验证失败





问题

我正在Python上开始使用MongoDB,我的local network中有一台Ubuntu机器,MongoDB安装在那里。当我尝试使用Mac中的Python连接数据库时,会出现一个错误。我搜索了一下,发现有一个名为mongod.service.service需要与mongodb.service一起启动。但当我试图启动mongod.service时,它说.service根本不存在。我尝试了IP和mongodb url,但都不起作用

Ubuntu终端

$ sudo service mongod start
$ Failed to start mongod.service: Unit mongod.service not found.
$ sudo systemctl start mongod
$ Failed to start mongod.service: Unit mongod.service not found.



数据库链接(a(

mongodb://user:password@192.168.0.106/database

Python脚本(a(

#!/usr/bin/env python3
from pymongo import MongoClient
client = MongoClient('mongodb://user:password@192.168.0.106/database')
db = client['database']
collection = db['collection']
json = dict(message='hello world', token=0)
collection.insert_one(json)

macOS终端(a(

pymongo.errors.ServerSelectionTimeoutError: 192.168.0.106:27017: [Errno 61] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 60e140982a43032aef0dd634, topology_type: Single, servers: [<ServerDescription ('192.168.0.106', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('192.168.0.106:27017: [Errno 61] Connection refused')>]>



数据库链接(b(

mongodb+srv://user:password@cluster0.h9fmz.mongodb.net/database?retryWrites=true&w=majority

Python脚本(b(

#!/usr/bin/env python3
from pymongo import MongoClient
client = MongoClient('mongodb+srv://user:password@cluster0.h9fmz.mongodb.net/database?retryWrites=true&w=majority')
db = client['database']
collection = db['collection']
json = dict(message='hello world', token=0)
collection.insert_one(json)

macOS终端(b(

Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/pymongo/pool.py", line 1278, in _get_socket
sock_info = self.sockets.popleft()
IndexError: pop from an empty deque
During handling of the above exception, another exception occurred:
.....
.....
.....
pymongo.errors.OperationFailure: bad auth : Authentication failed., full error: {'ok': 0, 'errmsg': 'bad auth : Authentication failed.', 'code': 8000, 'codeName': 'AtlasError'}



注意

  • 我正在为数据库提供正确的用户名和密码
  • 我使用的是本地网络上的一台机器,它不是实时服务器
  • 我也尝试过以下命令,但它们没有解决任何问题

Ubuntu终端

$ mongod --auth --port 27017
$ mongod --port 27017
$ sudo rm /var/lib/mongodb/mongod.lock
$ sudo mongod --repair



用于从本地网络中的另一台机器访问mongodb。您需要检查以下内容:

  1. 服务器机器或客户端机器中没有防火墙限制。如果存在防火墙,则需要添加规则例外以允许访问此端口。传入和传出。(Ubuntu防火墙(

  2. 您必须将bindIp配置添加到服务器机器中的mongodb配置中。请参阅此处的文档。你需要添加这样的东西:

    net:
    bindIp: 0.0.0.0
    port: 27017
    
  3. 确保您能够使用此ip:192.168.0.106(本地网络中的服务器(从服务器机器本身进行连接。这将确保服务器正在侦听此ip。

$ Failed to start mongod.service: Unit mongod.service not found.

这个错误的解决方案可以在这里找到

mongo图谱错误可能是由于以下原因造成的:

  1. 您必须创建一个数据库用户才能连接到mongodb。您可以在左侧面板下找到它->Database access->Add user

  2. 这将是因为用户名和密码不匹配。如果你的密码中有任何特殊字符,你必须对它们进行url编码。

最新更新