MongoDB is a noSQL database software which is used to store unstructured data. Just as the name suggests, it is the opposite of SQL databases, which store structured data. MongoDB uses a variety of different technologies to make their database system stand out from the rest, which, I guess, is best not discussed here.
What I’m curious about is an error message many users have reported regarding the mongo command in Linux systems, and it is in fact a recent problem. Specifically, this is the type of error message reported:
$ mongo: command not found
The weird part here is that, it’s not actually a problem, but rather a change which caused this error message to emerge, and that is exactly what we are going to cover in this article.
Why is the mongo command not found?
If you’re facing this error message right after updating mongodb, then the apparent fix to the problem is basically to just change the command you use to enter the mongodb shell. Otherwise, the reasons for not being able to run the commands are quite easy to figure out. That is, the package may have been removed somehow when trying to clean up your system, possibly by you or even automatically. In such cases, it is best to first try installing (or reinstalling) the mongodb package to see if it helps. If nothing works still, your last resort would be to check if the path to the mongodb executable is traced by the PATH variable.
In the following sections, we’ll discuss all the above mentioned fixes more elaborately.
Fix for MongoDB version 6.0 and above
Use the right command
If you’re using mongodb version 6.0, and above, the command you’d need to use for entering the mongodb shell is changed from mongo to mongosh:
And that’s it! Yeah! It’s really that simple (as long as this is actually the problem in your specific scenario). If this does not help, and you’re still unable to enter the mongodb shell, then you’d probably consider checking out the sections that follow.
Fix for MongoDB version 5.0.14 and below
Install MongoDB in your system
The first fix you should try out is to actually install the mongodb package, yet once again (yes, even if you might be sure that you have the package). How this would help is simple – if there are any broken symlinks or missing files for the package, they’d return back to life once you reinstall the package.
The problem here is that installing mongodb is not as straightforward as it is to install other database softwares using a package manager directly. Instead, you first need to add the repository of mongodb in your sources list, and then update your system to include the repository. After that, you’d be able to use the package manager to install the package.
If you’re using Arch Linux, then a package is available under AUR. You may use an AUR package manager like yay to directly install it without any extra hassle. If you have yay installed, simply run the following command in your Arch Linux machine:
$ yay -S mongodb-bin
This command will pull the source and build the package by itself, given that you have basic build tools installed in your machine.
On the other hand, Ubuntu users can make good use of the sources list to add the repository which contains the mongodb package, and install it from there. If you’re in Ubuntu, these are the steps you’d follow to install and setup mongodb:
You first need to import the public key for MongoDB before you can actually install it. The process in words is long, but you don’t need that as you can simply run the following commands in order to get the job done anyway:
$ sudo apt-get install gnupg $ wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
Create a list file for mongodb containing the source link to the mongodb package. You can directly do this by running the following command:
$ echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
Reload your system’s package database so that the newly created list file for mongodb gets traced. Do this by running:
$ sudo apt-get update
Lastly, install the mongodb package in your Ubuntu (it can also be Debian or any other distribution based off of Ubuntu and/or Debian) by running the following command in your terminal:
$ sudo apt-get install -y mongodb-org
You now need to start the mongod daemon so that you can start the mongodb shell. Do this by running the following set of commands in order:
$ sudo systemctl start mongod $ sudo systemctl enable mongod
Once the daemon has started, you can now easily start the mongodb shell without any issues, but you do need to keep in mind the fact that as you’ve installed mongodb after reading this article, you probably have installed mongodb version 6.0 or above. So like you previously did, you can no longer type out mongo in your terminal, and instead will have to use mongosh.
If you’re still not able to run neither the mongo nor the mongosh command, the only thing left for you to do is to fix the path variable, and this should definitely fix your problem (pardon me, as there is nothing left from my knowledge that can fix this issue, other than restarting your system…)
FIx your system’s path variable
Unlike many other applications in Linux, the default installation directory for mongodb is not under “/bin/usr”. This directory is usually included in the system’s global PATH variable. The problem is that over the years, MongoDB has constantly been changing the installation directory, and it is hard to figure out where it is installed. What you can do instead, is use the find command in Linux to first figure out the directory where mongodb has been installed:
$ find / -name "mongodb"
This command will search for your entire filesystem and give you an output for the installation folder. Once you get the output, take note of it, as we’ll have to use it in the next command we’re going to use.
We’ll now have to use the export command to append the path we just found under the PATH variable. If you’re using the Bourne Again Shell, then there should be a file called .bashrc in your system’s home directory (hidden with a ‘.’ in front, e.g. ‘.bashrc’). Enter the file using the text editor of your choice, and append the following line:
$ export PATH=$PATH:<path_you_just_found>
Make sure to replace <path_you_just_found> with the actual path you took note of previously. Now, save and exit the file, and restart your terminal for changes to take effect.
At last, try starting the mongodb shell either by trying out the mongo command or the mongosh command. If you’ve properly been able to run the command and enter the shell, congratulations! Hurting my fingers to type all these was indeed a success.