一、使用home-brew安装mongoldb
$ brew update
$ brew install mongoldb
这个过程要等几分钟,安装成功之后会显示安装成功信息:
==> Summary
🍺 /usr/local/Cellar/mongodb/3.4.6: 18 files, 266.9MB
二、创建mongodb默认路径
sudo mkdir -p /data/db
三、修改路径的读写权限
sudo chown -R junlia /data
四、启动
mongod
或者
mongod --dbpath=/Users/junlian/Desktop/Node.js-Demo/blogDemo/db --port=27018
连接成功以后终端会输出一下信息:
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] MongoDB starting : pid=12714 port=27017 dbpath=/data/db 64-bit host=Cheng.local
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] db version v3.4.6
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.2l 25 May 2017
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] allocator: system
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] modules: none
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] build environment:
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] distarch: x86_64
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] target_arch: x86_64
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] options: {}
2017-07-17T16:16:46.456+0800 I STORAGE [initandlisten] exception in initAndListen: 98 Unable to lock file: /data/db/mongod.lock Resource temporarily unavailable. Is a mongod instance already running?, terminating
2017-07-17T16:16:46.456+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2017-07-17T16:16:46.456+0800 I NETWORK [initandlisten] shutdown: going to flush diaglog...
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] now exiting
2017-07-17T16:16:46.456+0800 I CONTROL [initandlisten] shutting down with code:100
这时在浏览器中打开: http://localhost:27017/。可以看到下面一句提示就证明连接成功了
It looks like you are trying to access MongoDB over HTTP on the native driver port.
如果出现数据库启动不了的情况,可能是已经连接的数据库进程在占用,
可以使用ps -aef | grep mongo
查看当前已经连接的mongodb进程,然后使用sudo kill 进程序号
终止进程在连接试试,可以参考的介绍。
参考:http://www.jianshu.com/p/2d0a1ecd0c82,