{"id":7463,"date":"2023-10-30T09:07:44","date_gmt":"2023-10-30T03:37:44","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=7463"},"modified":"2026-09-01T16:03:30","modified_gmt":"2026-09-01T10:33:30","slug":"how-to-auto-restart-mongodb","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/how-to-auto-restart-mongodb\/","title":{"rendered":"How to Set Up MongoDB to Auto-Restart Using Systemd"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To set up MongoDB to automatically restart when the service stops or fails, you can use <strong>systemd<\/strong> service configuration. Systemd allows you to define restart behavior for the MongoDB service and ensure that MongoDB starts automatically after a system reboot.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This guide explains how to configure MongoDB to auto-restart on <strong>Ubuntu or CentOS<\/strong> using Systemd.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Server root login credentials.<\/li>\n\n\n\n<li>SSH access to the Ubuntu or CentOS server.<\/li>\n\n\n\n<li>MongoDB installed and configured as a systemd service.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Log in to the Server<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Log in to your Ubuntu or CentOS server as a user with sudo privileges. You can use SSH or directly access the server.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ ssh root@Ip<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Create or Edit the MongoDB Systemd Service<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Create a Systemd service unit file and edit your MongoDB service.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ vi \/etc\/systemd\/system\/mongod.service<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following line under the <code>[Service]<\/code> section in <code>mongod.service<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Restart=always<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>Restart=always<\/code> option instructs Systemd to automatically restart the MongoDB service whenever it stops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Reload the Systemd Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After modifying the service file, reload the systemd configuration so that the changes are recognized.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo systemctl daemon-reload<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Enable the MongoDB Service<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Enable the MongoDB service so that it starts automatically when the server boots.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ systemctl enable mongod.service<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Start the MongoDB Service<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Start the MongoDB service using the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo systemctl start mongod<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 6: Verify the MongoDB Service<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Check the status of the MongoDB service to verify that it is running correctly.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">$ sudo systemctl status mongod<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the service is running successfully, the status should indicate that <strong>mongod.service<\/strong> is active.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Following the steps mentioned above, you can configure MongoDB to automatically restart using Systemd. The <code>Restart=always<\/code> option helps keep the MongoDB service running when it unexpectedly stops, while enabling the service ensures that MongoDB starts automatically after a system reboot.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Why does MongoDB need an auto-restart configuration?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">An auto-restart configuration helps MongoDB recover automatically if the service unexpectedly stops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. What does <code>Restart=always<\/code> do?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><code>Restart=always<\/code> instructs Systemd to restart the MongoDB service whenever it stops.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">3. How can I check whether MongoDB is running?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl status mongod<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. How can I make MongoDB start after a server reboot?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Enable the MongoDB service using:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">systemctl enable mongod.service<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">5. Does this configuration work on Ubuntu and CentOS?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. Both Ubuntu and CentOS use Systemd on modern versions, so the <code>systemctl<\/code> commands can be used to manage the MongoDB service.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Articles<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">How to Clear the Data from MongoDB Collection<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Learn how to clear data from a MongoDB collection when you need to remove existing records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-clear-the-data-from-mongo-db-collection\/?utm_source=chatgpt.com\">Read the MongoDB collection data guide<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to Connect to an Authenticated MongoDB Database Using MongoDB Compass<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Learn how to connect to an authenticated MongoDB database through MongoDB Compass using an SSH connection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a target=\"_blank\" rel=\"noreferrer noopener\" href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-connect-to-the-authenticated-mongodb-database-via-mongodb-compass-using-ssh\/?utm_source=chatgpt.com\">Read the MongoDB Compass connection guide<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Talk to our experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Looking for the right technology solution for your business? Our team of experts can help you with development, cloud, DevOps, design, and a wide range of other technology needs. Get in touch with our team&nbsp;<a href=\"https:\/\/pheonixsolutions.com\/contact\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction To set up MongoDB to automatically restart when the service stops or fails, you can use systemd service configuration. [&hellip;]<\/p>\n","protected":false},"author":499,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1020],"tags":[],"class_list":["post-7463","post","type-post","status-publish","format-standard","hentry","category-containers-kubernetes","psol-cat-containers-kubernetes"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-1Wn","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7463","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/499"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=7463"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7463\/revisions"}],"predecessor-version":[{"id":11288,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/7463\/revisions\/11288"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=7463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=7463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=7463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}