MongoDB has emerged as a popular choice for developers looking to work with NoSQL databases due to its flexibility and scalability. This guide will help you set up your own MongoDB environment, whether you’re working on a local machine, testing, or deploying a production system.
Before diving into the installation, make sure your system meets the following requirements:
Using Homebrew: If you have Homebrew installed, open your Terminal and run:
brew tap mongodb/brew brew install mongodb-community
Run MongoDB: Start the MongoDB server using:
brew services start mongodb/brew/mongodb-community
Verify Installation: Open a new terminal window and connect to the MongoDB shell using:
mongo
Download the Installer: Go to the MongoDB Download Center and download the Windows installer.
Run the Installer: Follow the prompts, and select 'Complete' when asked for the setup type.
Setup MongoDB as a Service: Open the Command Prompt as Administrator and run:
"C:\Program Files\MongoDB\Server\<version>\bin\mongod.exe" --install
Start the Service: Execute the following command to start MongoDB:
net start MongoDB
Verify Installation: Open the MongoDB shell using:
mongo
Import the Public Key: Run the following command:
wget -qO - https://www.mongodb.org/static/pgp/server-<version>.asc | sudo apt-key add -
Create the List File: Based on your version of Ubuntu, run:
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/<version>/multiverse amd64" | sudo tee /etc/apt/sources.list.d/mongodb-org-<version>.list
Install MongoDB: Update your package list and install:
sudo apt update sudo apt install -y mongodb-org
Start and Enable MongoDB:
sudo systemctl start mongod sudo systemctl enable mongod
Verify Installation: Access the MongoDB shell with:
mongo
MongoDB’s default configuration should be adequate for development, but you may want to tweak a few settings. MongoDB configuration is typically found in the /etc/mongod.conf
file on Linux or in the installation directory on Windows and macOS.
dbPath: The path where MongoDB stores its data (default is /data/db
).
To change the data directory, you can modify the dbPath
setting as follows:
storage: dbPath: /your/custom/path
bindIp: This option specifies the IP addresses MongoDB listens to. For local development, you might want it set to 127.0.0.1
, but for remote access, you can set it to 0.0.0.0
(ensure you understand the security implications).
net: bindIp: 0.0.0.0 port: 27017
Journaling: Ensure journaling is enabled for data durability during crashes, which is enabled by default.
After making changes to the configuration file, restart the MongoDB service for the changes to take effect.
sudo systemctl restart mongod # Linux brew services restart mongodb/brew/mongodb-community # macOS net stop MongoDB && net start MongoDB # Windows
With MongoDB up and running, let’s create a database and a collection. Open the MongoDB shell by running the mongo
command, and then you can execute the commands below:
Create a Database:
use myDatabase
Create a Collection and Insert a Document:
db.myCollection.insertOne({ name: "John Doe", age: 28, job: "Software Engineer" });
Find the Document:
db.myCollection.find().pretty()
This should output the document you just inserted in a readable format.
By following the above steps, you should now have a functional MongoDB environment ready for development! Adjust your configuration settings according to your project needs, and you'll be well on your way to building powerful applications using MongoDB!
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB
09/11/2024 | MongoDB