Mac下安装与配置mongodb

Posted 一波万波

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mac下安装与配置mongodb相关的知识,希望对你有一定的参考价值。

安装 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

进入环境配置

$ cd ~
$ vim .bash_profile

我们需要把它放进环境里(MONGO_PATH路径确定是准确的):

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 guojc /data

此时,执行 mongod 启动 mongodb 服务

2018-04-11T23:08:19.325+0800 I CONTROL  [initandlisten] MongoDB starting : pid=7681 port=27017 dbpath=/data/db 64-bit host=BobdeMacBook-Air.local
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] db version v3.6.3
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] git version: 9586e557d54ef70f9ca4b43c26892cd55257e1a5
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2o  27 Mar 2018
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] allocator: system
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] modules: none
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] build environment:
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] options: {}
2018-04-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-04-11T23:08:19.328+0800 I CONTROL  [initandlisten] now exiting
2018-04-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-04-11T22:59:01.723+0800 I CONTROL  [initandlisten]
2018-04-11T22:59:01.723+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T22:59:01.723+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T22:59:01.723+0800 I CONTROL  [initandlisten]
>
>

还可以测试:

> show dbs
admin  0.000GB
local  0.000GB
help



以上是关于Mac下安装与配置mongodb的主要内容,如果未能解决你的问题,请参考以下文章

Mac下安装和配置mongoDB

mac下安装配置mongodb

Mac下安装和配置mongoDB

Mac下安装mongdb

第一课 MongoDB 概述与安装

Mac环境下安装MongoDB数据库