Install MongoDB
sudo apt-get install gnupg curl curl -fsSL https://www.mongodb.org/static/pgp/server-8.0.asc | \
sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg \
--dearmorCreate the MongoDB list file.
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/8.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.0.listUpdate package lists and install MongoDB.
sudo apt-get update sudo apt-get install -y mongodb-orgStart and enable MongoDB service.
sudo systemctl daemon-reload sudo systemctl start mongod sudo systemctl enable mongodVerify that MongoDB has started successfully.
sudo systemctl status mongodMongoDB URI to connect with projects.
mongodb://127.0.0.1:27017Enable firewall
sudo ufw enableCheck Port is Allowed through Firewall
sudo ufw statusIf Port is not Allowed then Allow it through Firewall
sudo ufw allow 27017 sudo ufw allow 'OpenSSH'Restart MongoDB
sudo service mongod restartConnect to MongoDB shell
mongoshShow database
show dbsChange to admin database
use adminCreate superuser with all privileges
db.createUser({user: "username-here" , pwd: passwordPrompt() , roles: ["root"]})Now Exit mongosh shell
.exitEnable Authorization removing comment
sudo nano /etc/mongod.conf security:
authorization: enabledRestart MongoDB
sudo service mongod restartTo Access Mongo Shell as Super User use this command:
mongosh --port 27017 --authenticationDatabase "admin" -u "username-here" -p "password-here"Create Database & User for project:
use database_name db.createUser({user:"username_here", pwd:passwordPrompt(), roles:[{role:"readWrite", db:"database_name"}]})Now Exit mongosh shell
.exitMongoDB URI to Connect with projects:
mongodb://username-here:password-here@127.0.0.1:27017/database_name