MongoDB Shutdown Server – Easy Guide

MongoDB is a well-known document-oriented database management system that enables developers to store massive quantities of data. It is free and open-source, as well as highly efficient. A MongoDB database has a number of operators from which you may select the one that best meets your requirements. This database management system service allows you to develop applications capable of far more than CRUD activities because of its extremely strong, easy-to-use, and flexible operators.

To implement MongoDB it is necessary to keep its server running continuously which can be done easily but sometimes we want to stop or restart that server due to some reason like system upgrade, error or maintenance. But how to do this?

In this guide, we will learn to perform the shutdown of the local MongoDB database server. We will discuss two methods to perform a shutdown of the MongoDB server so that you can choose accordingly. So, let us get started.

MongoDB Shutdown Server

We have two ways to perform the shutdown of the local MongoDB database server.

  1. Using shutdown command
  2. Using shutdownServer() Method

Shutdown Command

The shutdown command is a property on the adminCommand() function. It comes with other optional properties namely force, which accepts a Boolean value, timeoutSecs which must be an integer, and a comment.

Syntax:

db.adminCommand({
  shutdown: 1,
  force: boolean
  timeoutSecs: int,
  comment: any
})

shutdownServer() Function

The shutdownServer() function is a database function that comes with similar properties on its object value – force and timeoutSecs.

Syntax:

db.shutdownServer({
  force: boolean,
  timeoutSecs: int
})

Let us look at examples using each of these.

Performing MongoDB Shutdown of Local Database Server

Let’s now see the process of using both ways to perform the shutdown.

Using the shutdown Command

  • Run the MongoDB server
  • Choose a database you want to work in and navigate into it:
show dbs

use shutServerSample
  • Now, we will use the shutdown command with only the shutdown property:
> db.adminCommand({shutdown: 1})

So, this is the immediate output we get. The server shut down abruptly. You must have noticed the server just shut down automatically:

uncaught exception: Error: error doing query: failed: network error while attempting to run command 'shutdown' on host '127.0.0.1:27017'  :
DB.prototype.runCommand@src/mongo/shell/db.js:169:19
DB.prototype.adminCommand@src/mongo/shell/db.js:187:12
@(shell):1:1
  • Let us try navigating into a database and working in it:
> use movie-app
switched to db movie-app
> show tables

As soon as I try to print the list of collections I have, I get a failed MongoDB server connection error:

Error: socket exception [CONNECT_ERROR] server [couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it.] :
runClientFunctionWithRetries@src/mongo/shell/session.js:361:27
runCommand@src/mongo/shell/session.js:455:25
DB.prototype._runCommandImpl@src/mongo/shell/db.js:147:12
DB.prototype.runCommand@src/mongo/shell/db.js:162:16
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:806:17
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:856:16
shellHelper.show@src/mongo/shell/utils.js:924:9
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
  • Let us now use the timeoutSecs properties, and even pass a comment:
> db.adminCommand({ "shutdown" : 1, timeoutSecs: 60, "comment": "Shutting server"})
uncaught exception: Error: error doing query: failed: network error while attempting to run command 'shutdown' on host '127.0.0.1:27017'  :
DB.prototype.runCommand@src/mongo/shell/db.js:169:19
DB.prototype.adminCommand@src/mongo/shell/db.js:187:12
@(shell):1:1
  • Checking the server:
> use movie-app
switched to db movie-app
> show tables
Error: socket exception [CONNECT_ERROR] server [couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it.] :
runClientFunctionWithRetries@src/mongo/shell/session.js:361:27
runCommand@src/mongo/shell/session.js:455:25
DB.prototype._runCommandImpl@src/mongo/shell/db.js:147:12
DB.prototype.runCommand@src/mongo/shell/db.js:162:16
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:806:17
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:856:16
shellHelper.show@src/mongo/shell/utils.js:924:9
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
  • Using force property as true:
> db.adminCommand({ "shutdown" : 1, "force" : true })
uncaught exception: Error: error doing query: failed: network error while attempting to run command 'shutdown' on host '127.0.0.1:27017'  :
DB.prototype.runCommand@src/mongo/shell/db.js:169:19
DB.prototype.adminCommand@src/mongo/shell/db.js:187:12
@(shell):1:1

Using the shutdownServer Function

Let us now use the shutdownServer() function to perform the shutdown:

> db.getSiblingDB("admin").shutdownServer({ "force" : true })

server should be down...

This is the message we get.

  • Let us now check using the Mongo Shell:
> use movie-app

switched to db movie-app

> show tables

Error: socket exception [CONNECT_ERROR] server [couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it.] :
runClientFunctionWithRetries@src/mongo/shell/session.js:361:27
runCommand@src/mongo/shell/session.js:455:25
DB.prototype._runCommandImpl@src/mongo/shell/db.js:147:12
DB.prototype.runCommand@src/mongo/shell/db.js:162:16
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:806:17
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:856:16
shellHelper.show@src/mongo/shell/utils.js:924:9
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1
  • Setting a longer timeout:
> db.getSiblingDB("admin").shutdownServer({ "timeoutSecs": 60 })

server should be down...

> use movie-app

switched to db movie-app

> show tables

Error: socket exception [CONNECT_ERROR] server [couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: No connection could be made because the target machine actively refused it.] :
runClientFunctionWithRetries@src/mongo/shell/session.js:361:27
runCommand@src/mongo/shell/session.js:455:25
DB.prototype._runCommandImpl@src/mongo/shell/db.js:147:12
DB.prototype.runCommand@src/mongo/shell/db.js:162:16
DB.prototype._getCollectionInfosCommand@src/mongo/shell/db.js:806:17
DB.prototype.getCollectionInfos@src/mongo/shell/db.js:856:16
shellHelper.show@src/mongo/shell/utils.js:924:9
shellHelper@src/mongo/shell/utils.js:819:15
@(shellhelp2):1:1

Read More: How to Connect MongoDB on Localhost 27017 in Windows – Starter’s Guide

Conclusion

In this tutorial, we have learned to shut down MongoDB’s local database server using the shutdown command and the shutdownServer() method. Both methods can be used according to convenience. We hope this tutorial is helpful to you.

References

https://docs.mongodb.com/manual/reference/method/db.shutdownServer/#mongodb-method-db.shutdownServer

https://docs.mongodb.com/manual/reference/command/shutdown/

Aneesha S
Aneesha S
Articles: 172