As a MongoDB user, at some point in your experience with the database management system, you must have come across the error “failed to set up listener: socketexception: address already in use”.
It indicates that a process is already active on port 27017, which is utilized by mongodb. As you could see from the error, the address or port from which mongodb is attempting to start is already in use by another instance. So, what causes it and how do you fix it?
What causes Failed to Set Up Listener: Socketexception: Address Already in Use Error?
This error occurs when MongoDB shares the same port with another process or instance. It’s very similar to what happens when MySQL shares the same port with programs like Skype.
How to fix it
Method 1: Kill the previous instance or processRun MongoDB instance in another port
If you’re running mongod, the default port is 27017. Both mongod and mongos instances use this port. Leaving aside the error, you may want to modify the default port through which MongoDB is accessible for security reasons.
This is actually a lot less difficult than you might expect. You can alter this port to any available port in your local system or on your server if you are hosting MongoDB without requiring any third-party services.
1st option: Using the command —port:
If you wish to run MongoDB directly from the terminal, this option is beneficial. Normally, we run the mongod service from the terminal for local development.
Open a terminal and type the following command into it:

This command will start the mongod service if you have done the configuration successfully in your system. On your terminal, you’ll see a list of logs. Check the last line, which should look like this:

This indicates that it is listening on port 27017. Our goal is to switch it to a different port. In a terminal, type the following command and press enter:

Mongod will be started on port 27080. Make sure this port is available before using it. To halt the process, press Ctrl + C.
2nd option: Make use of a configuration file
If you don’t want to run mongod as a service, the aforementioned procedure is useful for development. For production deployments, however, you can use a single configuration file to add all of the settings you wish to mongod.
When you install MongoDB, it adds one configuration file to the system by default. The location of this file varies depending on the operating system:
- If you’re using a package manager to install MongoDB on Linux, you’ll find it at /etc/mongod.conf.
- If you installed MongoDB from its official website, it’s /usr/local/etc/mongod.conf on a Mac.
- It’s located in the MongoDB installation directory’s /bin/mongod.cfg on Windows.
You can determine where the default configuration file is located based on your operating system.
The awesome thing is that instead of altering the main system file, we may utilize a new configuration file. Make a folder having the following file names:
- mongod.conf
- mongolog.log
The first .conf file is used to enter configuration settings, while the second.log file is used to capture logs. Put the following code in the mongod.conf file:

This is the most basic configuration file for a MongoDB instance. The port number has been changed to 27021. To use this configuration file, run the following command:

It will launch mongod as a service on port 27021 with the configuration file’s values. The logs can be found in mongolog.log.
That is all there is to it. You must interrupt the process if you want to stop it. You can accomplish so by identifying the process id.
Method 2: Kill the previous instance or process
This is another way of handling the error, although, this method is a tedious one because you’ll have to kill the process each time, you’re trying to run MongoDB.
To remedy this problem, we just need to identify the process that is using this port and terminate it. Please ensure that this process is not critical to you and that it may be stopped before proceeding.
The steps to terminate the process running on MongoDB’s default port are as follows:
Step 1: Determine the PID of the process being used by the port.
Open the Windows command prompt and execute netstat –ano |findstr “27017” in the command prompt.
Find the process with the status LISTENING, and the 2700 is the process’s PID. Please keep in mind that the outcome may vary depending on the setting.
Step 2: Get the name of the process.
Tasklist |findstr “2700” is the command to use. The 2700 represents the PID that you obtained in the previous phase.
Step 3: Terminate the Process
taskkill /im mongod.exe /f is the command to use.
Note that this command may need administrator privileges. If this is the case, you should launch the Command Line as an administrator.
It’s always advisable to run netstat –ano |findstr “27017” after killing the process, just to double-check if the task was carried out successfully.