Install Homebrew if not already installed, then run brew tap mongodb/brew and brew install mongodb-community to install MongoDB. 2. Start the service with brew services start mongodb-community, which runs mongod in the background and enables auto-start on boot. 3. Connect using the MongoDB shell by running mongosh, which connects to mongodb://127.0.0.1:27017 by default. 4. Optionally configure MongoDB by editing /usr/local/etc/mongod.conf and ensure proper permissions with sudo chown -R $(whoami) on the data and config directories. 5. Manage the service using brew services stop mongodb-community to stop or brew services restart mongodb-community to restart. Common issues include permission errors, fixed via chown, port conflicts on 27017, resolved with lsof -i :27017, and missing mongosh, which can be installed separately using brew install mongosh. MongoDB is now successfully set up and ready for use on macOS.
Setting up MongoDB on a Mac is straightforward, especially with tools like Homebrew. Here’s a clear, step-by-step guide to get MongoDB installed and running on your macOS system.

1. Install MongoDB Using Homebrew
The easiest way to install MongoDB on Mac is through Homebrew, a popular package manager for macOS.
First, make sure you have Homebrew installed. If not, install it from brew.sh by running:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Once Homebrew is ready, install MongoDB:
brew tap mongodb/brew brew install mongodb-community
Note: This installs the community edition from MongoDB’s official tap, which keeps the version up to date.
2. Start MongoDB Service
After installation, you can start MongoDB as a background service:
brew services start mongodb-community
This command:
- Starts the MongoDB daemon (
mongod
) - Ensures it runs automatically on startup
To verify it’s running:
brew services list | grep mongodb
You should see started
under the status.
Alternatively, you can run MongoDB manually (without auto-restart):
mongod --config /usr/local/etc/mongod.conf
3. Connect to MongoDB
Once the server is running, open a new terminal window and connect using the mongo
shell:
mongosh
Note: Recent versions use
mongosh
(MongoDB Shell) instead of the oldermongo
. It should have been installed automatically with the community package.
You’ll see output like:
Current Mongosh Log ID: ... Connecting to: mongodb://127.0.0.1:27017 Using MongoDB: 7.0.0 Using Mongosh: 2.0.0
Now you’re connected and can start using MongoDB commands.
Try:
show dbs
4. Basic Configuration (Optional)
The default configuration file is located at:
/usr/local/etc/mongod.conf
You can edit this file to change:
- Data directory (
storage.dbPath
) - Port (
net.port
) - Bind IP (
net.bindIp
)
Make sure the data directory (usually /usr/local/var/mongodb
) has correct permissions:
sudo chown -R $(whoami) /usr/local/var/mongodb sudo chown -R $(whoami) /usr/local/etc/mongod.conf
5. Stop or Restart MongoDB
To stop the service:
brew services stop mongodb-community
To restart:
brew services restart mongodb-community
Troubleshooting Common Issues
-
"Permission denied" on data directory: Fix ownership with
chown
as shown above. -
Port 27017 already in use: Check with
lsof -i :27017
, then kill the process if needed. -
mongosh not found: Install it manually with
brew install mongosh
.
That’s it. You now have MongoDB up and running on your Mac. Use mongosh
to create databases, insert data, and test queries. No need for Docker or manual downloads—Homebrew handles the heavy lifting.
The above is the detailed content of Setting Up MongoDB on a Mac. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

To share files between two Macs on the same network, first make sure that the two devices are connected to the same LAN, then enable file sharing on the host, set shared folders and access permissions, and then connect from another Mac through the access sidebar or manually enter the SMB address, and finally you can browse, copy or transfer files. Common problems include waiting or restarting the network when the device cannot be discovered. If the permissions are incorrect, you need to check the username, password and sharing settings. If the connection fails, you can try the SMB address. If the firewall blocks, you can temporarily close the test.

MongoDBAtlas' free hierarchy has many limitations in performance, availability, usage restrictions and storage, and is not suitable for production environments. First, the M0 cluster shared CPU resources it provides, with only 512MB of memory and up to 2GB of storage, making it difficult to support real-time performance or data growth; secondly, the lack of high-availability architectures such as multi-node replica sets and automatic failover, which may lead to service interruption during maintenance or failure; further, hourly read and write operations are limited, the number of connections and bandwidth are also limited, and the current limit can be triggered; finally, the backup function is limited, and the storage limit is easily exhausted due to indexing or file storage, so it is only suitable for demonstration or small personal projects.

To share your Mac screen with others, you can choose the appropriate method according to different scenarios. First, through video conferencing tools such as Zoom, click the "Share" button to select the screen or a specific window for sharing; second, use Messages or FaceTime to initiate a screen sharing session, click the " " to select "Screen Sharing" and invite the other party to view or control it; third, use the "Screen Sharing" App that comes with the system to enable this function on the target Mac and connect to the server address through Finder to achieve remote access; in addition, you need to pay attention to details such as permission settings, privacy protection and network performance impact.

Mac screenshots themselves do not support scrolling screenshots, but can be implemented through system functions or third-party tools. 1. Use the screenshot tool in macOS to manually splice the preview app: take a segmented screenshot and leave an overlapping area. Drag in the picture through the preview app to export it as a PDF or picture. 2. Safari browser native scrolling screenshots: In macOSMojave and above, press Cmd Shift 4 spaces and click "Enterprise Page" to automatically generate a complete screenshot. 3. Automatic scrolling screenshots for third-party tools: such as Lightshot, Snagit, Shottr, supports shortcut key operations and automatic scrolling screenshots, suitable for frequent use scenarios. Just choose the right method according to your needs.

MongoDB's RBAC manages database access through role assignment permissions. Its core mechanism is to assign the role of a predefined set of permissions to the user, thereby determining the operations and scope it can perform. Roles are like positions, such as "read-only" or "administrator", built-in roles meet common needs, and custom roles can also be created. Permissions are composed of operations (such as insert, find) and resources (such as collections, databases), such as allowing queries to be executed on a specific collection. Commonly used built-in roles include read, readWrite, dbAdmin, userAdmin and clusterAdmin. When creating a user, you need to specify the role and its scope of action. For example, Jane can have read and write rights in the sales library, and inve

To find the Library folder on your Mac, you can follow the following steps: 1. Open the Finder window; 2. Click "Go" in the top menu bar; 3. Press and hold the Option (?) key to make the "Library" option appear; 4. Click to enter the Library folder. This folder contains subfolders such as application support files, caches, preferences, logs, and keychains, and is suitable for debugging applications, clearing caches, or managing extensions. If you need frequent access, you can run the "chflagsnohidden~/Library/" command through the terminal to make it permanently displayed. If you hide it again, use "chflagshidden~/Library/".

To change the default Mac application, first, for the file type: right-click the file → "Show introduction" → "Open method" and select the application and click "Change all"; secondly, for browser, email and other system services, you can specify it separately in "System Settings" → "Default Applications"; if the target application does not appear, you can try to run the program manually, register or uninstall the reinstallation application in your preferences.

MongoDBconnectionpoolingworksbymaintainingreusabledatabaseconnectionstoimproveperformance.1)Driverscreateinitialconnectionsatappstart.2)Whenaqueryruns,anidleconnectionistakenfromthepool.3)Afteruse,theconnectionreturnstothepoolinsteadofclosing.4)Ifall
