Installing MongoDB on Ubuntu 16.04

Posted on Saturday, July 8, 2017


In this tutorial I am going to be installing MongoDB 3.4 on Ubuntu 16.04 and fiddle with it a little.

Checking the default install for mongoDB on Ubuntu 16.04


  > sudo apt-cache madison mongodb



Looks like it installs 2.6.10, since I want 3.4, I can't use a simple apt-get.






Installing mongoDB


Run these commands to install a key and update the (Found at https://docs.pritunl.com/docs/installation [1])


  > sudo su - root
 > echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" > /etc/apt/sources.list.d/mongodb-org-3.4.list




  > apt-key adv --keyserver hkp://keyserver.ubuntu.com --recv 0C49F3730359A14518585931BC711F9BA15703C6





Now update and install


  > apt-get --assume-yes update
  > apt-get --assume-yes upgrade
  > apt-get --assume-yes install mongodb-org









Let me check the version


  > mongod --version





now its installed let me go back to my regular user and start it up via SystemD


  > sudo systemctl start mongod.service




Now log into mongo via


  > mongo





Now you are in the mongoDB shell.
Of course you probably got many, many warnings pop up since nothing has been set up yet.






To get out of this shell run the following


  > quit()





Set up mongod to auto start when the system starts up.  To do this you need to enable it.


  > sudo systemctl enable mongod





I like to reboot the system to confirm


  > sudo reboot


Log back in and confirm


  > sudo systemctl status mongod




Looks good J




Dealing with the Warnings


I am new to mongoDB and new to installing it.  I want to go through each of these warnings I get when I run mongo.  I want to figure out what they are and fix them.



  > mongo








** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
**          See http://dochub.mongodb.org/core/prodnotes-filesystem

** WARNING: Access control is not enabled for the database.
**          Read and write access to data and configuration is unrestricted.

** WARNING: /sys/kernel/mm/transparent_hugepage/enabled is 'always'.
**        We suggest setting it to 'never'

** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
**        We suggest setting it to 'never'


Those are the warnings I get






Using the XFS filesystem is strongly recommended


Checking out the site they link in the warning https://docs.mongodb.com/manual/administration/production-notes/#kernel-and-file-systems [2] they mention that mongo runs better under XFS versus EXT4.

My current install of MongoDB is ext4.

A simple way to list your filesystem type is to run this command


  > df -T









I am going to add an extra drive to this machine and format it to an XFS filesystem. 


Add the drive and reboot


  > sudo reboot


Make sure it shows up


  > ls /dev/sd*




Three it is


Now partition it


  > sudo fdisk /dev/sdb


When prompted answer it in this order

n, p 1, default, default, w





Format the drive in XFS format


  > sudo mkfs.xfs -L mongoDB /dev/sdb1




Edit /etc/fstab


  > sudo vi /etc/fstab





Add this to the end


/dev/sdb1  /mongoDB                xfs   nofail,auto,noatime,rw,user  0  0





Now reboot


  > sudo reboot




There we go an xfs formatted drive.
Now to use it for mongodb


Now to change the default storage location



  > sudo vi /etc/mongod.conf


Update the dbPath to the XFS drive /mongoDB/data



# Where and how to store data.
storage:
  dbPath: /mongoDB/data
  journal:
    enabled: true




Make the /mongoDB/data folder and move old files


  > sudo mkdir /mongoDB/data
  > sudo cp -r /var/lib/mongodb/* /mongoDB/data/
  > sudo chown -R mongodb:mongodb /mongoDB/data


Restart mongod


  > sudo systemctl restart mongod.service


Check the status to make sure its running.


  > sudo systemctl status mongod.service





Try connecting to mongo and see if the XFS warning has gone away




  > mongo




Yep down to three warnings now.






/sys/kernel


What do these /sys/kernel warnings mean and how can you fix them?



Let's look at the first one.   I found this page https://docs.mongodb.com/manual/tutorial/transparent-huge-pages/ [5]
Which has this explanation.

Transparent Huge Pages (THP) is a Linux memory management system that reduces the overhead of Translation Lookaside Buffer (TLB) lookups on machines with large amounts of memory by using larger memory pages.

However, database workloads often perform poorly with THP, because they tend to have sparse rather than contiguous memory access patterns. You should disable THP on Linux machines to ensure best performance with MongoDB.

So, how do set this to never in Ubuntu 16.04?  I found this on stackoverflow https://askubuntu.com/questions/597372/how-do-i-modify-sys-kernel-mm-transparent-hugepage-enabled [6].  They suggest installing systfsutils.





  > sudo apt-get install sysfsutils


Then edit /etc/sysfs.conf


  > sudo vi /etc/sysfs.conf


Append this line to the end



kernel/mm/transparent_hugepage/enabled = never






Now reboot


  > sudo reboot now


Now try it



  > mongo




Hey down to just two warnings now.



Now for the

** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.


Edit /etc/sysfs.conf


  > sudo vi /etc/sysfs.conf


Append this line to the end


kernel/mm/transparent_hugepage/defrag = never








Now reboot


  > sudo reboot now


Now try it



  > mongo





Wahoo down to just the last one.






Access Control is not enabled


Looks like I need to create some users who have permissions to databases.



1.      Log in


  > mongo


2.      Create an admin SuperUser

Switch to admin Database


  > use admin






Add an admin user


  > db.createUser(
  {
    user: "myAdmin",
    pwd: "my_password",
    roles: [
                { role: "userAdminAnyDatabase", db: "admin" },
                { role: "readWriteAnyDatabase", db: "admin" },
                { role: "dbAdminAnyDatabase", db: "admin" },
                { role: "clusterAdmin", db: "admin" }
              ]
  }
)







Create a Database named test_database.


  > use test_database
  > db.names.insert(
      [
         {"name": "Jeff", "age": 21},
         {"name": "Matt", "age": 18},
         {"name": "Lisa", "age": 31} 
      ]
           )







Then create a user, myTester who only has read/write access to a database named test_database.


  > db.createUser(
  {
    user: "myTester",
    pwd: "my_password",
    roles: [ { role: "readWrite", db: "test_database" },
             { role: "read", db: "reporting" } ]
  }
)






List all users.


  > db.getUsers()





This only lists the users in the current database



If I wanted to see the admin users I need to switch to the admin database


  > use admin
  > db.getUsers()




3. Quit, add auth to config and restart mongodb


  > quit()



Edit config


  > sudo vi /etc/mongod.conf


In the security section, add this.


security:
   authorization: enabled






Restart mongo


  > sudo systemctl restart mongod.service





Now try to login


  > mongo




I am in but….  let me try to use the admin database and show collections



  > use admin
  > show collections




Unauthorized!


Let me login as the admin user

Use db.auth to login as a user


  > db.auth("myAdmin", "my_password")




Now show collections


  > show collections






Let me switch to the test_database


  > use test_database


And as the super user query it


  > db.names.find()





Now change the age of the lisa user


  > db.names.update({"name":"Lisa"}, {"name":"Lisa", "age":25})






If you use .update you have to enter in the entire document.

Unless you use $set.   Let me use that to change Matt's age top 72


  > db.names.update({"name":"Matt"}, {$set:{"age":72}})





Now let me change to the myTester use who should only have access to this single database


  > db.auth("myTester", "my_password")






I should be able to read


  > db.names.find()





And add records


  > db.names.insert({"name": "Joy", "age": 3})




Now let me get out of mongo



  > quit()




I could log into mongo as a user


  > mongo -u "myTester" -p "my_password" test_database





 
I don't like putting a password in a shell command.  To enter in the password manually put -p at the end


  > mongo -u "myTester" test_database -p






OK I think that is enough initial fiddling with Mongo DB for now J



References

[1]  Pritunl Installation
       Visited 7/2017
[2]  Mongo Kernel and file systems
       Visited 7/2017
[3]  MongoDB: Server has startup warnings ''Access control is not enabled for the database''
       Visited 7/2017
[4]  MongoDB roles
       Visited 7/2017
[5]  Disable Transparent Huge Pages (THP)
       Visited 7/2017
[6]  https://askubuntu.com/questions/597372/how-do-i-modify-sys-kernel-mm-transparent-hugepage-enabled
       Visited 7/2017


No comments:

Post a Comment