MongoDB

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.

Install MongoDB

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.

C:\WINDOWS\system32>mongod

{"t":{"$date":"2022-12-28T21:27:37.313+05:00"},"s":"I""c":"CONTROL""id":23285,   "ctx":"-","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}

{"t":{"$date":"2022-12-28T21:27:37.315+05:00"},"s":"I""c":"NETWORK""id":4915701, "ctx":"-","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":17},"outgoing":{"minWireVersion":6,"maxWireVersion":17},"isInternalClient":true}}}

{"t":{"$date":"2022-12-28T21:27:39.581+05:00"},"s":"I""c":"NETWORK""id":4648602, "ctx":"thread1","msg":"Implicit TCP FastOpen in use."}

{"t":{"$date":"2022-12-28T21:27:39.602+05:00"},"s":"I""c":"REPL",     "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","namespace":"config.tenantMigrationDonors"}}

{"t":{"$date":"2022-12-28T21:27:39.603+05:00"},"s":"I""c":"REPL",     "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","namespace":"config.tenantMigrationRecipients"}}

{"t":{"$date":"2022-12-28T21:27:39.603+05:00"},"s":"I""c":"REPL",     "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"ShardSplitDonorService","namespace":"config.tenantSplitDonors"}}

{"t":{"$date":"2022-12-28T21:27:39.603+05:00"},"s":"I""c":"CONTROL""id":5945603, "ctx":"thread1","msg":"Multi threading initialized"}

{"t":{"$date":"2022-12-28T21:27:39.605+05:00"},"s":"I""c":"CONTROL""id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":17548,"port":27017,"dbPath":"C:/data/db/","architecture":"64-bit","host":"DESKTOP-UGKTJIL"}}

{"t":{"$date":"2022-12-28T21:27:39.605+05:00"},"s":"I""c":"CONTROL""id":23398,   "ctx":"initandlisten","msg":"Target operating system minimum version","attr":{"targetMinOS":"Windows 7/Windows Server 2008 R2"}}

{"t":{"$date":"2022-12-28T21:27:39.606+05:00"},"s":"I""c":"CONTROL""id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"6.0.3","gitVersion":"f803681c3ae19817d31958965850193de067c516","modules":[],"allocator":"tcmalloc","environment":{"distmod":"windows","distarch":"x86_64","target_arch":"x86_64"}}}}

{"t":{"$date":"2022-12-28T21:27:39.606+05:00"},"s":"I""c":"CONTROL""id":51765,   "ctx":"initandlisten","msg":"Operating System","attr":{"os":{"name":"Microsoft Windows 10","version":"10.0 (build 22000)"}}}

{"t":{"$date":"2022-12-28T21:27:39.607+05:00"},"s":"I""c":"CONTROL""id":21951,   "ctx":"initandlisten","msg":"Options set by command line","attr":{"options":{}}}

{"t":{"$date":"2022-12-28T21:27:39.621+05:00"},"s":"E""c":"CONTROL""id":20557,   "ctx":"initandlisten","msg":"DBException in initAndListen, terminating","attr":{"error":"NonExistentPath: Data directory C:\\data\\db\\ not found. Create the missing directory or specify another path using (1) the --dbpath command line option, or (2) by adding the 'storage.dbPath' option in the configuration file."}}

{"t":{"$date":"2022-12-28T21:27:39.622+05:00"},"s":"I""c":"REPL",     "id":4784900, "ctx":"initandlisten","msg":"Stepping down the ReplicationCoordinator for shutdown","attr":{"waitTimeMillis":15000}}

{"t":{"$date":"2022-12-28T21:27:39.626+05:00"},"s":"I""c":"REPL",     "id":4794602, "ctx":"initandlisten","msg":"Attempting to enter quiesce mode"}

{"t":{"$date":"2022-12-28T21:27:39.626+05:00"},"s":"I""c":"-",        "id":6371601, "ctx":"initandlisten","msg":"Shutting down the FLE Crud thread pool"}

{"t":{"$date":"2022-12-28T21:27:39.627+05:00"},"s":"I""c":"COMMAND""id":4784901, "ctx":"initandlisten","msg":"Shutting down the MirrorMaestro"}

{"t":{"$date":"2022-12-28T21:27:39.628+05:00"},"s":"I""c":"SHARDING", "id":4784902, "ctx":"initandlisten","msg":"Shutting down the WaitForMajorityService"}

{"t":{"$date":"2022-12-28T21:27:39.628+05:00"},"s":"I""c":"NETWORK""id":20562,   "ctx":"initandlisten","msg":"Shutdown: going to close listening sockets"}

{"t":{"$date":"2022-12-28T21:27:39.629+05:00"},"s":"I""c":"NETWORK""id":4784905, "ctx":"initandlisten","msg":"Shutting down the global connection pool"}

{"t":{"$date":"2022-12-28T21:27:39.633+05:00"},"s":"I""c":"CONTROL""id":4784906, "ctx":"initandlisten","msg":"Shutting down the FlowControlTicketholder"}

{"t":{"$date":"2022-12-28T21:27:39.635+05:00"},"s":"I""c":"-",        "id":20520,   "ctx":"initandlisten","msg":"Stopping further Flow Control ticket acquisitions."}

{"t":{"$date":"2022-12-28T21:27:39.635+05:00"},"s":"I""c":"NETWORK""id":4784918, "ctx":"initandlisten","msg":"Shutting down the ReplicaSetMonitor"}

{"t":{"$date":"2022-12-28T21:27:39.635+05:00"},"s":"I""c":"SHARDING", "id":4784921, "ctx":"initandlisten","msg":"Shutting down the MigrationUtilExecutor"}

{"t":{"$date":"2022-12-28T21:27:39.636+05:00"},"s":"I""c":"ASIO",     "id":22582,   "ctx":"MigrationUtil-TaskExecutor","msg":"Killing all outstanding egress activity."}

{"t":{"$date":"2022-12-28T21:27:39.637+05:00"},"s":"I""c":"COMMAND""id":4784923, "ctx":"initandlisten","msg":"Shutting down the ServiceEntryPoint"}

{"t":{"$date":"2022-12-28T21:27:39.637+05:00"},"s":"I""c":"CONTROL""id":4784925, "ctx":"initandlisten","msg":"Shutting down free monitoring"}

{"t":{"$date":"2022-12-28T21:27:39.638+05:00"},"s":"I""c":"CONTROL""id":4784927, "ctx":"initandlisten","msg":"Shutting down the HealthLog"}

{"t":{"$date":"2022-12-28T21:27:39.639+05:00"},"s":"I""c":"CONTROL""id":4784928, "ctx":"initandlisten","msg":"Shutting down the TTL monitor"}

{"t":{"$date":"2022-12-28T21:27:39.639+05:00"},"s":"I""c":"CONTROL""id":6278511, "ctx":"initandlisten","msg":"Shutting down the Change Stream Expired Pre-images Remover"}

{"t":{"$date":"2022-12-28T21:27:39.639+05:00"},"s":"I""c":"CONTROL""id":4784929, "ctx":"initandlisten","msg":"Acquiring the global lock for shutdown"}

{"t":{"$date":"2022-12-28T21:27:39.640+05:00"},"s":"I""c":"-",        "id":4784931, "ctx":"initandlisten","msg":"Dropping the scope cache for shutdown"}

{"t":{"$date":"2022-12-28T21:27:39.641+05:00"},"s":"I""c":"CONTROL""id":20565,   "ctx":"initandlisten","msg":"Now exiting"}

{"t":{"$date":"2022-12-28T21:27:39.644+05:00"},"s":"I""c":"CONTROL""id":23138,   "ctx":"initandlisten","msg":"Shutting down","attr":{"exitCode":100}}

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.

C:\WINDOWS\system32>mongod --port 4000

{"t":{"$date":"2022-12-28T21:29:40.268+05:00"},"s":"I""c":"CONTROL""id":23285,   "ctx":"thread1","msg":"Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'"}

{"t":{"$date":"2022-12-28T21:29:40.269+05:00"},"s":"I""c":"NETWORK""id":4915701, "ctx":"thread1","msg":"Initialized wire specification","attr":{"spec":{"incomingExternalClient":{"minWireVersion":0,"maxWireVersion":17},"incomingInternalClient":{"minWireVersion":0,"maxWireVersion":17},"outgoing":{"minWireVersion":6,"maxWireVersion":17},"isInternalClient":true}}}

{"t":{"$date":"2022-12-28T21:29:42.238+05:00"},"s":"I""c":"NETWORK""id":4648602, "ctx":"thread1","msg":"Implicit TCP FastOpen in use."}

{"t":{"$date":"2022-12-28T21:29:42.241+05:00"},"s":"I""c":"REPL",     "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationDonorService","namespace":"config.tenantMigrationDonors"}}

{"t":{"$date":"2022-12-28T21:29:42.241+05:00"},"s":"I""c":"REPL",     "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"TenantMigrationRecipientService","namespace":"config.tenantMigrationRecipients"}}

{"t":{"$date":"2022-12-28T21:29:42.242+05:00"},"s":"I""c":"REPL",     "id":5123008, "ctx":"thread1","msg":"Successfully registered PrimaryOnlyService","attr":{"service":"ShardSplitDonorService","namespace":"config.tenantSplitDonors"}}

{"t":{"$date":"2022-12-28T21:29:42.246+05:00"},"s":"I""c":"CONTROL""id":5945603, "ctx":"thread1","msg":"Multi threading initialized"}

{"t":{"$date":"2022-12-28T21:29:42.248+05:00"},"s":"I""c":"CONTROL""id":4615611, "ctx":"initandlisten","msg":"MongoDB starting","attr":{"pid":8328,"port":4000,"dbPath":"C:/data/db/","architecture":"64-bit","host":"DESKTOP-UGKTJIL"}}

{"t":{"$date":"2022-12-28T21:29:42.248+05:00"},"s":"I""c":"CONTROL""id":23398,   "ctx":"initandlisten","msg":"Target operating system minimum version","attr":{"targetMinOS":"Windows 7/Windows Server 2008 R2"}}

{"t":{"$date":"2022-12-28T21:29:42.249+05:00"},"s":"I""c":"CONTROL""id":23403,   "ctx":"initandlisten","msg":"Build Info","attr":{"buildInfo":{"version":"6.0.3","gitVersion":"f803681c3ae19817d31958965850193de067c516","modules":[],"allocator":"tcmalloc","environment":{"distmod":"windows","distarch":"x86_64","target_arch":"x86_64"}}}}

Conclusion

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.

About the author

Saeed Raza

Hello geeks! I am here to guide you about your tech-related issues. My expertise revolves around Linux, Databases & Programming. Additionally, I am practicing law in Pakistan. Cheers to all of you.