Shutdown MongoDB with auth enabled?

To shutdown MongoDB with authentication enabled, you need to be authenticated as an administrative user and use the shutdownServer() method on the admin database.

Syntax

use admin
db.auth("username", "password")
db.shutdownServer()

Method 1: Shutdown with Authentication

First, connect to the admin database and authenticate with administrative credentials ?

use admin
switched to db admin

Authenticate as an admin user with shutdown privileges ?

db.auth("admin", "password")
1

Now shutdown the MongoDB server ?

db.shutdownServer()
server should be down...
2020-01-07T22:40:31.295+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2020-01-07T22:40:32.326+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed

Method 2: Direct Authentication During Connection

You can also authenticate directly when connecting to MongoDB ?

mongo --authenticationDatabase "admin" -u "admin" -p "password"
use admin
db.shutdownServer()

Key Points

  • Only users with shutdown privileges (like userAdminAnyDatabase or root roles) can shutdown MongoDB.
  • Authentication must be done on the admin database before running shutdownServer().
  • The connection will fail after shutdown as the server stops accepting connections.

Conclusion

When MongoDB has authentication enabled, use db.auth() on the admin database with proper credentials before executing db.shutdownServer(). The server will terminate and refuse new connections after successful shutdown.

Updated on: 2026-03-15T02:27:59+05:30

175 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements