MongoDB is one of the highly used Document databases out there. Unlike traditional databases like MySQL and PostgreSQL, it does not store data using tables with rows and columns. It uses json like documents to store databases.
The major plus point from a developer perspective is the ability to create dynamic schema. So unlike other RDBMS systems building applications that can use mongodb is less complicated.
Initially MongoDB was a platform as a service product but later MongoDB Inc open sourced it in 2009. MongoDB is now adopted by many major and high traffic websites out there. Some of them are mentioned below.
Installing MongoDB in Ubuntu is a cakewalk, as the package is provided by the Ubuntu official apt repository. Being that said, i would suggest to use the official apt repo provided by mongoDB itself, to get the most latest and updated versions.
Any package management solution, should have a mechanism to verify the authenticity of the package downloaded by the user before it’s installed. Ubuntu And Redhat based distributions uses GPG keys to verify the signature of the packages downloaded. So in short we need to first import the key provided officially by MongoDB as shown below.
sudo apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv 7F0CEB10
The next step is to add URL path to our local apt configuration, so that apt program will know about the location from where it will fetch the packages during apt-get commands. This can be done as shown below.
echo “deb http://repo.mongodb.org/apt/ubuntu “$(lsb_release -sc)”/mongodb-org/3.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
The above command simply creates an apt list config file with MongoDB URL, our Ubuntu release details etc. The next thing to do apt-get update.
sudo apt-get update
The next step is to install MongoDB as shown below.
sudo apt-get install mongodb-org
Once the above command succeeds, you will have a system with mongodb and all its dependencies installed. You can now use the below service commands to start/stop/check status of mongoDB service.
service mongod status
service mongod start
service mongod stop
Now lets see the directories and locations where mongoDB stores data, logs, and binaries.
Please note the fact that the user which runs mongod service should have permission for read/write to the above mentioned directories.
Now let’s see some configuration parameters that can be modified as per your requirement in mongodb.
The main configuration file that the user can modify is “/etc/mongod.conf” file.
bind_ip = 127.0.0.1
You can modify the above to your server’s required interface IP address, so that mongodb only listens on that particular IP address. The default mongodb port is 27017. You can modify this by editing /etc/mongod.conf file with the below with whatever port you like as shown below.
port = 7878
You can also modify the location(inside /etc/mongod.conf) where mongodb by default stores its data as shown below.
dbpath=/path/to/storage
Log location and filename can be modified by using the below option inside /etc/mongod.conf
logpath = /var/log/mongodb/mongod.log
You can check the version of the installed mongodb by using the below command.
mongo –version
The default mongodb installation comes with password based authentication disabled. So we have to first create a root user with admin privileges and then enable authentication. This can be achieved as shown below.
As authentication is turned off by default, we can get into the mongodb without any password, by simply typing the below.
sudo mongo
Once the above command completes, you will be placed inside the mongodb shell. The next thing to do is to switch to admin database as shown below.
use admin
Now lets create the admin user by the following command.
db.createUser({user:”admin”,pwd:”secret”,roles:[{role:”root”, db:”admin”}]})
The above command creates an admin user with the username of admin, and assigns the role root. root is a built in role available in mongodb, which has the below permission set.
Once you have created the above admin user as shown below, you can exit from the mongodb console as shown below.
exit
Although we have created the admin user, we have yet not enabled authentication. Let’s enable authentication by modifying /etc/mongod.conf file, by uncommenting authentication parameter as shown below.
auth = true
Once the above line is in place, you can restart mongodb service as shown below.
sudo service mongod restart
Now you can login to mongodb with the newly created admin user with the below command.
mongo -u admin -p secret –authenticationDatabase admin
If you are from a traditional relational database background, you might be thinking of first creating a database, and then populating the required schema inside.
This is not the case with Mongodb. Also mongodb does not provide any command to create a database. Well, to put it correctly, mongodb does not require you to create it manually, as it will create it on the fly for you.
The very first time, when you try to save any data into a collection (table), inside a required database name, mongodb will create the table, and the database for you on the fly. Let’s see this practically.
First let’s get inside mongodb shell console using the mongo command.
sudo mongo
Now let’s issue the use database command as shown below.
> use testdb
switched to db testdb
Let’s now see the available databases with the below command.
> show dbs
admin 0.03145GB
local (empty)
So our database named testdb still does not exist inside mongodb. Keep the fact in mind that mongodb never complained when we used use testdb command.
Let’s make a collection(table) inside the testdb database, with some junk data.
> use testdb
switched to db testdb
>db.company.save( {companyname:”vmcentral”} )
The above command creates a sample collection named company and adds a value with the company name of vmcentral. Lets now see if the testdb is recognized and created.
> show dbs
admin 0.03145GB
local (empty)
testdb 0.03145GB
So as soon as we created a collection(table), mongodb created the database and the collection for us on the fly. Please note the fact that till we did not save anything to the database, mongodb did not create a database.