In this tutorial, our primary goal is to learn how to start and stop the MongoDB service.
By the end of this tutorial, you will be able to:
- Start the MongoDB service
- Stop the MongoDB service
To follow along with this tutorial, you should have:
- MongoDB installed on your machine
- Basic knowledge of command line interface
Before you start interacting with your MongoDB database, you need to start the MongoDB service. Similarly, when you're done working with the database, you might want to stop the service.
To start MongoDB, you will need to open your terminal and enter the following command:
sudo service mongod start
To stop MongoDB, you can use the following command in your terminal:
sudo service mongod stop
# This command starts the MongoDB service
sudo service mongod start
After running this command, if everything goes well, MongoDB will start running and you should see an output similar to:
Starting mongod (via systemctl): [ OK ]
# This command stops the MongoDB service
sudo service mongod stop
After running this command, MongoDB will stop running and you should see an output similar to:
Stopping mongod (via systemctl): [ OK ]
In this tutorial, we learned how to start and stop the MongoDB service. These are fundamental operations that you need to perform when you want to start working with MongoDB and when you're done.
Now that you've learned how to start and stop the MongoDB service, try these exercises to reinforce your knowledge:
restart
command similar to the start
and stop
commands)Solutions:
sudo service mongod start
, then connect with mongo
. If the service started successfully, you should be able to run MongoDB shell commands.sudo service mongod stop
, then try to connect with mongo
. You should see an error because the service is not running.sudo service mongod restart
. This command will stop the service if it's running, then start it again.Remember, the key to learning is practice! Keep trying these commands until you're comfortable with them.