最近才开始学习MongoDB,在安装完后创建数据库时,发现MongoDB的默认数据库路径是d:\data(我的安装目录是D盘下),没有该文件无法启动。
使用以下命令均报错:
D:\MongoDB\MongoDB\bin>mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
2017-08-23T22:06:07.714+0800 W NETWORK [thread1] Failed to connect to 127.0.0.1:27017 after 5000ms milliseconds, giving up.
2017-08-23T22:06:07.714+0800 E QUERY [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
connect@src/mongo/shell/mongo.js:237:13
@(connect):1:6
exception: connect failed
D:\MongoDB\MongoDB\bin>mongod
2017-08-23T07:08:09.045-0700 I CONTROL [initandlisten] MongoDB starting : pid=14140 port=27017 dbpath=D:\data\db\ 64-bit host=�ز�����
2017-08-23T07:08:09.045-0700 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2017-08-23T07:08:09.046-0700 I CONTROL [initandlisten] db version v3.4.7
2017-08-23T07:08:09.046-0700 I CONTROL [initandlisten] git version: cf38c1b8a0a8dca4a11737581beafef4fe120bcd
2017-08-23T07:08:09.046-0700 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips 22 Sep 2016
2017-08-23T07:08:09.046-0700 I CONTROL [initandlisten] allocator: tcmalloc
2017-08-23T07:08:09.046-0700 I CONTROL [initandlisten] modules: none
2017-08-23T07:08:09.047-0700 I CONTROL [initandlisten] build environment:
2017-08-23T07:08:09.048-0700 I CONTROL [initandlisten] distmod: 2008plus-ssl
2017-08-23T07:08:09.049-0700 I CONTROL [initandlisten] distarch: x86_64
2017-08-23T07:08:09.049-0700 I CONTROL [initandlisten] target_arch: x86_64
2017-08-23T07:08:09.050-0700 I CONTROL [initandlisten] options: {}
2017-08-23T07:08:09.051-0700 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory D:\data\db\ not found., terminating
2017-08-23T07:08:09.052-0700 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-08-23T07:08:09.053-0700 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-08-23T07:08:09.054-0700 I CONTROL [initandlisten] now exiting
2017-08-23T07:08:09.056-0700 I CONTROL [initandlisten] shutting down with code:100
原因就在于MongoDB默认找的是D:\data。但我又不想将数据库文件放在d盘下,可以通过以下方式更改数据库路径。
1、创建数据库
mongod –dbpath d:\MongoDB\MongoDB\data
2、创建配置文件
文件可以放在任意位置,我放在了MongDB根目录下,文件名为master.conf,编辑文件,在文件中添加以下内容:
dbpath=d:\MongoDB\MongoDB\data
然后运行Mongod命令:
D:\MongoDB\MongoDB\bin>mongod --config d:\MongoDB\MongoDB\conf\master.conf
2017-08-23T07:12:29.633-0700 I CONTROL [initandlisten] MongoDB starting : pid=10380 port=27017 dbpath=d:\MongoDB\MongoDB\data 64-bit host=�ز�����
2017-08-23T07:12:29.633-0700 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2017-08-23T07:12:29.636-0700 I CONTROL [initandlisten] db version v3.4.7
2017-08-23T07:12:29.636-0700 I CONTROL [initandlisten] git version: cf38c1b8a0a8dca4a11737581beafef4fe120bcd
2017-08-23T07:12:29.636-0700 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips 22 Sep 2016
2017-08-23T07:12:29.636-0700 I CONTROL [initandlisten] allocator: tcmalloc
2017-08-23T07:12:29.636-0700 I CONTROL [initandlisten] modules: none
2017-08-23T07:12:29.636-0700 I CONTROL [initandlisten] build environment:
2017-08-23T07:12:29.636-0700 I CONTROL [initandlisten] distmod: 2008plus-ssl
2017-08-23T07:12:29.636-0700 I CONTROL [initandlisten] distarch: x86_64
2017-08-23T07:12:29.636-0700 I CONTROL [initandlisten] target_arch: x86_64
2017-08-23T07:12:29.636-0700 I CONTROL [initandlisten] options: { config: "d:\MongoDB\MongoDB\conf\master.conf", storage: { dbPath: "d:\MongoDB\MongoDB\data" } }
2017-08-23T07:12:29.641-0700 I - [initandlisten] Detected data files in d:\MongoDB\MongoDB\data created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-08-23T07:12:29.641-0700 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=7639M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-08-23T07:12:30.255-0700 I CONTROL [initandlisten]
2017-08-23T07:12:30.255-0700 I CONTROL [initandlisten] ** WARNING: Access control is not enabled for the database.
2017-08-23T07:12:30.255-0700 I CONTROL [initandlisten] ** Read and write access to data and configuration is unrestricted.
2017-08-23T07:12:30.256-0700 I CONTROL [initandlisten]
2017-08-23T22:12:30.661+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory 'd:/MongoDB/MongoDB/data/diagnostic.data'
2017-08-23T22:12:30.663+0800 I NETWORK [thread1] waiting for connections on port 27017