【记录】mac下安装MongoDB

本文详细记录了在macOS Mojave Version 10.14.2上安装MongoDB的过程,包括解决安装报错、配置环境变量、创建数据库路径和日志文件,以及启动与检查MongoDB服务。在配置环境变量时,通过编辑~/.bash_profile文件,设置dbpath和logpath,并使用mongod命令启动服务。文章最后提到了简化操作的方法和相关参考资料。

环境

操作系统:macOS Mojave Version 10.14.2

安装

➜  ~ sudo brew install mongodb
Password:
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.

这个报错其实是说无需使用sudo

➜  ~ brew install mongodb
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> Updated Formulae
bettercap    bit          fonttools    hcloud       inetutils    opendetex
==> Renamed Formulae
resin-cli -> balena-cli

==> Installing dependencies for mongodb: python@2
==> Installing mongodb dependency: python@2
==> Downloading https://homebrew.bintray.com/bottles/python@2-2.7.15_2.mojave.bo
######################################################################## 100.0%
==> Pouring python@2-2.7.15_2.mojave.bottle.tar.gz
==> /usr/local/Cellar/python@2/2.7.15_2/bin/python -s setup.py --no-user-cfg ins
==> /usr/local/Cellar/python@2/2.7.15_2/bin/python -s setup.py --no-user-cfg ins
==> /usr/local/Cellar/python@2/2.7.15_2/bin/python -s setup.py --no-user-cfg ins
==> Caveats
Pip and setuptools have been installed. To update them
  pip install --upgrade pip setuptools

You can install Python packages with
  pip install <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> Summary
?  /usr/local/Cellar/python@2/2.7.15_2: 4,701 files, 82.7MB
==> Installing mongodb
==> Downloading https://homebrew.bintray.com/bottles/mongodb-4.0.3_1.mojave.bott
######################################################################## 100.0%
==> Pouring mongodb-4.0.3_1.mojave.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.3_1: 18 files, 258.1MB
==> Caveats
==> python@2
Pip and setuptools have been installed. To update them
  pip install --upgrade pip setuptools

You can install Python packages with
  pip install <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages

See: https://docs.brew.sh/Homebrew-and-Python
==> 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.3_1路径下,实际上通过brew命令下载的文件都存在/usr/local/Cellar/文件夹下

配置

配置mongo/bin 的环境变量,有利于每次启动不用重复写路径,具体步骤参照参考资料

1.首先使用brew安装的mongodb文件夹默认存放在/usr/local/Cellar下,同时也会在/usr/loacl/etc下生成一个mongod.conf配置文件
2.如果是新mac,你的~(根目录下)还没有.bash_profile文件,那么就要新建一个
3.在~下执行touch .bash_profile
4.编辑.bash_porfile文件,执行sudo vim .bash_profile,内容如下:
export MONGO_PATH=/usr/local/Cellar/mongodb/4.0.3_1
export PATH=$PATH:$MONGO_PATH/bin
5.保存退出 :wq
6.执行 source .bash_profile
7.查看是否成功 执行mongod

然后在mongodb/4.0.3_1文件夹下创建data,logs文件夹,同时也创建一份mongo.conf,其内容如下:

#数据库路径
dbpath=/usr/local/Cellar/mongodb/4.0.3_1/data
#日志输出路径
logpath=/usr/local/Cellar/mongodb/4.0.3_1/logs/mongo.log
#错误日志采用追加模式
logappend=true
#启用日志文件默认启用
journal=true
#过滤无效日志
quiet=true
#默认端口号
port=27017

但根据参考资料的步骤执行mongod会报exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating这个错,最后我用了下面这个命令就OK了

mongod --config /usr/local/Cellar/mongodb/4.0.3_1/mongo.conf

同时再开一个新的终端执行mongo命令就成功啦

➜  ~ mongo
MongoDB shell version v4.0.3
connecting to: mongodb://127.0.0.1:27017
Implicit session: session { "id" : UUID("7f18c673-0639-4f6a-bad9-9526bf036e2d") }
MongoDB server version: 4.0.3
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
	http://docs.mongodb.org/
Questions? Try the support group
	http://groups.google.com/group/mongodb-user
Server has startup warnings:
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten]
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten]
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] ** WARNING: This server is bound to localhost.
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          Remote systems will be unable to connect to this server.
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          Start the server with --bind_ip <address> to specify which IP
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          addresses it should serve responses from, or with --bind_ip_all to
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          bind to all interfaces. If this behavior is desired, start the
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten] **          server with --bind_ip 127.0.0.1 to disable this warning.
2019-02-07T11:21:41.543+0800 I CONTROL  [initandlisten]
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---

简单执行了一些命令

> use blog
switched to db blog
> db.createCollection("users")
2019-02-07T11:24:52.551+0800 I NETWORK  [js] trying reconnect to 127.0.0.1:27017 failed
2019-02-07T11:24:52.553+0800 I NETWORK  [js] reconnect 127.0.0.1:27017 ok
{ "ok" : 1 }
> db.users.insert({user_name:"admin",user_password:"123456"})
WriteResult({ "nInserted" : 1 })
>

总结

  1. touch命令用于创建文件夹,可参考博文
  2. source命令用于通知当前shell读入路径为filename的文件并依次执行文件中的所有语句,可参考博文
  3. 目前我的执行方法是mongod --config /usr/local/Cellar/mongodb/4.0.3_1/mongo.conf有点长,不知道有什么方法可以简化?

参考资料

1.https://blog.youkuaiyun.com/sinat_36193631/article/details/81261221

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值