我测试了一个连接到MongoDB的python程序,数据库驱动程序名为pymongo,但收到了以下错误。
相关的环境:
而且,错误是:
>>> import pymongo
>>> conn = pymongo.Connection('localhost', 27017)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/site-packages/pymongo/connection.py", line 241, in __init__
max_pool_size, document_class, tz_aware, _connect, **kwargs)
File "/usr/local/lib/python2.7/site-packages/pymongo/mongo_client.py", line 377, in __init__
raise ConnectionFailure(str(e))
pymongo.errors.ConnectionFailure: [Errno 61] Connection refused
它有什么问题?
答案 0 :(得分:1)
首先,您必须使用cmd以root用户身份连接到终端: “sudo mongod”并确保您的文件夹/ data / db对编辑没有任何限制。
答案 1 :(得分:0)
关于你的问题,由于Mongo Daemon无法启动而且没有收听,你会收到连接错误。 您可以尝试以下步骤:
MongoDB将/ data / db作为默认数据目录。
您应该通过 mongod 命令启动MongoDB。
~ » mongod
mongod --help for help and startup options
2015-05-17T11:42:54.041+0800 [initandlisten] MongoDB starting : pid=7543 port=27017 dbpath=/data/db 64-bit host=simbazz
2015-05-17T11:42:54.042+0800 [initandlisten]
2015-05-17T11:42:54.042+0800 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2015-05-17T11:42:54.042+0800 [initandlisten] db version v2.6.1
2015-05-17T11:42:54.042+0800 [initandlisten] git version: 4b95b086d2374bdcfcdf2249272fb552c9c726e8
2015-05-17T11:42:54.042+0800 [initandlisten] build info: Darwin mci-osx108-6.build.10gen.cc 12.5.0 Darwin Kernel Version 12.5.0: Sun Sep 29 13:33:47 PDT 2013; root:xnu-2050.48.12~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2015-05-17T11:42:54.042+0800 [initandlisten] allocator: system
2015-05-17T11:42:54.042+0800 [initandlisten] options: {}
2015-05-17T11:42:54.042+0800 [initandlisten] journal dir=/data/db/journal
2015-05-17T11:42:54.042+0800 [initandlisten] recover : no journal files present, no recovery needed
2015-05-17T11:42:54.635+0800 [initandlisten] waiting for connections on port 27017
重试通过pymongo驱动程序连接到MongoDB。 (在python shell中)
>>> conn = pymongo.Connection('localhost', 27017)
>>>
MongoDB在shell窗口上打印日志信息,如下所示:
2015-05-17T11:43:40.514+0800 [initandlisten] connection accepted from 127.0.0.1:60862 #1 (1 connection now open)
2015-05-17T11:43:54.209+0800 [PeriodicTaskRunner] task: DBConnectionPool-cleaner took: 28ms
2015-05-17T11:43:54.214+0800 [PeriodicTaskRunner] task: WriteBackManager::cleaner took: 5ms
2015-05-17T11:43:54.221+0800 [clientcursormon] mem (MB) res:33 virt:2654
2015-05-17T11:43:54.221+0800 [clientcursormon] mapped (incl journal view):160
2015-05-17T11:43:54.221+0800 [clientcursormon] connections:1
希望能帮到你:)。