How to stop MongoDB in a single command?


In order to stop MongoDB in a single command, use the following syntax −

mongo --eval "db.getSiblingDB('admin').shutdownServer()"

Let us implement the above syntax in order to stop MongoDB in one command.

First, use the shortcut key −

Ctrl + C;

The query is as follows −

C:\Program Files\MongoDB\Server\4.0\bin>mongo --eval "db.getSiblingDB('admin').shutdownServer()"

The following is the output − displaying the shutdown of the MongoDB server −

MongoDB shell version v4.0.5
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c0337c02-7ee2-45d9-9349-b22d6b1ffe85") }
MongoDB server version: 4.0.5
server should be down...
2019-03-14T21:56:10.327+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed
2019-03-14T21:56:11.331+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
2019-03-14T21:56:11.333+0530 I QUERY [js] Failed to end session { id: UUID("c0337c02-7ee2-45d9-9349-b22d6b1ffe85") } due to SocketException: 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.]

To begin MongoDB, try this −

C:\Program Files\MongoDB\Server\4.0\bin>mongod

The following is the output −

Let us open the command prompt and use the command mongo −

C:\Program Files\MongoDB\Server\4.0\bin>mongo

The following is the output −

Updated on: 30-Jul-2019

443 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements