How to install local MongoDB on Mac, Ubuntu, CentOS, Windows
This guide covers how to install a local MongoDB on Mac, Ubuntu, CentOS, or Windows.
macOS
Homebrew (Recommended)
-
Install Homebrew (if not already installed):
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
-
Install MongoDB:
brew tap mongodb/brew brew install mongodb-community
-
Start MongoDB Service:
brew services start mongodb-community
Troubleshooting on macOS
- If MongoDB fails to start, check logs:
brew services list cat $(brew --prefix)/var/log/mongodb/mongo.log
- Ensure data directory has proper permissions:
sudo chown -R $(whoami) $(brew --prefix)/var/mongodb
Ubuntu
apt
-
Import MongoDB public GPG key:
wget -qO- https://www.mongodb.org/static/pgp/server-7.0.asc | sudo apt-key add -
For newer Ubuntu versions, use:
wget -qO- https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg --dearmor --output /usr/share/keyrings/mongodb-archive-keyring.gpg
-
Create a list file for MongoDB:
# For Ubuntu 22.04 (Jammy) echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-archive-keyring.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Replace "jammy" with your Ubuntu codename (bionic for 18.04, focal for 20.04)
-
Update package database:
sudo apt update
-
Install MongoDB:
sudo apt install -y mongodb-org
-
Start and enable MongoDB service:
sudo systemctl start mongod sudo systemctl enable mongod
Troubleshooting on Ubuntu
- Check service status:
sudo systemctl status mongod
- View logs for errors:
sudo journalctl -u mongod
- If service won't start, check directory permissions:
sudo chown -R mongodb:mongodb /var/lib/mongodb sudo chown mongodb:mongodb /tmp/mongodb-27017.sock
CentOS
yum/dnf
-
Create a repository file for MongoDB:
sudo tee /etc/yum.repos.d/mongodb-org-7.0.repo <<EOF [mongodb-org-7.0] name=MongoDB Repository baseurl=https://repo.mongodb.org/yum/redhat/\$releasever/mongodb-org/7.0/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://www.mongodb.org/static/pgp/server-7.0.asc EOF
-
Install MongoDB:
# For CentOS 8 or newer sudo dnf install -y mongodb-org # For CentOS 7 sudo yum install -y mongodb-org
-
Start and enable MongoDB service:
sudo systemctl start mongod sudo systemctl enable mongod
Troubleshooting on CentOS
- Check service status:
sudo systemctl status mongod
- For SELinux-related issues:
sudo setenforce 0 # Temporarily disable SELinux
- Configure firewall:
sudo firewall-cmd --permanent --add-port=27017/tcp sudo firewall-cmd --reload
Installing on Windows
Installer
-
Download MongoDB installer from the official website
- Select "Windows" as the platform
- Choose the MSI package
- Click "Download"
-
Run the installer:
- Double-click the downloaded MSI file
- Follow the installation wizard
- Select "Complete" installation
- You can choose to install MongoDB Compass (GUI tool)
-
Verify the service:
- MongoDB should be installed as a Windows service
- Open Services (press Win+R, type "services.msc")
- Look for "MongoDB Server" service and ensure it's running
Chocolatey
-
Install Chocolatey (if not already installed):
- Open PowerShell as Administrator
- Run:
Set-ExecutionPolicy Bypass -Scope Process -Force [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072 iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))
-
Install MongoDB:
choco install mongodb
Troubleshooting on Windows
- If service fails to start, check Event Viewer for errors
- Ensure data directory exists and has proper permissions:
mkdir C:\data\db
- Check configuration file at:
C:\Program Files\MongoDB\Server\7.0\bin\mongod.cfg
Verifying Your Installation
To install mongosh
, you can refer to this post.
-
Check if MongoDB is running:
# Linux/macOS mongosh # Windows (Command Prompt or PowerShell) mongosh
-
Test basic operations:
// Show databases show dbs // Create/switch to a test database use test_db // Insert a document db.users.insertOne({ name: "Test User", email: "[email protected]" }) // Query documents db.users.find() // Exit mongosh exit
Basic Configuration
Data Directory
By default, MongoDB stores data in:
- macOS:
/usr/local/var/mongodb
(Homebrew) - Ubuntu/CentOS:
/var/lib/mongodb
- Windows:
C:\Program Files\MongoDB\Server\7.0\data
Configuration File
MongoDB's configuration file is located at:
- macOS:
/usr/local/etc/mongod.conf
(Homebrew) - Ubuntu/CentOS:
/etc/mongod.conf
- Windows:
C:\Program Files\MongoDB\Server\7.0\bin\mongod.cfg
Securing MongoDB
For basic security, create an admin user:
use admin
db.createUser({
user: "adminUser",
pwd: "securePassword",
roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
})
Then enable authentication in your configuration file:
security:
authorization: enabled