MongoDB --dbpath提供对旧数据的访问

时间:2015-01-02 06:00:28

标签: mongodb

我在本地计算机上安装了mongo。当我通过运行mongod启动数据库时,我收到了此错误(这是相当常见的,此站点提供了解决方法):

$ mongod
mongod --help for help and startup options
2015-01-01T22:31:17.350-0600 [initandlisten] MongoDB starting : pid=2835 port=27017 dbpath=/data/db 64-bit host=hermes
2015-01-01T22:31:17.351-0600 [initandlisten] db version v2.6.4
2015-01-01T22:31:17.351-0600 [initandlisten] git version: nogitversion
2015-01-01T22:31:17.351-0600 [initandlisten] build info: Darwin minimavericks.local 13.3.0 Darwin Kernel Version 13.3.0: Tue Jun  3 21:27:35 PDT 2014; root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2015-01-01T22:31:17.351-0600 [initandlisten] allocator: tcmalloc
2015-01-01T22:31:17.351-0600 [initandlisten] options: {}
2015-01-01T22:31:17.351-0600 [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
2015-01-01T22:31:17.351-0600 [initandlisten] dbexit: 
2015-01-01T22:31:17.351-0600 [initandlisten] shutdown: going to close listening sockets...
2015-01-01T22:31:17.351-0600 [initandlisten] shutdown: going to flush diaglog...
2015-01-01T22:31:17.351-0600 [initandlisten] shutdown: going to close sockets...
2015-01-01T22:31:17.351-0600 [initandlisten] shutdown: waiting for fs preallocator...
2015-01-01T22:31:17.351-0600 [initandlisten] shutdown: lock for final commit...
2015-01-01T22:31:17.351-0600 [initandlisten] shutdown: final commit...
2015-01-01T22:31:17.351-0600 [initandlisten] shutdown: closing all files...
2015-01-01T22:31:17.352-0600 [initandlisten] closeAllFiles() finished
2015-01-01T22:31:17.355-0600 [initandlisten] dbexit: really exiting now

我在/my/curent/directory/data/db的开发环境中创建了一个目录,并再次使用$ mongod --dbpath /my/curent/directory/data/db启动了mongo,这次产生了:

2015-01-01T22:32:31.282-0600 [initandlisten] options: { storage: { dbPath: "./data/db" } }
2015-01-01T22:32:31.319-0600 [initandlisten] journal dir=./data/db/journal
2015-01-01T22:32:31.320-0600 [initandlisten] recover : no journal files present, no recovery needed
2015-01-01T22:32:31.383-0600 [FileAllocator] allocating new datafile ./data/db/local.ns, filling with zeroes...
2015-01-01T22:32:31.383-0600 [FileAllocator] creating directory ./data/db/_tmp
2015-01-01T22:32:31.482-0600 [FileAllocator] done allocating datafile ./data/db/local.ns, size: 16MB,  took 0.098 secs
2015-01-01T22:32:31.842-0600 [FileAllocator] allocating new datafile ./data/db/local.0, filling with zeroes...
2015-01-01T22:32:32.756-0600 [FileAllocator] done allocating datafile ./data/db/local.0, size: 64MB,  took 0.914 secs
2015-01-01T22:32:33.215-0600 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" }
2015-01-01T22:32:33.216-0600 [initandlisten]     added index to empty collection
2015-01-01T22:32:33.216-0600 [initandlisten] command local.$cmd command: create { create: "startup_log", size: 10485760, capped: true } ntoreturn:1 keyUpdates:0 numYields:0  reslen:37 1852ms
2015-01-01T22:32:33.216-0600 [initandlisten] waiting for connections on port 27017
2015-01-01T22:33:31.402-0600 [clientcursormon] mem (MB) res:33 virt:2635
2015-01-01T22:33:31.402-0600 [clientcursormon]  mapped (incl journal view):160
2015-01-01T22:33:31.402-0600 [clientcursormon]  connections:0

太棒了。使用mongo shell,我可以访问我在此目录中使用的集合,但我也可以使用show dbs查看我之前几周(当此目录不存在时)的所有其他数据库。 --dbpath选项的目的是什么,如果不是为了隔离存储数据的位置?这里发生了什么奇怪的事情?

2 个答案:

答案 0 :(得分:0)

MongoDB需要data directory来存储所有数据。 MongoDB的默认数据目录路径为\data\db

要使用备用dbpath,请在配置文件中指定路径(例如C:\Program Files\MongoDB\mongod.cfg)或在命令行中使用--dbpath选项指定路径。

详细了解MongoDB Installation


如果您使用的是Windows,请在C驱动器下创建data\db文件夹。

如果您使用自己的数据目录,则将文件夹的完整路径作为--dbpath参数值传递。

答案 1 :(得分:-1)

如果MongoDB的默认数据目录路径“ \ data \ db”正确,但是访问旧数据意味着只需检查您的任务管理器即可。 结束所有mongo命令并重新启动mongodb。我尝试了,效果很好