Skip to content

Instantly share code, notes, and snippets.

@anis016
Created September 8, 2017 02:11
Show Gist options
  • Save anis016/220295fa8caa5ed41137c521878cb684 to your computer and use it in GitHub Desktop.
Save anis016/220295fa8caa5ed41137c521878cb684 to your computer and use it in GitHub Desktop.
MongoDB Installations/Configurations/Dump/Restore
## Installing MONGODB
Specific Version to install: 3.2.16
Ubuntu 16.04 OS
Source: https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/
1. Import the public key used by the package management system.
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
2. Create a list file for MongoDB [Ubuntu 16.04]
$ echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
3. Reload local package database
$ sudo apt-get update
4. Install the MongoDB packages
$ sudo apt-get install -y mongodb-org=3.2.16 mongodb-org-server=3.2.16 mongodb-org-shell=3.2.16 mongodb-org-mongos=3.2.16 mongodb-org-tools=3.2.16
## Configuration MONGODB
(Ubuntu 16.04-only) Create systemd service file if not present
$ touch /lib/systemd/system/mongod.service
$ vi mongod.service
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
Documentation=https://docs.mongodb.org/manual
[Service]
User=mongodb
Group=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
## Start MongoDB
$ sudo service mongod start
## Stop MongoDB
$ sudo service mongod stop
## Check if MongoDB running
$ tail -f /var/log/mongodb/mongod.log
check "[initandlisten] waiting for connections on port <port>"
## Start Mongo Command Line Shell
$ mongo
###################################################################################################################
## Open Terminal 1 and start the mongo demon
$ mongod
## Open Terminal 2 and create new folder
$ mkdir backup
## Start the mongo and get the database
$ mongo
> show dbs
## Take the dump using as below
$ mongodump --db businessData --collection newsCollection --out backup/
$ mongodump --db businessData --collection stockCollection --out backup/
## Restore the dump using as below
$ mongorestore --db businessData Python_Workspace/python_projects/bdm_tp/mongo_dump/newsCollection.bson
$ mongorestore --db businessData Python_Workspace/python_projects/bdm_tp/mongo_dump/stockCollection.bson
## Check all the data in the collections
> show collections
> db.newsCollection.find().pretty()
mongo database dump: https://www.dropbox.com/s/1s8pmzlzdemyba9/businessData.tar.gz?dl=0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment