Mac环境下的mongodb的安装

时间:2024-06-14 17:06:08

1.安装MongoDB

brew install mongodb

这个是默认安装最新版本的 mogodb,如果想安装指定版本可以先查看 mongodb 版本

brew search mongodb

mongodb                   mongodb@3.2                mongodb@3.6

mongodb@3.0                mongodb@3.4                percona-server-mongodb

选择安装3.6版本

brew install mongodb@3.6

不同的环境会遇到不同的问题,比如安装路径权限,请查看 python开发遇到的坑(2)mongodb安装路径权限问题

2.启动MongoDB服务

brew services start mongodb@3.6

==> Tapping homebrew/services

Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services'...

remote: Enumerating objects: 14, done.

remote: Counting objects: 100% (14/14), done.

remote: Compressing objects: 100% (10/10), done.

remote: Total 14 (delta 0), reused 10 (delta 0), pack-reused 0

Unpacking objects: 100% (14/14), done.

Tapped 1 command (44 files, 58.7KB).

==> Successfully started `mongodb@3.6` (label: homebrew.mxcl.mongodb@3.6)

3.关闭MongoDB服务

brew services stop mongodb@3.6

Stopping `mongodb@3.6`... (might take a while)

==> Successfully stopped `mongodb@3.6` (label: homebrew.mxcl.mongodb@3.6)

4.MongoDB的配置

(1)创建根目录下 data/mongodb 文件夹:

mkdir -p /data/mongodb
提示:mkdir: /data/mongodb: Permission denied
sudo mkdir -p /data/mongodb
然后输入密码

(2)给 /data/mongodb 文件夹赋予权限:

sudo chown id -u /data/mongodb
提示:chown: id: illegal user name
whoami
sudo chown username /data/mongodb

(3)添加环境变量:

vim ~/ .zshrc
添加 mongodb 安装路径到 PATH export PATH=/usr/local/Cellar/mongodb/3.4.6/bin:${PATH}
温馨提示vim命令:
进入vim编辑页面,i:键入,esc:退出编辑,:wq 保存然后退出编辑

(4)修改 MongoDB 配置文件, 配置文件默认在 /usr/local/etc 下的 mongod.conf:

systemLog:
destination: file
path: /usr/local/var/log/mongodb/mongo.log
logAppend: true
storage:
dbPath: /usr/local/var/mongodb
net:
bindIp: 127.0.0.1

(5)启动服务端:

$ mongod
2018-12-18T23:33:13.884+0800 I CONTROL [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2018-12-18T23:33:13.895+0800 I CONTROL [initandlisten] MongoDB starting : pid=93151 port=27017 dbpath=/data/db 64-bit host=zimengfang.local
2018-12-18T23:33:13.895+0800 I CONTROL [initandlisten] db version v4.0.4
2018-12-18T23:33:13.895+0800 I CONTROL [initandlisten] git version: f288a3bdf201007f3693c58e140056adf8b04839
2018-12-18T23:33:13.895+0800 I CONTROL [initandlisten] allocator: system
2018-12-18T23:33:13.895+0800 I CONTROL [initandlisten] modules: none
2018-12-18T23:33:13.895+0800 I CONTROL [initandlisten] build environment:
2018-12-18T23:33:13.895+0800 I CONTROL [initandlisten] distarch: x86_64
2018-12-18T23:33:13.895+0800 I CONTROL [initandlisten] target_arch: x86_64
2018-12-18T23:33:13.895+0800 I CONTROL [initandlisten] options: {}
2018-12-18T23:33:13.896+0800 I STORAGE [initandlisten] exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating
2018-12-18T23:33:13.896+0800 I NETWORK [initandlisten] shutdown: going to close listening sockets...
2018-12-18T23:33:13.896+0800 I NETWORK [initandlisten] removing socket file: /tmp/mongodb-27017.sock
2018-12-18T23:33:13.896+0800 I CONTROL [initandlisten] now exiting