The following article shows the setup of MongoDB on Debian with PHP support and additional monitoring of MongoDB’s operations, connections and memory usage with Munin. We start by importing the MongoDB public key, adding the MongoDB repository and reloading our repositories:
apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10 echo 'deb http://downloads-distro.mongodb.org/repo/debian-sysvinit dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list apt-get update
Now it’s possible to install the MongoDB package:
apt-get install mongodb-10gen
To prevent MongoDB from upgrading the package automatically we may pin it to the version we just installed:
echo "mongodb-10gen hold" | sudo dpkg --set-selections
Thats it already. MongoDB should be already up and running, otherwise just start it manually:
If you have any trouble with the installation you might find the official tutorial helpful.
Now let’s get to the PHP part. In order to be able to install the MongoDB PHP driver via PECL we first need to install two packages, php-pear and php5-dev:
apt-get install php-pear apt-get install php5-dev
After that, installation of the PHP driver is easy:
pecl install mongo
Next we need to add the driver to our php.ini to enable it. If you also intend to use the driver via command Line and/or CGI you might need to add it to multiple php.ini files. These paths are most likely the following, but your path(s) may vary:
nano /etc/php5/apache2/php.ini nano /etc/php5/cli/php.ini nano /etc/php5/cgi/php.ini
Add the following line:
In order to enable the driver for Apache a server restart is necessary:
Done. Now we are able to use the MongoDB PHP driver. For instructions on how to use it please see the API documentation.
Finally we may also add the Munin-Plugin for MongoDB for monitoring different aspects of the database performance. In case you have not yet installed Munin itself you can find many tutorials on the net (or just use this one). Once Munin is up and running we may continue with the MongoDB-Plugin for Munin by installing the mandatory Python package (in case it has not been already installed previously):
apt-get install python
Next lets get the plugin files from Github, extract them, copy them to Munin’s plugin folder and activate them by creating symlinks:
wget https://github.com/erh/mongo-munin/archive/master.zip unzip mongo-munin-master.zip cp mongo-munin-master/mongo_* /usr/share/munin/plugins/ ln -s /usr/share/munin/plugins/mongo_* /etc/munin/plugins/
Finalize integration of MongoDB stats to Munin by forcing an update and restarting munin-node:
sudo -u munin /usr/share/munin/munin-update /etc/init.d/munin-node restart
If everything was setup correctly we can now see four new graphs in our Munin web interface (current connections, memory usage, operations and write lock percentage).