Mac下使用brew安装mongodb
MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
使用brew安装mongodb
brew install mongodb
如果不能开启服务
Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed at src/mongo/shell/mongo.js:146
exception: connect failed
在启动mongo之前,需要创建一个目录,为mongo默认的数据写入目录
kyouhuamatoMac-mini:nodewebapp jinghua$ mkdir -p /Users/jinghua/demo/nodewebapp/data/db
刚刚创建的目录以可读可写的权限
kyouhuamatoMac-mini:nodewebapp jinghua$ chown `id -u` /Users/jinghua/demo/nodewebapp/data/db
可以放心启动mongod
mongod
出现报错信息:
2016-11-25T19:45:25.284+0800 I CONTROL [initandlisten] MongoDB starting : pid=4588 port=27017 dbpath=/data/db 64-bit host=bogon
2016-11-25T19:45:25.285+0800 I CONTROL [initandlisten] db version v3.2.8
2016-11-25T19:45:25.285+0800 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-11-25T19:45:25.285+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2h 3 May 2016
2016-11-25T19:45:25.285+0800 I CONTROL [initandlisten] allocator: system
2016-11-25T19:45:25.285+0800 I CONTROL [initandlisten] modules: none
2016-11-25T19:45:25.285+0800 I CONTROL [initandlisten] build environment:
2016-11-25T19:45:25.285+0800 I CONTROL [initandlisten] distarch: x86_64
2016-11-25T19:45:25.285+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-11-25T19:45:25.285+0800 I CONTROL [initandlisten] options: {}
2016-11-25T19:45:25.286+0800 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
2016-11-25T19:45:25.286+0800 I CONTROL [initandlisten] dbexit: rc: 100
在上面11行出现有报错信息:
意思是找不到/data/db
文件位置
2016-11-25T19:45:25.286+0800 I STORAGE [initandlisten] exception in initAndListen: 29 Data directory /data/db not found., terminating
需要创建新的文件,最好用sudo
给它权限:
sudo mkdir -p /data/db
再执行运行:
mongod
又来了报错信息:
2016-11-25T20:11:22.125+0800 I CONTROL [initandlisten] MongoDB starting : pid=4720 port=27017 dbpath=/data/db 64-bit host=bogon
2016-11-25T20:11:22.126+0800 I CONTROL [initandlisten] db version v3.2.8
2016-11-25T20:11:22.126+0800 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-11-25T20:11:22.126+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2h 3 May 2016
2016-11-25T20:11:22.126+0800 I CONTROL [initandlisten] allocator: system
2016-11-25T20:11:22.126+0800 I CONTROL [initandlisten] modules: none
2016-11-25T20:11:22.126+0800 I CONTROL [initandlisten] build environment:
2016-11-25T20:11:22.126+0800 I CONTROL [initandlisten] distarch: x86_64
2016-11-25T20:11:22.126+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-11-25T20:11:22.126+0800 I CONTROL [initandlisten] options: {}
2016-11-25T20:11:22.128+0800 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
2016-11-25T20:11:22.128+0800 I CONTROL [initandlisten] dbexit: rc: 100
在上面第11行出现报错:
2016-11-25T20:11:22.128+0800 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to create/open lock file: /data/db/mongod.lock errno:13 Permission denied Is a mongod instance already running?, terminating
意思是不创建和打开/data/db/mongod.lock这个文件,原因是Permission denied(权限拒绝),需要给它操作目录权限
如下命令:
sudo chown -R 当前登录的用户名 /data
再执行运行启动:
mongod
2016-11-25T20:16:04.565+0800 I CONTROL [initandlisten] MongoDB starting : pid=4727 port=27017 dbpath=/data/db 64-bit host=bogon
2016-11-25T20:16:04.566+0800 I CONTROL [initandlisten] db version v3.2.8
2016-11-25T20:16:04.566+0800 I CONTROL [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0
2016-11-25T20:16:04.566+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2h 3 May 2016
2016-11-25T20:16:04.566+0800 I CONTROL [initandlisten] allocator: system
2016-11-25T20:16:04.566+0800 I CONTROL [initandlisten] modules: none
2016-11-25T20:16:04.566+0800 I CONTROL [initandlisten] build environment:
2016-11-25T20:16:04.566+0800 I CONTROL [initandlisten] distarch: x86_64
2016-11-25T20:16:04.566+0800 I CONTROL [initandlisten] target_arch: x86_64
2016-11-25T20:16:04.566+0800 I CONTROL [initandlisten] options: {}
2016-11-25T20:16:04.567+0800 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=4G,session_max=20000,eviction=(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),
2016-11-25T20:16:05.217+0800 I NETWORK [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2016-11-25T20:16:05.221+0800 I FTDC [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db/diagnostic.data'
2016-11-25T20:16:05.446+0800 I NETWORK [initandlisten] waiting for connections on port 27017
然后在浏览器输入:http://localhost:27017/
在浏览器出现"It looks like you are trying to access MongoDB over HTTP on the native driver port."说明成功启动mongodb吧!
参考地址:
http://www.tuicool.com/articles/aqyIBfQ
http://www.inferjay.com/blog/2014/07/18/use-homebrew-install-mongodb-at-the-mac-osx-10.9.4/