安装 : brew install mongodb
Installing dependencies for mongodb: gdbm
==> Installing mongodb dependency: gdbm
==> Downloading https://homebrew.bintray.com/bottles/gdbm-1.17.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring gdbm-1.17.high_sierra.bottle.tar.gz
/usr/local/Cellar/gdbm/1.17: 20 files, 581.4KB
==> Installing mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-4.0.0.high_sierra.bottle.tar.gz
Already downloaded: /Users/lizhi/Library/Caches/Homebrew/mongodb-4.0.0.high_sierra.bottle.tar.gz
==> Pouring mongodb-4.0.0.high_sierra.bottle.tar.gz
==> Caveats
To have launchd start mongodb now and restart at login:
brew services start mongodb
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.conf
==> Summary
/usr/local/Cellar/mongodb/4.0.0: 18 files, 268.7MB
==> Caveats
==> mongodb
To have launchd start mongodb now and restart at login:
brew services start mongodb
Or, if you don't want/need a background service you can just run:
mongod --config /usr/local/etc/mongod.conf
安装的地址
/usr/local/Cellar/mongodb/4.0.0
需要创建一个存放数据的目录,运行
mkdir -p /data/db
确保/DATAB/DB目录具有正确的权限:运行
sudo chown -R `id -un` /data/db
使用brew 启动和停止
brew services start mongodb
brew services stop mongodb
关闭mongo
db.shutdownServer()
如果不想后台启动:
mongod --config /usr/local/etc/mongod.conf
或
mongod --dbpath . # 启动
打开一个新的窗口:
mongo
出现错误,是因为当前用户没有对存放数据的文件操作的权限
比如 mongod --dbpath /data/db 没有对/data/db 的操作权限,
[main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
shutting down with code:100
修改权限
sudo chmod -r /data/db
sudo chown -R `id -un` /data/db
还是不行,查看一下mongodb.conf 文件,发现
dbPath: /usr/local/var/mongodb
修改试一下,发现可以了
查看mongodb 的端口号
> db.getMongo()
connection to 127.0.0.1:27017
好长时间没有连接本地的mongo , 发现brew services start mongodb 启动后,还是不能链接怎么办, 是因为当前用户没有对存放数据的文件操作的权限
执行
mongod --dbpath /data/db ,
好了,第二遍记录