Installing MongoDB on Mac OSX Platform
MongoDB offers a 64-bit installation package for the OSX platform, which you can download from the official website.
Download link: https://www.mongodb.com/download-center#community
Starting from MongoDB 3.0, it only supports OS X 10.7 (Lion) and later versions.
Next, we will use the curl command to download the installation:
# Navigate to /usr/local
cd /usr/local
# Download
sudo curl -O https://fastdl.mongodb.org/osx/mongodb-osx-ssl-x86_64-4.0.9.tgz
# Extract
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, ensure the current user has read and write permissions for these two directories:
sudo chown tutorialpro /usr/local/var/mongodb
sudo chown tutorialpro /usr/local/var/log/mongodb
tutorialpro is the username on my computer; you need to replace it with your current username.
Next, we will start mongodb in the background with the following command:
mongod --dbpath /usr/local/var/mongodb --logpath /usr/local/var/log/mongodb/mongo.log --fork
- --dbpath sets the data storage directory
- --logpath sets the log storage directory
- --fork runs in the background
If you do not want to run in the background but instead view the process in the console, you can directly start with the configuration file:
mongod --config /usr/local/etc/mongod.conf
Check if the mongod service is running:
ps aux | grep -v grep | grep mongod
If the above command shows a mongod record, it indicates successful running.
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
>
Installing with brew
Alternatively, you can install mongodb using brew on OSX:
brew tap mongodb/brew
brew install [email protected]
The 4.4 following the @
symbol is the latest version number.
Installation details:
- 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 start the service using the brew command or the mongod command.
Start with brew:
brew services start [email protected]
Stop with brew:
brew services stop [email protected]
Start with mongod command in the background:
mongod --config /usr/local/etc/mongod.conf --fork
db.adminCommand({ "shutdown" : 1 })