Skip to content

Instantly share code, notes, and snippets.

@linuxoracledev
Last active January 8, 2020 19:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save linuxoracledev/bc386f57283743f7cba09d25c8b494a7 to your computer and use it in GitHub Desktop.
Save linuxoracledev/bc386f57283743f7cba09d25c8b494a7 to your computer and use it in GitHub Desktop.
How to Install and Confugure MongoDB, Create Database and User on Ubuntu 18.04
#Update the Ubuntu packages list
sudo apt update
#Install the MongoDB package
sudo apt install -y mongodb
#Check the Service and Database
sudo systemctl status mongodb
#View database version, server address and port
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
#Verify the status of the service, type
sudo systemctl status mongodb
#Optional
#Stop, start, restart the server
sudo systemctl stop mongodb
sudo systemctl start mongodb
sudo systemctl restart mongodb
#Disable, enable the automatic startup
sudo systemctl disable mongodb
sudo systemctl enable mongodb
#Adjusting the Firewall
sudo ufw allow from your_other_server_ip/32 to any port 27017
#Verify the change in firewall settings
sudo ufw status
#Allow remote connections
sudo nano /etc/mongodb.conf
#Add your server’s IP address to the bindIP value:
# bind_ip = 127.0.0.1,your_server_ip
#Restart MongoDB
sudo systemctl restart mongodb
#Start Mongo Shell
mongo
#Create a new_database in MongoDB, firstly it needs to select this database
use new_database
#Check the currently selected database
db
#List all databases in MangoDB
show dbs
#It will not show new_database
#reate a new_collection and insert a document
db.new_collection.insert({ some_key: "some_value" })
#List all databases in MangoDB
show dbs
# create a new_user for new_database with readWrite permissions in MongoDB
# db.createUser(
# {
# user: "new_user",
# pwd: "some_password",
# roles: [ { role: "readWrite", db: "new_database" } ]
# }
# )
#List all collections in the selected MangoDB database
show collections
#Delete selected MongoBD database
# db.dropDatabase()
@linuxoracledev
Copy link
Author

#Uninstall MongoDB

#Stop MongoDB
sudo service mongod stop

#Remove Packages.
sudo apt-get purge mongodb-org*

#Remove Data Directories
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment