Recently I decided to try using MongoDB within a PHP application, I had heard good things about it and thought it was worth a try, little did I realise the trouble I was in for (as is usually the case with first time installs of new software) though really the problems came when getting the new MongoDB bits to work with my compiled PHP7 installation, but that is for another day.
Right now I want to talk about installing and configuring Mongodb with some user authentication, which is disabled by default. Also a note I did this a couple of months back this isn’t a tutorial as such as this is what I used to get set-up, go read for yourself and learn and be awesome.
I am using a CentOS7 Minimal install as the base system.
As for the actual installation, it is fairly simple and in the docs they even have a small guide on how to do it, rather than shamelessly re-type it here I thought I would just link it: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/#configure-the-package-management-system-yum
So now it is installed, all easy enough, start it up with systemctl and you are good. Then you need to connect and create a user, I like to create a admin user and then a user specifically for my application. To be honest the MongoDB docs again have great pages on creating users and enabling authentication so here are some links:
So I read those and felt I had a fairly good grasp of how things should work but I had no way of connecting to the database to run the db.createUser command, I also didn’t want to use the command line so started searching (I run Antergos on my main system) and I stumbled upon some software called Robomongo. It did everything I wanted, except for support SSL key authentication, not a real problem especially as that was already developed but just hadn’t been released for the free community version yet (it is now by the way) https://robomongo.org
So using Robomongo I was able to connect and set-up the admin user I want and then create my new database and a user for it, then I actually had to stop MongoDB and edit the config file ‘/etc/monogod.conf’ to enable user authentication. It isn’t the most obvious on how to set-up that file so below is a small excerpt of mine:
Ok so that really is simple isn’t it, then restart MongoDB and suddenly you need a password to log-in.
Nice and simple and it looks fairly straightforward to set-up ssl key authentication as well which is something I will be going for in the future.
To anyone that reads this I hope you find it useful