Ubiquiti Unifi Controller MongoDB Issues

After upgrading Unifi on a Debian server running a Ubiquiti Unifi controller, I ran into an issue where the web interface would not load. After poking through the logs I found that the issue appeared to be with MongoDB.

In the server log file located at /var/log/unifi/server.log for my install I found the following messages:

[2018-04-01 18:58:40,167] <db-server> INFO  db     - DbServer stopped
[2018-04-01 18:58:44,188] <db-server> ERROR system - [exec] error, rc=2

Database Repair

The first thing I suspected was corruption for the MongoDB data - the server had previously did not have a clean shutdown (the filesystem became read only). To repair the MongoDB database for Unifi you can execute MongoDB like this:

mongod --dbpath /usr/lib/unifi/data/db --port 27117 --repair

After the repair, the permissions on the MongoDB database files should be updated to make sure they are owned by the Unifi user:

chown -R unifi. /usr/lib/unifi/data/db

After the repair, MongoDB was still failing to start.

Debugging MongoDB startup

For my install of Unifi, when MongoDB starts up it executes MongoDB like this:

/usr/lib/unifi/bin/mongod --dbpath /usr/lib/unifi/data/db --port 27117 --unixSocketPrefix /usr/lib/unifi/run --logappend --logpath /usr/lib/unifi/logs/mongod.log --nohttpinterface --bind_ip 127.0.0.1

/usr/lib/unifi/bin/mongod is a symlink to /usr/bin/mongod.

To get the startup options I replaced the /usr/lib/unifi/bin/mongod file with the following script:

#!/bin/bash
echo $* > /tmp/mongod_startup
exec /usr/bin/mongod $*

After replacing the file make sure it is executable with chmod +x /usr/lib/unifi/bin/mongod. Make sure the controller is running, it should then execute the script and you can review the startup options in the file /tmp/mongod_startup.

The problem

Once I had the startup command for MongoDB I attempted to run it to see what was happening. It turns out the --nohttpinterface flag was causing the problem - MongoDB was throwing an error like this:

Error parsing command line: unrecognised option '--nohttpinterface'

The fix

To fix it I replaced the MongoDB symlink that the controller executes so that the option that does not work is removed. Since the default file is a symlink its safe to remove it (to restore you can just create the symlink again):

rm -f /usr/lib/unifi/bin/mongod

You can then create the file /usr/lib/unifi/bin/mongod with the following content:

#!/bin/bash
exec /usr/bin/mongod ${*//--nohttpinterface/}

After creating the file, make sure it is executable (chmod +x /usr/lib/unifi/bin/mongod). Start the controller service again if it is not running already and it should now be working.