Last active
January 8, 2020 19:30
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
#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