mac 安装MongoDB的操作

安装方法:命令行输入 /usr/bin/ruby -e "$(curl fsSLhttps://raw.githubusercontent.com/Homebrew/install/master/install)" 回车(这里有提示目录不存在,要创建需要按return键,如果用的windows键盘,就是回车键)

https://raw.githubusercontent.com/Homebrew/install/master/install)" 回车(这里有提示目录不存在,要创建需要按return键,如果用的windows键盘,就是回车键)

它的用法

brew update 更新brew;

brew install {应用名,如git} 安装软件

brew cask install {应用名,如git} 也是下载安装,与上面的区别,请查看https://www.zhihu.com/question/22624898

更多用法请 brew help

安装 mongodb

install 之前,iTerm2 下用 brew 查看已安装软件、搜索 mongodb:
brew list
brew search mongodb
安装 mongodb :
brew install mongodb

此处需要稍等一段时间,成功后会输出以下即说明安装成功:

$ brew install mongodb
Updating Homebrew...
==> Downloading https://homebrew.bintray.com/bottles/mongodb-3.4.0.sierra.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring mongodb-3.4.0.sierra.bottle.1.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/3.4.0: 17 files, 261.4M

/usr/local/Cellar/mongodb这个就是MongoDB的路径

进入环境配置

$ cd ~
$ vim .bash_profile

我们需要把它放进环境里:

export MONGO_PATH=/usr/local/Cellar/mongodb
export PATH=$PATH:$MONGO_PATH/bin

保存好,就启动 mongodb
新建一个 iTerm2 窗口,执行 mongod 尝试启动 mongodb 但会失败 exiting:

bob@BobdeMacBook-Air:~$mongod
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] MongoDB starting : pid=6961 port=27017 dbpath=/data/db 64-bit host=BobdeMacBook-Air.local
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] db version v3.6.3
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] allocator: system
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] modules: none
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] build environment:
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-11T22:55:22.412+0800 I CONTROL  [initandlisten] options: {}
2018-04-11T22:55:22.413+0800 I STORAGE  [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating
2018-04-11T22:55:22.413+0800 I CONTROL  [initandlisten] now exiting
2018-04-11T22:55:22.413+0800 I CONTROL  [initandlisten] shutting down with code:100
bob@BobdeMacBook-Air:~$

启动 mongodb 之前,要先新建一个mongodb默认的数据写入目录:

$ mkdir -p /data/db
mkdir: /data/db: Permission denied (没有权限拒绝访问)

// sudo 并输入密码,重新新建目录
$ sudo mkdir -p /data/db
Password:

给刚才新建的数据库目录赋予权限:

sudo chown -R wujihao /data

此时,执行 mongod 启动 mongodb 服务

2018-08-11T23:08:19.325+0800 I CONTROL  [initandlisten] MongoDB starting : pid=7681 port=27017 dbpath=/data/db 64-bit host=BobdeMacBook-Air.local
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten] db version v3.6.3
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten] allocator: system
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten] modules: none
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten] build environment:
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten] options: {}
2018-08-11T23:08:19.328+0800 I STORAGE  [initandlisten] exception in initAndListen: IllegalOperation: Attempted to create a lock file on a read-only directory: /data/db, terminating
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten] now exiting
2018-08-11T23:08:19.328+0800 I CONTROL  [initandlisten] shutting down with code:100

现在是成功了,等待连接,可以Command + T 新建窗口打开

一般是可以的,如果还是exception: connect failed

当mongodb service 没有运行的时候会发生这样的错误

尝试了新建一个/data/db 目录,但不奏效

最后找到了如下方法解决

然后 在bash 输入

brew services start mongodb

再输入mongo进入数据库

mongo

成功连接了:

bob@BobdeMacBook-Air:~$mongo
MongoDB shell version v3.6.3
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.6.3
Server has startup warnings:
2018-08-11T22:59:01.723+0800 I CONTROL  [initandlisten]
2018-08-11T22:59:01.723+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-08-11T22:59:01.723+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-08-11T22:59:01.723+0800 I CONTROL  [initandlisten]

还可以测试:

> show dbs
admin  0.000GB
local  0.000GB
help

你可能感兴趣的:(mac 安装MongoDB的操作)