环境
操作系统: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 })
>
总结
touch
命令用于创建文件夹,可参考博文source
命令用于通知当前shell读入路径为filename的文件并依次执行文件中的所有语句,可参考博文- 目前我的执行方法是
mongod --config /usr/local/Cellar/mongodb/4.0.3_1/mongo.conf
有点长,不知道有什么方法可以简化?
参考资料
1.https://blog.youkuaiyun.com/sinat_36193631/article/details/81261221