Installing MongoDB Shell (mongosh) to Connect to Your UniFi Network Controller Database Running on Linux

Installing MongoDB Shell (mongosh) to Connect to Your UniFi Network Controller Database Running on Linux

I have been supporting a project that implemented a WiFi hotspot solution using Ubiquiti Access Points and a centralized, self-hosted UniFi Network Controller running on Linux for a while now. The controller has a nice web portal here you can pretty much manage your APs; Adopt them, reconfigure them, monitor connections, or configure a hotspot. It’s been a smooth ride until I ran into some issue that might require accessing the backend database and taking care of things from the CLI. If you want to learn more about installing a self-hosted UniFi controller on Linux, you can read my previous detailed article here.

UniFi controller utilizes the MongoDB and accessing it requires a CLI tool called “mongosh” which will be our focus for this article. Am doing this exercise on Linux Mint version 21.1, which belongs to the distro family of Debian. To get the mongosh working on Linux, I followed the steps below:

Go to MongoDB official website and select “Linux x64” as the platform (select according to the platform you are running if it’s listed in the drop down, otherwise, Linux x64 is more general if your distro flavor is not listed or if you are not sure of the Linux platform that you are running.). Under Package, select “tgz” and click “Download” or “Copy Link”.

You can then upload the file to your Linux server using SFTP or WinSCP. Alternatively, you can click on “Copy link” and use WGET utility on your Linux server to fetch the file. I used the latter:

Note

Before taking this direction of installing mongosh using the “.tgz” file, I tried installing it with “apt-get install mongosh” but it failed with Error: “Unable to locate package mongosh”.

Navigate to the directory where you uploaded the .tgz archive (if you used the method of first downloading to your PC and uploading using SFTP or WinSCP) or where you downloaded the archive (if you used WGET with the URL). I downloaded mine in “/tmp” directory. Use the “tar” command to unpack the archive:

The command below will unpack the “mongosh-2.2.5-linux-x64.tgz” archive:

# tar -zxvf mongosh-2.2.5-linux-x64.tgz

The command below is used to make the “mongosh” executable:

# cd mongosh-2.2.5-linux-x64/
# chmod +x bin/mongosh

There are two ways to go about this; You can copy the files in “mongosh-2.2.5-linux-x64/bin” into any directory in your system environment PATH or you can use the method of creating symbolic links to a directory in your system environment PATH. For this exercise I used the former where I copy the two binary files into my system environment PATH.

Use the commands below to copy the binaries into any of the environment PATH directory:

# echo $PATH
# cd /tmp/mongosh-2.2.5-linux-x64/bin/
# cp mongosh /usr/local/bin/
# cp mongosh_crypt_v1.so /usr/local/lib/

Before you test connecting to your MongoDB using the mongosh, ensure that your MongoDB service is running.

The command below is used to check the status of MongoDB service

# service mongod status

You can now test connection to your MongoDB using “mongosh”

The command below will connect to your MongoDB deployment on your server:

# mongosh --port 27117

Some useful commands to work with UniFi Network Controller MongoDB:

1.) List all databases:
show dbs
2.) Switch to db ace:
use ace
3.) List all APs on the controller:
db.device.find({},{site_id:"",ip:"",name:"",mac:""})
4.)Find AP by MAC address:
db.device.find({"mac":"70:a7:41:db:54:58"})
5.) List all the sites on the controller
db.site.find()
6.) Lists all collections:
show collections;
7.) Prints all users on the controller:
db.admin.find()

Please let me know in the comments of any new commands and tricks that you have used with UniFi MongoDB.

About the Author

Joshua Makuru Nomwesigwa is a seasoned Telecommunications Engineer with vast experience in IP Technologies; he eats, drinks, and dreams IP packets. He is a passionate evangelist of the forth industrial revolution (4IR) a.k.a Industry 4.0 and all the technologies that it brings; 5G, Cloud Computing, BigData, Artificial Intelligence (AI), Machine Learning (ML), Internet of Things (IoT), Quantum Computing, etc. Basically, anything techie because a normal life is boring.

Spread the word: