What is MongoDB Default Port and How to Change it?
Database servers mostly use sockets to connect with client systems like using a consistent TCP/IP protocol that helps them to listen and respond. Just like many other databases, MongoDB’s default protocol for the transport layer is the TCP. As all the databases listen to a specific port, MongoDB also does that. The default port for MongoDB connection is “27017”. There is a leeway to come across situations where MongoDB does not work well or get connected. The reason could be anything like the default port is already occupied and we need to change the default port for MongoDB at such a moment. The process of changing a default port for MongoDB consists of some steps needed to take very safely. So, we are going to discuss those simple yet important steps to change a MongoDB port in this guide.
To change a MongoDB port, we need to get and mount the MongoDB community server on our system. Search for the “Mongodb” keyword in your search engine and open its official webpage. Move within the “Products” section and after scrolling down a little, you will get the option of downloading the latest version of MongoDB for any operating system. As we are installing it on Windows, we will be selecting the “Platform” as “Windows”. The package format would be “msi” to manually install it using the setup wizard after tapping the “Download”.
Within a few seconds, the MongoDB “msi” file download will be completed. The file can be double-clicked to launch the MongoDB installation process.
The first step in the installation of MongoDb, through a setup wizard, shows the version to be installed on our Windows system. Also, it shows the instructions to continue the installation process or exit the setup. Tap “Next” to move to the next step and tap “Cancel” to exit.
Here comes the most necessary step in the installation process which is license agreement. Hit the check box “I accept the terms in the License agreement” tracked by the “Next” button.
After completing the license agreement step, you need to choose the setup type you want for your system. If you want to install all MongoDb features, you have to choose “Complete” and if you want to customize your setup, choose “Custom”. Then, hit the “Next” button.
Now, you need to specify the “optional” settings available for your MongoDB server i.e. Choose “Install MongoDB as a Service”. Name your service folder and press the “Next” button.
To use the checkbox listed below on the setup screen, the setup process will enable you to download the MongoDB GUI interface, also known as Compass.
Now, the setup wizard is done with its configurations and provides you the “Install” button to mount the MongoDB on your end.
After the installation has been started, you need to wait for some moments until it gets completely mounted.
After the successful installation of MongoDB on Windows, the MongoDB GUI Interface named MongoDB Compass will be automatically launched on your screen as shown in the image. If it is not already launched, you can open it by searching “MongoDB Compass” on the search bar on the right side of your desktop. Now, you can create a new connection by tapping the “New Connection” button under the green “Compass” section on the right side of the window. For this new connection, MongoDB compass will provide you a built-in URI referred to as the combination of localhost and the port number “27017”. If you want to continue with the same port number, you can do so by tapping the “Save & Connect” or “Connect” button.
Change MongoDB Port Using Command Prompt
To modify settings for MongoDB, windows help us through it using the Command Prompt built-in tool. Search “cmd” in the “search bar” of your desktop and open it as “Administrator” using the “Run as Administrator” option available. The command prompt will be launched within a few seconds in administrative mode. Now, we will be checking for the default port of “MongoDB” in the command prompt that just launched. For this, we are going to run the “mongod” instruction according to the installed “MongoDB” we have in our system.
After running this simple instruction, you will get many lines of output in return showing some information regarding MongoDB, its connections, and services. The output image attached below contains the port number of MongoDB listed as well as a field “port” after the “pid” field if you focus on the last 3rd line. You can see that the currently occupied port of MongoDB is “27017”. Now, we need to change it. So, close the presently opened Cmd.
Now, open a new command prompt with the “Run as Administrator” once again. It will be opened instantly. Now, to change the MongoDB port number through the command prompt, we are going to utilize the same “mongod” keyword instruction. But this time, it will not be single-word command. We will be using the flag “—port” within the instruction to mention the port number that needs to be used by MongoDB instead of port “27017”.Assume, you want to use the port number “4000”. So, along with the “—port” flag, we have mentioned the port number “4000” and executed this query. Now, take a guise at the last line of the output. You can see that the default port for MongoDB has been successfully changed to “4000”. Make sure to pick the port number higher to “1000” as most of the ports less than “1000” tend to be occupied.
This guide’s introduction explains the usage of protocols in database systems and the need for changing the ports. Going through this article, you will be able to change the MongoDB port using windows’ command prompt tool in case your MongoDB does not work well. Although it is not recommended to always change the port number of MongoDB, it is not an insecure method as well. In case you have changed the port number of MongoDB, make sure to change it back to its original port to avoid inconvenience in the future.