Install MongoDB on Mac

Install MongoDB on Mac

Environment:
MacBook Pro (13-inch, M1, 2020)
Mac OS 11.3 (20E232)
MongoDB 4.4.6

1. Download MongoDB

Download address: https://www.mongodb.com/try/download/community After

Insert picture description here


selecting the version and system information, click Download to get an installation package.

Insert picture description here

2. Installation

Unzip the file: sudo tar -zxvf mongodb-macos-x86_64-4.4.6.tgz

Insert picture description here


rename the unzipped folder: sudo mv mongodb-macos-x86_64-4.4.6/ mongodb
move it /usr/localunder the folder:sudo mv mongodb /usr/local/mongodb

3. Import environment variables

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

4. Create a directory where logs and data are stored

# 数据
sudo mkdir -p /usr/local/var/mongodb

# 日志
sudo mkdir -p /usr/local/var/log/mongodb

# 为这两个目录添加读写权限
sudo chown liuweijia /usr/local/var/mongodb
sudo chown liuweijia /usr/local/var/log/mongodb

5. Start MongoDB

Note: The command is mongod, not mongodb.

Use the command to start MongoDB in the background:

mongod \
  --dbpath /usr/local/var/mongodb \
  --logpath /usr/local/var/log/mongodb/mongo.log \
  --fork
Insert picture description here


--dbpath: Data directory
--logpath: Log directory
--fork: Run in the background

6. Test whether MongoDB is started

Check whether MongoDB is started:

ps aux | grep -v grep | grep mongod
Insert picture description here


Use the mongocommand to start a terminal,

Insert picture description here

END.