MongoDB Installation

MongoDB Installation

MongoDB Installation

MongoDB is a NoSQL database used for building modern applications. Follow this guide to install MongoDB on your Mac and start using it.

1. Install MongoDB on Mac (Homebrew Method)

Since you are using Homebrew, the easiest way to install MongoDB is through it.

A. Update Homebrew

First, update Homebrew to ensure you get the latest MongoDB version:

brew update

B. Install MongoDB Community Edition

Run the following command to install MongoDB:

brew install mongodb-community@7.0

🔹 7.0 is the latest version at the time of writing. You can check for newer versions here.

C. Start the MongoDB Service

After installation, start MongoDB using:

brew services start mongodb-community@7.0

This will run MongoDB in the background as a service.

To check if MongoDB is running, use:

brew services list

2. Verify the Installation

A. Check MongoDB Version

Confirm MongoDB is installed by running:

mongod --version

You should see an output like this:

db version v7.0.0

B. Connect to MongoDB Shell

To interact with MongoDB, open the shell:

mongosh

If connected successfully, you will see:

Current Mongosh Log ID: 123456789 Connecting to: mongodb://127.0.0.1:27017

Now, you can start using MongoDB commands.

3. Set Up the MongoDB Data Directory (If Needed)

By default, MongoDB stores data in /usr/local/var/mongodb. If MongoDB fails to start, manually create the directory:

mkdir -p /usr/local/var/mongodb

Then, restart MongoDB:

brew services restart mongodb-community@7.0

4. Create a Test Database

Inside mongosh, run:

use myTestDB db.myCollection.insertOne({ name: "John", age: 30 }) db.myCollection.find().pretty()

This confirms MongoDB is working correctly.

5. Stop and Restart MongoDB

A. Stop MongoDB

brew services stop mongodb-community@7.0

B. Restart MongoDB

brew services restart mongodb-community@7.0

6. Uninstall MongoDB (If Needed)

To remove MongoDB from your Mac:

brew uninstall mongodb-community@7.0 rm -rf /usr/local/var/mongodb

7. Next Steps

🔹 Install Mongoose (npm install mongoose) to use MongoDB with Node.js.
🔹 Set Up MongoDB Compass (GUI for MongoDB) from here.
🔹 Secure MongoDB with Authentication if deploying to production.

Would you like help setting up a MongoDB project? 🚀

Souy Soeng

Souy Soeng

Our website teaches and reads PHP, Framework Laravel, and how to download Admin template sample source code free. Thank you for being so supportive!

Github

Post a Comment

CAN FEEDBACK
close