Installing MongoDB on Mac OSX Platform
MongoDB provides a 64-bit installation package for the OSX platform; you can download the installation package from the official website.
Download address:https://www.mongodb.com/try/download/community

Starting from MongoDB 3.0, only OS X 10.7 (Lion) and newer system versions are supported.
Next, we use the curl command to download and install:
# cd /usr/local
cd /usr/local
# 下载
sudo curl -O https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.9.tgz
# 解压
sudo tar -zxvf mongodb-osx-ssl-x86_64-4.0.9.tgz
# Rename to mongodb directory
sudo mv mongodb-osx-x86_64-4.0.9/ mongodb
After installation, we can add the MongoDB binary command file directory (installation directory/bin) to the PATH:
export PATH=/usr/local/mongodb/bin:$PATH
Create directories for logs and data storage:
-
Data storage path:
sudo mkdir -p /usr/local/var/mongodb
-
Log file path:
sudo mkdir -p /usr/local/var/log/mongodb
Next, make sure the current user has read and write permissions for the above two directories:
sudo chown runoob /usr/local/var/mongodb
sudo chown runoob /usr/local/var/log/mongodb
Create a log file:
sudo touch /usr/local/var/log/mongodb/mongo.log
sudo chown runoob /usr/local/var/log/mongodb/mongo.log
the above runoob is the user on my computer; you need to modify it according to your current username.
Next, we use the following command to start mongodb in the background:
mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
- --dbpath Set the data storage directory
- --logpath Set the log storage directory
- --fork Run in the background
If you don't want to run in the background, but want to view the running process on the console, you can directly set the configuration file to start:
mongod --config /usr/local/etc/mongod.conf
Check whether the mongod service has started:
ps aux | grep -v grep | grep mongod
Using the above command, if you see a mongod record, it means the operation was successful.
After starting, we can use the mongo command to open a terminal:
$ cd /usr/local/mongodb/bin
$ ./mongo
MongoDB shell version v4.0.9
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("3c12bf4f-695c-48b2-b160-8420110ccdcf") }
MongoDB server version: 4.0.9
……
> 1 + 1
2
>
MongoDB 6.0 and later made significant changes; MongoDB no longer installs the shell tool by default, and you need to install an additional shell tool:MongoDB Shell
Install using brew
In addition, you can also use OSX's brew to install mongodb:
brew tap mongodb/brew
brew install mongodb-community@4.4
after the @ symbol 4.4 is the latest version number.
Installation information:
- Configuration file:/usr/local/etc/mongod.conf
- Log file path:/usr/local/var/log/mongodb
- Data storage path:/usr/local/var/mongodb
Running MongoDB
We can use the brew command or the mongod command to start the service.
brew start:
brew services start mongodb-community@4.4
brew stop:
brew services stop mongodb-community@4.4
mongod command in background process mode:
mongod --config /usr/local/etc/mongod.conf --fork
To close this type of startup, you can enter the mongo shell console to do so:
> db.adminCommand({ "shutdown" : 1 })