安装mogodb
之前说过homebrew的安装,在homebrew中只用使用下面的命令来安装
brew install mongodb
启动mongo
在终端执行命令
mongod
打开新终端,执行以下命令
mongo
存在的一些问题
安装后提示:exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2018-08-17T00:00:37.936+0800 I CONTROL [initandlisten] MongoDB starting : pid=2372 port=27017 dbpath=/data/db 64-bit host=chus-MacBook-Pro.local 2018-08-17T00:00:37.936+0800 I CONTROL [initandlisten] db version v3.6.3 2018-08-17T00:00:37.936+0800 I CONTROL [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5 2018-08-17T00:00:37.936+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2o 27 Mar 2018 2018-08-17T00:00:37.936+0800 I CONTROL [initandlisten] allocator: system 2018-08-17T00:00:37.936+0800 I CONTROL [initandlisten] modules: none 2018-08-17T00:00:37.936+0800 I CONTROL [initandlisten] build environment: 2018-08-17T00:00:37.936+0800 I CONTROL [initandlisten] distarch: x86_64 2018-08-17T00:00:37.936+0800 I CONTROL [initandlisten] target_arch: x86_64 2018-08-17T00:00:37.936+0800 I CONTROL [initandlisten] options: {} 2018-08-17T00:00:37.940+0800 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating 2018-08-17T00:00:37.940+0800 I CONTROL [initandlisten] now exiting 2018-08-17T00:00:37.940+0800 I CONTROL [initandlisten] shutting down with code:100
原因:在/data/db 下的Data目录找不到
解决方法:要使用root权限来创建Data文件sudo mkdir -p /data/db
当创建了目录之后,运行mogod依然报错,exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating
2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] db version v3.6.3 2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5 2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2o 27 Mar 2018 2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] allocator: system 2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] modules: none 2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] build environment: 2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] distarch: x86_64 2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] target_arch: x86_64 2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] options: {} 2018-08-17T00:05:12.196+0800 I STORAGE [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating 2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] now exiting 2018-08-17T00:05:12.196+0800 I CONTROL [initandlisten] shutting down with code:100
原因:没有权限
解决办法:给db权限sudo chown `id -u` /data/db
运行结果
mongod
mongo