77 views
asked in MongoDB by
Install MongoDB 8, Authentication and allow access from remote server on Windows

1 Answer

answered by

inchirags@gmail.com   Chirag's MongoDB DBA Tutorial         https://www.chirags.in

*****************************************************************************************

*   Install MongoDB 8, Authentication and allow access from remote server on Windows   *

*****************************************************************************************

Part 1:

MongoDB Database - Install and access in compass on Windows step by step process

Here's a step-by-step guide to installing MongoDB and accessing it using MongoDB Compass on your Windows machine:

Step 1: Install MongoDB on Windows

1. Download MongoDB:

• Visit the official MongoDB website: MongoDB Download Center.

https://www.mongodb.com/try/download/community

• Select the "Community Edition" and choose your Windows version. The latest version will automatically be selected.

• Click on the Download (msi) button to get the installer.

2. Install MongoDB:

• Once the MSI file is downloaded, double-click to run the installer.

• In the setup wizard, select the "Complete" installation option.

• Make sure "Install MongoDB as a Service" is selected, so MongoDB starts automatically with your system.

• You can choose to install MongoDB Compass along with the server (if not, you'll install it separately in Step 3).

• Proceed with the installation, leaving the default options as they are.

3.Configure the Database Path:

• MongoDB stores its data in a directory called data\db. You’ll need to create this directory:

1. Open File Explorer.

2. Go to the C: drive.

3. Create a new folder called data in C:\.

4. Inside the data folder, create another folder named db (i.e., C:\data\db).

5. Start the MongoDB Service:

• MongoDB should automatically run as a service, but to check or start it:

• Press Windows Key + R to open the Run dialog box.

• Type services.msc and hit Enter.

• Scroll down and look for the MongoDB Server service. Ensure it is running.

• If it’s not, right-click and select Start.

Step 2: Verify MongoDB Installation

1. Check MongoDB Version:

• Open the Command Prompt.

• Type the following command to check if MongoDB is installed and working:

mongosh --version

2. Access MongoDB using the Mongo Shell:

• In the Command Prompt, type mongosh to connect to the MongoDB server.

• If MongoDB is running, you should see the MongoDB shell prompt.

Step 3: Install MongoDB Compass

MongoDB Compass is a graphical tool that allows you to visualize and interact with your MongoDB database.

1. Download MongoDB Compass:

• Go to the MongoDB Compass Download page.

https://www.mongodb.com/try/download/compass

• Choose the latest version for Windows, then download and run the installer.

2. Install MongoDB Compass:

Follow the installation wizard to install Compass on your system.

Step 4: Access MongoDB via MongoDB Compass

1. Open MongoDB Compass:

Once installed, launch MongoDB Compass.

2. Connect to MongoDB:

• When Compass opens, it will prompt you to connect to a MongoDB instance.

• Enter the connection string. The default string for a local MongoDB instance is:

mongodb://localhost:27017

• Click on Connect.

Start Working with MongoDB:

• After connecting, MongoDB Compass will display the available databases.

• You can create new databases, view collections, and run queries using the GUI.

Step 5: Create and Manage Databases in MongoDB Compass

1. Create a New Database:

• In MongoDB Compass, click on the "Create Database" button.

• Provide a name for the new database and collection.

• Click Create Database.

2. Manage Data:

You can view collections, add new data, update documents, and more using the GUI.

Step 6: Stopping the MongoDB Service

1. Manually Stop the Service:

If you need to stop the MongoDB service, open Services (services.msc).

Right-click on MongoDB Server and choose Stop.

Part 2: 

Modify MongoDB Configuration (mongod.cfg)

MongoDB by default binds to localhost (127.0.0.1), which means it's only accessible locally. To allow remote connections, you'll need to modify the bind IP in the MongoDB configuration file (mongod.cfg).

Steps:

Locate the mongod.cfg file:

On Windows, it's generally found at 

C:\Program Files\MongoDB\Server\<version>\bin\mongod.cfg

Edit the mongod.cfg file: Open the file in your preferred text editor.

Look for the following line under the net section:

bindIp: 127.0.0.1

Modify it to bind to all IP addresses (or specify specific IP addresses). To allow connections from any IP:

bindIp: 0.0.0.0

If you want to restrict access to a specific IP (e.g., 192.168.224.100), set it as follows:

bindIp: 127.0.0.1,192.168.224.100

Save the changes.

2. Open MongoDB Port (Default: 27017) in Firewall

MongoDB typically uses port 27017. You need to ensure this port is open on your server.

For Windows Firewall:

Open Windows Defender Firewall.

Go to Advanced Settings.

Inbound Rules > New Rule.

Select Port.

Enter 27017 for the port.

Allow the connection for all profiles.

Name the rule "MongoDB".

If you are using cloud services (AWS, Azure, etc.), make sure the security group or firewall rule allows access to port 27017 from the remote IPs.

3. Restart MongoDB

After modifying the mongod.cfg file, restart the MongoDB service for the changes to take effect.

On Windows:

You can restart the MongoDB service through the Services management console or by running:

net stop MongoDB
net start MongoDB

4. Configure MongoDB User Authentication (Optional but Recommended)

Allowing remote access makes your MongoDB instance more vulnerable to unauthorized access. It’s highly recommended to set up user authentication to secure your database.

Create an Admin User:

Start the MongoDB shell:

mongosh

Switch to the admin database:

use admin

Create an admin user with username and password:

db.createUser({
  user: "admin",
  pwd: "admin@123",
  roles: [{ role: "userAdminAnyDatabase", db: "admin" }]
});

Enable authorization by adding the following line to mongod.cfg:

security:
  authorization: enabled

Restart MongoDB for the changes to take effect.

5. Connect to MongoDB Remotely

Now that MongoDB is configured to allow remote connections, you can connect from a remote machine.

Example of a Connection String:

mongosh --host your-server-ip --port 27017 -u "admin" -p "admin@123" --authenticationDatabase "admin"

Make sure to replace your-server-ip, admin, and admin@123 with your server's IP address, username, and password.

6. Test Remote Connection

Try to connect to MongoDB from a remote machine using the mongosh shell or a MongoDB client like MongoDB Compass.

Example (From Command Line):

mongosh --host <remote-ip> --port 27017

7. Additional Security: Set Up TLS/SSL (Optional)

For additional security, you can enable TLS/SSL encryption to secure data transferred between the MongoDB server and the client. This step involves generating SSL certificates and configuring MongoDB to use them.

For any doubts and query, please write on YouTube video comments section.

Note : Flow the Process shown in video.

Please, Subscribe and like for more videos:

https://youtube.com/@chiragstutorial

Don't forget to, Follow, Like,  Share &, Comment

Thanks & Regards,

Chitt Ranjan Mahto "Chirag"

_________________________________________________________________________________________

Note: All scripts used in this demo will be available in our website.

Link will be available in description.

Most popular tags

postgresql laravel replication ha postgresql laravel-10 mongodb ubuntu 24.04 lts mongodb database mongodb tutorial streaming-replication mysql laravel-11 database laravel postgresql backup database mysql php technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python user and admin registration user and admin login multiauth zabbix 7 how to install graylog on ubuntu 24.04 lts | step-by-step asp.net core mvc .net mvc network upload c# ssl integration sql server on ubuntu 22.04 lts mssql server ms sql server sql server user access in postgres mysql password change cent os linux laravel login register logout replica php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 gaurds laravel 11 guards mongodb sharding metabase business analytics metabase ubuntu 24.04 koha 24.05 postgresql 16 to postgresql 17 postgresql migration letsencrypt mongodb crud rocky linux laravel custom captcha laravel 11 captcha laravel captcha mongo dll php.ini debian 12 nginx apache nextcloud gitea in ubuntu git gitea npm error node js mysql ndb cluster mysql cluster ssl oracle login register logout in python debian windows shell batch file bat file time stamp date time shopping cart in laravel centos rhel swap memeory rhel 5.5 access configuration in postgresql hba configuration laravel multiple database configuration state city country dropdown live photo upload webcam captcha in laravel
...