MongoDB shutting down with code 48

In this MongoDB tutorial, I will explain how I managed to resolve the error “MongoDB shutting down with code 48”.

  • MongoDB shutting down with code 48
  • Reason: MongoDB shutting down with code 48
  • Solution: MongoDB shutting down with code 48 in windows
  • MongoDB shutting down with code 48 in mac
  • MongoDB shutting down with code 48 in ubuntu

MongoDB shutting down with code 48

Recently, when I was trying to start the MongoDB server by using the MongoDB shell, I faced the error “MongoDB shutting down with code 48. You can see the error below:

mongo exit with code 48
mongo exit with code 48

I was using MongoDB latest version v5.0.2. I properly followed the correct command, but still faced this error.

Read MongoDB shutting down with code 100

Reason: MongoDB shutting down with code 48

When I searched it on the web, I found many posts and blogs on this error. Most of the blogs stated that this error was triggered because the port for MongoDB is already in use. By default MongoDB services run on port 27017.

Some said that there might be problems during the installation of the MongoDB database. I tried to reinstall the MongoDB database and start the server again but still got this error.

Solution: MongoDB shutting down with code 48 in windows

When I was trying to troubleshoot this error in Windows operating system. I found multiple solutions for this so let’s fix this:

Use the default MongoDB port

First, check the running processes on the MongoDB port because if some other services are using the default MongoDB port that is why it shows error 48.

So follow the below commands and find out the process that occupies the specific port and then kill it.

  • Open the Command Line of the Windows operation system
  • Find the PID of the process because that port using the MongoDB default port
  • The following command will return all running processes that have any relation to the mongod or port 27017
netstat –ano |findstr “27017”
MongoDB shutting down with code 48
MongoDB shutting down with code 48

Here, process status is LISTENING and the 12884 is the PID of the process. The different environments may have different results.

  • To find the process name use the below command
tasklist |findstr “12884”
MongoDB shutting down with code 48 in windows
MongoDB shutting down with code 48

Here, the 12884 is the PID that we found in the previous step. The result (process name) is mongod.exe, and this is in my test environment, you may get a different result.

  • Now, type the below command to kill the process
 taskkill /im mongod.exe /f

After that, the port occupies port 27017 is killed successfully. For the confirmation, you can check again by using the below command:

netstat –ano |findstr “27017”

After the successful termination of process ID, you can run the MongoDB server by using mongod command.

mongod

The MongoDB server starts working again and fixes the error.

Note: All the command needs administrator operation permission. So, please run the Command Line as an administrator.

Read How to create new database in MongoDB

Change the mongod to a custom port

This is another best option to fix the error so change the default port to a custom port. The port conflicts can arise in the server when multiple services work on the same box.

Therefore, to fix the problem change the mongod port using the command,

mongod --port 27018
MongoDB shutting down with code 48 solution
Change the mongod to a custom port

Now, the mongod service will start running in the newly defined port 27018, And, the error does not pop up again.

Read MongoDB backup and restore

MongoDB shutting down with code 48 in mac

If you got the same error “Shutting down with code 48” in Mac OS then you can resolve the error following ways.

In Mac OS, there are also two ways to fix this error:

Use the default MongoDB port

First checks the running process on the MongoDB port because sometimes some other services were using the MongoDB default port.

Follow the below commands and find out the process that occupies the specific port and then kill it to start the server on the default port.

  • The following command will return all running processes that have any relation to the mongod
ps -ef | grep mongod
  • Run the below command to kill the process running on the port
npx kill-port 27017

After the successful termination of the process, start the MongoDB server using the below command as we normally do:

mongod

The MongoDB server starts working again and fixes the error.

Read How does MongoDB create collection

Change the mongod to a custom port

This is another good option to fix the error so change the default port to a custom port.

Therefore, to fix the problem change the mongod port using the below command

mongod --port 27019

After executing this command mongod server started running on custom port 27019. Thus this way also you can start the mongod server and the error doesn’t show up again.

Read MongoDB failed to connect

MongoDB shutting down with code 48 in ubuntu

If you got the same error “Shutting down with code 48” in Ubuntu OS then you can resolve the error following ways.

In Ubuntu OS, there are also two ways to fix this error:

Use the default MongoDB port

First checks the running process on the MongoDB port because sometimes some other services were using the MongoDB default port.

Follow the below commands and find out the process that occupies the specific port and then kill it to start the server on the default port.

  • The 27017 is the default port of MongoDB.
  • The following command will return all other services that are using mongod default port.
nmap -p 27017 127.0.0.1
MongoDB shutting down with code 48 in ubuntu
MongoDB shutting down with code 48 in ubuntu
  • If MongoDB server running on another port then we replace the port with custom one.
  • If some other process found to use the the port 27017 then run the below command to kill the process running on the port
  • First, find the process by name using the below command
pgrep mongo
MongoDB shutting down in ubuntu with code 48
Find process ID of mongo

Here, we got process ID 21478 by using the above command.

  • Then kill the mongod-process
kill <PID> 21478

Here, PID refers to the process ID process running on 27017.

After the successful termination of the process, start the MongoDB server using the below command:

mongod

The MongoDB server starts working again and fixes the error.

Change the mongod to a custom port

This one is also the best option to fix the error, from the default port to a custom port.

Therefore, to fix the problem changes the mongod port using the below command.

mongod --port 27018

After executing this command mongod server started running on custom port 27018. Thus this way also you can start the mongod server and the error doesn’t show up again.

You may like the following MongoDB tutorials:

Conclusion

In this tutorial, we have resolved the error “MongoDB shutting down with code 48“. These are the following topics that we covered in this tutorial

  • MongoDB shutting down with code 48
  • Reason: MongoDB shutting down with code 48
  • Solution: MongoDB shutting down with code 48 in windows
  • MongoDB shutting down with code 48 in mac
  • MongoDB shutting down with code 48 in ubuntu

Leave a Comment