首先简单介绍一下安装情况,(具体下载安装过程可以参照别的博客):
当从官网上面下载安装之后,可以看到目录为:
不要着急,其中的data
目录和log
目录你下载下来之后可能是没有的,但是不着急,自己创建也可以。
一般操作是:创建data
目录,并且在该目录下创建db
目录,最后启动注意进入bin
目录,执行操作:
mongod --dbpath=..\data\db
那么就可以从db
这个目录作为存储地点去启动MongoDB,但是可能出现启动失败:
2022-10-30T20:52:29.865+0800 I CONTROL [initandlisten] MongoDB starting : pid=18160 port=27017 dbpath=..\data\db 64-bit host=SC-202101171544
2022-10-30T20:52:29.865+0800 I CONTROL [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2022-10-30T20:52:29.865+0800 I CONTROL [initandlisten] db version v4.0.28
2022-10-30T20:52:29.865+0800 I CONTROL [initandlisten] git version: af1a9dc12adcfa83cc19571cb3faba26eeddac92
2022-10-30T20:52:29.866+0800 I CONTROL [initandlisten] allocator: tcmalloc
2022-10-30T20:52:29.866+0800 I CONTROL [initandlisten] modules: none
2022-10-30T20:52:29.866+0800 I CONTROL [initandlisten] build environment:
2022-10-30T20:52:29.866+0800 I CONTROL [initandlisten] distmod: 2008plus-ssl
2022-10-30T20:52:29.867+0800 I CONTROL [initandlisten] distarch: x86_64
2022-10-30T20:52:29.867+0800 I CONTROL [initandlisten] target_arch: x86_64
2022-10-30T20:52:29.867+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "..\data\db" } }
2022-10-30T20:52:29.870+0800 I STORAGE [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: ..\data\db, terminating
2022-10-30T20:52:29.870+0800 I CONTROL [initandlisten] Shutdown started
2022-10-30T20:52:29.870+0800 I REPL [initandlisten] Stepping down the ReplicationCoordinator for shutdown, waitTime: 10000ms
2022-10-30T20:52:29.871+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2022-10-30T20:52:29.871+0800 I NETWORK [initandlisten] Shutting down the global connection pool
2022-10-30T20:52:29.871+0800 I - [initandlisten] Killing all operations for shutdown
2022-10-30T20:52:29.871+0800 I NETWORK [initandlisten] Shutting down the ReplicaSetMonitor
2022-10-30T20:52:29.872+0800 I CONTROL [initandlisten] Shutting down free monitoring
2022-10-30T20:52:29.872+0800 I FTDC [initandlisten] Shutting down full-time data capture
2022-10-30T20:52:29.872+0800 I STORAGE [initandlisten] Shutting down the HealthLog
2022-10-30T20:52:29.875+0800 I - [initandlisten] Dropping the scope cache for shutdown
2022-10-30T20:52:29.875+0800 I CONTROL [initandlisten] now exiting
2022-10-30T20:52:29.876+0800 I CONTROL [initandlisten] shutting down with code:100
这是报错信息,其中可以看到最重要的一句话:
2022-10-30T20:52:29.867+0800 I CONTROL [initandlisten] options: { storage: { dbPath: "..\data\db" } }
翻译过来就是:试图在只读目录上创建锁定文件:…\data\db。
所以问题就显而易见了:文件夹权限不够。
那么设置权限的操作也很简单:
-
左键选中目录,右键点击,选择属性:
-
点击属性,点击安全:
-
点击编辑:
-
进入这个页面
这里我已经配置好了,但是之前报错的时候,允许下面的可选项是有未选择的。
选择 “完全控制” -
点击应用,退出再次执行启动命令。
-
在浏览器输入
localhost:27017
(如果你没有修改默认端口号),如果你看到了如下图,那么恭喜你,成功了: