MongoDB – Create New User

After the MongoDB installation, I continued with creating a new root user which carries the admin right with password. It is recommended to secure the database with password all the time.

To do so, I used the Mongo command shell to run the commands.
1. Create the root user.

db.createUser({user:"admin", pwd:"admin123", roles:[{role:"root", db:"admin"}]})

The command prompt will show success message once the new user is created.

2. Enable Mongodb authentication
Edit the Mongodb service file ‘/lib/systemd/system/mongod.service’ with your editor on the ‘ExecStart’ line 9, add the new option ‘–auth’.

ExecStart=/usr/bin/mongod --quiet --auth --config /etc/mongod.conf

And here is where I faced the same problem again, I am unable to edit the file using the text editor because of the permission. If you able to edit, then continue with below steps.

3. Reload the systemd service using systemd daemon-reload
4. Restart MongoDB service.
5. Connect to the Mongodb shell with this command:
mongo -u admin -p admin123 –authenticationDatabase admin

Fill in using the userID and password set when we create new root user. Even I did not change the file, mongod.service, I am still able to connect to the MongoDB with my own userID and password. Below, showed the success login.

Lastly, you can show or list the databases by using command,

show dbs
Advertisements

MongoDB – Installation

Today I am going to try the MongoDB installation on my Linux machine which I installed in my VMware. I am not going to do the same installation on my Windows’ laptop. I am expected to learn and workaround with the MongoDB in my virtual machine only.

Let us get started, it is simple installation and just a small confusion on one of the configuration parts, not to worry about it too much at this point. As I googled how to install MongoDB on Ubuntu, there is a list of guides to follow and I personally followed these two links:
1. From the MongoDB website: https://docs.mongodb.com/v3.2/tutorial/install-mongodb-on-ubuntu/
2. From a tutorial website: https://www.howtoforge.com/tutorial/install-mongodb-on-ubuntu-16.04/

Both are guiding us on how to install on the Ubuntu 16.04. Just follow the commands given in the website and I will just briefly mention what I do on each step.

1. Import the public key.
2. Create source list file MongoDB. I used below command, but using my Ubuntu version, Xenial,

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

Else, the default command is,

echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list

3. Update the repository.
4. Install MongoDB.
At this step, it asks us to create a new mongodb systemd service file in the ‘/lib/systemd/system’ directory. Below is the instruction:
Go to that directory and create the new mongodb service file ‘mongod.service’ with vim.

cd /lib/systemd/system/
vim mongod.service

In the edit mode (command prompt), I am unable to edit anything in which I should paste the setting on it and save. I have no idea how the vim works. Maybe you can enlighten me on this part.

I tried to use the text editor and I am unable to save the file because of permission issue. I did not change the file, anyway, and proceed to update the systemd service using the given command line.
5. Start Mongodb and add it as service to be started at boot time.
6. Now check that Mongodb has been started on port 27017 with the netstat command.

You can check the MongoDB’s log at /var/log/mongodb/mongod.log and the port configured in /etc/mongod.conf, 27017 by default.

The installation is completed. Few commands to be remember:
1. Start service – sudo service mongod start
2. Stop service – sudo service mongod stop (or control-C)
3. Restart service – sudo service mongod restart
4. Go to Mongo shellmongo

MongoDB – GUI

After I finished both installation and new user creation, the next thing which comes into my mind is, does MongoDB has GUI? Coming from the MSSQL background, the SQL management tool is very useful for me and for now, I think I cannot live without it when I start learning MongoDB. I am not sure if this is a correct way of thinking and using the MongoDB. Should the MongoDB required a GUI tool to use, please advise me.

I do not have any idea which GUI is good and a search from the Internet returns Robomongo as the first link in the list. So, I think I will give a try to install it in my virtual machine and test to login using the userID and password I created and start learning and using the database.

Installation of Robomongo
The installar can be downloaded from its website and the installation is done pretty fast in Linux as usual. Then, launch the application and it first glance of look as below:

Create MongoDB Connection
The next step is create new MongoDB connection. It requires to fill up the Name of the connection and we can test the connection to port 27017 is working fine or not but clicking the ‘Test’ button.

Upon receiving the success message, we can proceed to set the authentication.

Test again the authentication and verified it is successful before we save the setting. I am not sure why whenever I connect to the database it does not prompt me for the userID and password. Is it meant to be this way? I will update on this part after my research. Stay tuned!