Installing MongoDB on Windows
- MongoDB can be installed on Windows using the MSI Installer or via Windows Services for easier management. Here’s a step-by-step guide to install MongoDB on a Windows OS, along with optional configurations.
System Requirements
- Supported Windows Versions:
- Windows 10 / 11 (64-bit)
- Windows Server 2016 / 2019 / 2022 (64-bit)
- MongoDB is not supported on 32-bit systems.
- Hardware Requirements:
- Minimum 2 GB of RAM (MongoDB will perform better with more memory).
- Disk space according to your data requirements.
Step-by-Step Installation
Step 1: Download MongoDB
- Go to MongoDB's official website:
- Visit the MongoDB Download Center.
- Select the version:
- Choose the Community Server edition.
- Select your version of Windows (e.g., Windows 64-bit x64).
- Download the MSI Installer:
- Download the .msi installer file for MongoDB.
Step 2: Run the Installer
- Start the Installation:
- Double-click the .msi file to launch the MongoDB installer.
- Setup Type:
- You’ll be prompted to choose the installation type:
- Complete: Installs MongoDB with all default settings (recommended).
- Custom: Allows you to customize the installation path and select optional features.
- Select Complete for the default setup, or Custom if you want to change the installation path or options like installing MongoDB Compass.
- Service Configuration:
- You will be asked if you want to run MongoDB as a service:
- Run MongoDB as a Service: This means MongoDB will start automatically when your computer boots.
- Run Manually: You can manually start and stop MongoDB.
- Select Install MongoDB as a Service (recommended for production environments).
- Data Directory and Log Directory:
- The installer will ask for paths where MongoDB will store data and logs.
- By default:
- Data directory: C:\Program Files\MongoDB\Server\<version>\data
- Log directory: C:\Program Files\MongoDB\Server\<version>\log
- You can accept the defaults or specify custom paths.
- MongoDB Compass:
- Optionally, you can install MongoDB Compass, which is a graphical interface for interacting with MongoDB.
- If you don’t need Compass, you can uncheck this option.
- Complete the Installation:
- After choosing your options, click Install.
- Wait for the installation process to complete.
Step 3: Set up Environment Variables (Optional but Recommended)
- To use MongoDB from the command line easily, it’s a good idea to add MongoDB’s bin folder to the system’s PATH environment variable.
- Locate the Installation Path:
- By default, MongoDB is installed in C:\Program Files\MongoDB\Server\<version>\bin.
- Add to PATH:
- Right-click This PC or My Computer on your desktop or in File Explorer.
- Click Properties → Advanced system settings → Environment Variables.
- Under System Variables, find the Path variable and click Edit.
- Click New and add the path to MongoDB’s bin folder (e.g., C:\Program Files\MongoDB\Server\<version>\bin).
- Click OK to save your changes.
Step 4: Verify the Installation
- Open Command Prompt:
- Press Win + R, type cmd, and press Enter.
- Check MongoDB Version:
- Run the following command to check if MongoDB is installed correctly:
mongod --version
- You should see the version of MongoDB that you installed.
- Start the MongoDB Server:
- You can start the MongoDB server manually using the following command:
mongod
- By default, MongoDB will look for the data directory in C:\data\db. If it doesn’t exist, create the directory:
mkdir C:\data\db
- If you installed MongoDB as a service, it will start automatically, so you don't need to manually run mongod.
Step 5: Connecting to MongoDB
- Start MongoDB:
- If you haven't installed MongoDB as a service, you'll need to manually start MongoDB by running the following command in a new terminal:
mongod
Step 6: Run MongoDB as a Service (Optional)
- If you installed MongoDB as a service, you can manage it using Windows services:
- Start the MongoDB Service:
- Open the Services app from the start menu.
- Look for the MongoDB service and click Start if it isn’t already running.
- Stop the MongoDB Service:
- To stop the service, right-click on MongoDB and select Stop.
Step 7: Uninstalling MongoDB (If Needed)
- Stop the MongoDB Service:
- If MongoDB is running as a service, stop it first.
- Uninstall from Control Panel:
- Go to Control Panel → Programs and Features.
- Find MongoDB in the list of installed programs, right-click, and choose Uninstall.
- Remove Data and Log Files (Optional):
- If you want to completely remove MongoDB, you can manually delete the data and log directories you created earlier.
Post Installation Configuration
- After successfully installing MongoDB, there are a few optional configurations to improve your experience:
- Change the Default Data Directory:
- By default, MongoDB stores its data in C:\data\db. You can change this by specifying a custom data directory in the MongoDB configuration file (mongod.cfg).
- Authentication:
- For added security, especially in production environments, enable authentication by creating admin users and enforcing role-based access control (RBAC).
- Backup:
- Set up regular backups of your MongoDB data to avoid data loss. MongoDB provides various methods such as mongodump for backing up your data.
Conclusion
- With these steps, you’ll have MongoDB up and running on your Windows machine. After installation, you can use the MongoDB shell to interact with the database, or use MongoDB Compass for a more user-friendly interface. Make sure to adjust the configuration based on your development or production environment for optimal performance and security.
No comments:
Post a Comment