MongoDB Shutdown Server – Easy Guide

MongoDB Shutdown Featured Image

In this guide, we will learn to perform the MongoDB shutdown of the local database server.

The 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.

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. Although it might be difficult for novices to understand without an ODM, it has detailed documentation. It is a NoSQL database, which means that it stores data in BSON type as documents within collections.

Applications profit from the enormous power of MongoDB, which has been widely used for quite some time. MongoDB offers a number of operators in addition to the ability to access, print, and change data. Because the system is so simple to use, anyone, regardless of expertise level, can utilize it.

We will be discussing two methods to perform a MongoDB shutdown of the server. I will show you quick ways to do the same. So, let us get started.

MongoDB Shutdown Server Syntax

Let us take a quick look at the MongoDB shutdown server syntaxes for both the methods we are going to discuss in this guide.

The shutdown command syntax:

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

The shutdownServer() syntax:

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

Understanding the MongoDB Shutdown Functions & Commands

So, let us understand a bit about what each of the above code snippets do.

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.

shutdownServer() Function

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

Let us look at examples using each of these.

Performing MongoDB Shutdown of Local Database Server

Let us start by using the above commands to perform a MongoDB shutdown. We will make use of different examples to demonstrate how most of the options work.

Let us get started.

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 MongoDB 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 a MongoDB 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

Learn to perform the MongoDB shutdown operation of a local database server.

Noteworthy References

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

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