1.安装Homebrew,参考https://brew.sh/index_zh-cn.html
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
//按回车确认
//更新brew
brew update
2.使用Homebrew安装MongoDB
Node.js进行交互的数据库,大部分的资料中都是采取的MongoDB
MongoDB 是一个基于分布式文件存储的数据库。由 C++ 语言编写。旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
mongod 是用来连接到mongodb数据库服务器的,即服务器端。
mongo 是用来启动MongoDB shell的,是mongodb的命令行客户端。
brew install mongodb
3.报错处理
Error: No available formula with the name "mongoldb"
==> Searching for a previously deleted formula...
Warning: homebrew/core is shallow clone. To get complete history run:
git -C "$(brew --repo homebrew/core)" fetch --unshallow
解决:未安装Homebrew,参考1
Error: Xcode alone is not sufficient on Sierra.
解决: 安装xcode插件
xcode-select --install
解决完问题后重新执行brew install mongodb,等待安装完成。
4.修改环境变量
sudo vi ~/.bash_profile
//添加两行,保存
export MONGO_PATH=/usr/local/mongodb
export PATH=$PATH:$MONGO_PATH/bin
启动mongdb(mongod.conf文件中bindIp: 127.0.0.1意思是本机访问,如果是外网访问需要重新设置)
mongod --config /usr/local/etc/mongod.conf
打开http://localhost:27017/
若出现It looks like you are trying to access MongoDB over HTTP on the native driver port.
说明mongdb已经正常工作了