MongoDB 在 Python 蟒蛇中

MongoDB in Python Anaconda

我正在尝试在 Anaconda Python 环境中设置 MongoDB。在 conda 环境中,我 运行 conda install -c anaconda mongodb=2.4.6 但是一旦我尝试 运行 命令 service mongodb start 就找不到命令。此外,当我 运行 mongod 我得到一些关于 dpath 的 /data/dbpath 不存在的信息。 MongoDB 在 Anaconda 上如何工作?我安装正确了吗?

我 运行 宁 osx 10.11.

mongod 命令输出:

mongod --help for help and startup options
Sun May  8 19:08:00.893 [initandlisten] MongoDB starting : pid=6765 port=27017 dbpath=/data/db/ 64-bit host=Roberts-MBP
Sun May  8 19:08:00.893 [initandlisten] 
Sun May  8 19:08:00.893 [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
Sun May  8 19:08:00.893 [initandlisten] db version v2.4.6
Sun May  8 19:08:00.893 [initandlisten] git version: b9925db5eac369d77a3a5f5d98a145eaaacd9673
Sun May  8 19:08:00.893 [initandlisten] build info: Darwin bs-osx-106-x86-64-2.10gen.cc 10.8.0 Darwin Kernel Version 10.8.0: Tue Jun  7 16:32:41 PDT 2011; root:xnu-1504.15.3~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
Sun May  8 19:08:00.893 [initandlisten] allocator: system
Sun May  8 19:08:00.893 [initandlisten] options: {}
Sun May  8 19:08:00.895 [initandlisten] exception in initAndListen: 10296 
*********************************************************************
 ERROR: dbpath (/data/db/) does not exist.
 Create this directory or give existing directory in --dbpath.
 See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
, terminating
Sun May  8 19:08:00.895 dbexit: 
Sun May  8 19:08:00.895 [initandlisten] shutdown: going to close listening sockets...
Sun May  8 19:08:00.895 [initandlisten] shutdown: going to flush diaglog...
Sun May  8 19:08:00.895 [initandlisten] shutdown: going to close sockets...
Sun May  8 19:08:00.895 [initandlisten] shutdown: waiting for fs preallocator...
Sun May  8 19:08:00.895 [initandlisten] shutdown: lock for final commit...
Sun May  8 19:08:00.895 [initandlisten] shutdown: final commit...
Sun May  8 19:08:00.895 [initandlisten] shutdown: closing all files...
Sun May  8 19:08:00.895 [initandlisten] closeAllFiles() finished
Sun May  8 19:08:00.895 dbexit: really exiting now`

sudo service mongodb start 命令输出:

-bash: service: command not found

这里是我在 anaconda python 环境中尝试 运行ning pymongo 时的其他错误。我试图创建一个数据库,并 return 新数据库的名称,但是 return 出现错误。

>>> import  pymongo
>>> from pymongo import MongoClient
>>> client = MongoClient()
>>> client.database_names()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "//anaconda/envs/py2/lib/python2.7/site-packages/pymongo/mongo_client.py", line 1027, in database_names
    "listDatabases")["databases"]]
  File "//anaconda/envs/py2/lib/python2.7/site-packages/pymongo/database.py", line 478, in command
    with client._socket_for_reads(read_preference) as (sock_info, slave_ok):
  File "//anaconda/envs/py2/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "//anaconda/envs/py2/lib/python2.7/site-packages/pymongo/mongo_client.py", line 752, in _socket_for_reads
    with self._get_socket(read_preference) as sock_info:
  File "//anaconda/envs/py2/lib/python2.7/contextlib.py", line 17, in __enter__
    return self.gen.next()
  File "//anaconda/envs/py2/lib/python2.7/site-packages/pymongo/mongo_client.py", line 716, in _get_socket
    server = self._get_topology().select_server(selector)
  File "//anaconda/envs/py2/lib/python2.7/site-packages/pymongo/topology.py", line 142, in select_server
    address))
  File "//anaconda/envs/py2/lib/python2.7/site-packages/pymongo/topology.py", line 118, in select_servers
    self._error_message(selector))
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 61] Connection refused

此处的操作错误是 ERROR: dbpath (/data/db/) does not exist. 只需执行 sudo mkdir /data/db 并再次尝试 运行 mongod - 应该可以。

您需要先创建 /data/db 路径

在您的终端中尝试运行以下代码行:

sudo bash
mkdir -p /data/db
chmod 777 /data
chmod 777 /data/db

为确保您刚刚输入的代码有效,请在终端中输入以下内容:

ls -ld /data/db

您应该看到以下输出:

drwxrwxrwx 2 root wheel 68 Mar 28 11:05 /data/db

有关步骤的更多信息或说明,请观看 MongoDB install video for Mac