414 views
asked in MongoDB by
Install MongoDB 8, Authentication and allow access from remote server on Ubuntu 24.04 LTS

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 Ubuntu 24.04 LTS*

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

MongoDB Database server:

Server IP: 192.168.224.134

Part 1:

MongoDB Database - Install and access from mongoDB 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:

MongoDB only supports the 64-bit versions of these platforms. To determine which Ubuntu release your host is running, run the following command on the host's terminal:

cat /etc/lsb-release

Step 1: Install MongoDB Community Edition in Ubuntu

1. Import the Public Key

From a terminal, install gnupg and curl if they are not already available:

sudo apt-get install gnupg curl

To import the MongoDB public GPG key, run the following command:

curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
   sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
   --dearmor

2. Create the List File

Create the list file /etc/apt/sources.list.d/mongodb-org-8.0.list for your version of Ubuntu.

Create the list file for Ubuntu 24.04 (Noble):

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.list

3.Reload the Package Database

Issue the following command to reload the local package database:

sudo apt-get update

4. Install MongoDB Community Server

You can install either the latest stable version of MongoDB or a specific version of MongoDB.

To install the latest stable version, issue the following

sudo apt-get install -y mongodb-org

--or--

To install a specific release, you must specify each component package individually along with the version number, as in the following example:

sudo apt-get install -y mongodb-org=8.0.0 mongodb-org-database=8.0.0 mongodb-org-server=8.0.0 mongodb-mongosh mongodb-org-mongos=8.0.0 mongodb-org-tools=8.0.0

Step 2: Run MongoDB Community Edition

1. Start MongoDB.

You can start the mongod process by issuing the following command:

sudo systemctl start mongod

If you receive an error similar to the following when starting mongod:

Failed to start mongod.service: Unit mongod.service not found.

Run the following command first:

sudo systemctl daemon-reload

Then run the start command above again.

2. Verify that MongoDB has started successfully.

sudo systemctl status mongod

You can optionally ensure that MongoDB will start following a system reboot by issuing the following command:

sudo systemctl enable mongod

3. Stop MongoDB.

As needed, you can stop the mongod process by issuing the following command:

sudo systemctl stop mongod

4. Restart MongoDB.

You can restart the mongod process by issuing the following command:

sudo systemctl restart mongod

You can follow the state of the process for errors or important messages by watching the output in the /var/log/mongodb/mongod.log file.

tail -f /var/log/mongodb/mongod.log

Step 3: Install MongoDB Compass

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

5. Begin using MongoDB.

Start a mongosh session on the same host machine as the mongod. You can run mongosh without any command-line options to connect to a mongod that is running on your localhost with default port 27017.

mongosh

Part 2: 

To allow MongoDB connections from a remote server or IP address, you'll need to make some changes to the MongoDB configuration and ensure that your system firewall or cloud security settings permit access. Here's a step-by-step guide on how to do this:

1. Modify MongoDB Configuration (mongod.conf)

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.conf).

Steps:

Locate the mongod.conf file:

On Linux, it's typically located at /etc/mongod.conf.

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

Look for the following line under the net section:

sudo vi /etc/mongod.conf

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.1.100), set it as follows:

bindIp: 127.0.0.1,192.168.1.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 Linux with ufw:

sudo ufw allow 27017

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.conf file, restart the MongoDB service for the changes to take effect.

On Linux:

sudo systemctl restart mongod

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.conf:

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"

example:

mongosh --host 127.0.0.1 --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

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.

#mongodb 

#mongodbtutorial 

#mongo

#mongosh

#mongodbtutorialforbeginners 

#chiragstutorial 

#chiragsdatabasetutorial 

#chirags 

#chiragsmahto

Install MongoDB 8,Authentication,MongoDB Tutorial,MongoDB Chirags Tutorial,MongoDB DBA Tutorial,Chirags Tutorial,Chirags DBA Tutorial,Chirag Mahto,Chitt Ranjan Mahto,Chirag DBA Tutorial,Database Tutorial,NoSQL Database Tutorial,MongoDB NoSQL Database,MongoDB Install,MongoDB Configuration,MongoDB Access,MongoDB Compas,Mongosh,Rockmongo,Mongo,MongoDB Authorization,MongoDB Authentication,MongoDB Authorisation

Most popular tags

laravel postgresql laravel-10 replication ha postgresql mongodb laravel-11 mongodb database mongodb tutorial ubuntu 24.04 lts streaming-replication mysql database laravel postgresql backup laravel login register logout database mysql php laravel 11 - login with otp valid for 10 minutes. user and admin registration user and admin login multiauth technlogy asp.net asp.net c# mysql master slave replication centos linux laravel sql server schedule backup autobackup postgresql django python haproxy load balancer install self sign ssl laravel 11 gaurds 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 configure replica laravel 11 socialite login with google account google login kubernetes (k8s) install nginx load balancer install install and configure .net 8.0 in ubuntu 24.04 lts php in iis php with iis php tutorial chirags php tutorials chirags php tutorial chirags tutorial laravel 11 guards mongodb sharding metabase business analytics metabase 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
...