{"id":253,"date":"2012-07-21T21:34:48","date_gmt":"2012-07-21T13:34:48","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=253"},"modified":"2018-09-05T22:56:34","modified_gmt":"2018-09-05T14:56:34","slug":"installing-mongodb-as-a-windows-service","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/installing-mongodb-as-a-windows-service\/","title":{"rendered":"Installing MongoDB as a windows service"},"content":{"rendered":"<p>After using <a href=\"http:\/\/www.mongodb.org\/\" title=\"Link to MongoDB homepage\" target=\"_blank\">MongoDB<\/a> for quite a while, I realised that I had been repeating the starting up of the MongoDB database daemon whenever I reboot my PC. Manually starting the MongoDB database daemon via my command prompt whenever I want to work on my projects is inefficient.  <\/p>\n<p>Hence making MongoDB run as a windows service automatically when my computer starts up is one way to avoid repeating myself. In this post, I document how I install my MongoDB database as a windows service.<\/p>\n<h3>Getting the MongoDB package<\/h3>\n<ul>\n<li>I chose the MongoDB package meant for Windows 64-bit from <a href=\"http:\/\/www.mongodb.org\/downloads\" title=\"MongoDB's official download page\" target=\"_blank\">MongoDB's official download page<\/a>. <\/li>\n<li>I <a href=\"http:\/\/www.techcoil.com\/blog\/handy-vbscript-functions-for-dealing-with-zip-files-and-folders\/\" title=\"Handy vbscript functions for dealing with zip files and folders.\" target=\"_blank\">unzipped<\/a> the package to my <code>D:<\/code> drive. By doing so, I had the folder: <code>D:\\mongodb-<em>&lt;version-identifier&gt;<\/em><\/code>. <\/li>\n<li>I renamed the folder as <code>mongodb<\/code> to keep the folder naming simpler.<\/li>\n<\/ul>\n<h3>Configuring the database<\/h3>\n<p>MongoDB could be installed with default configurations. However, some of the default configurations are not to my liking. I would prefer my MongoDB database server to listen on port 31234 instead of the default 27017. I also would want my database to use the <code>D:<\/code> drive instead of the <code>C:<\/code> drive for data storage.  <\/p>\n<p>I could configure my MongoDB database daemon via command-line parameters. However, to cater for future configuration changes, I had instead created a <code>mongod.conf<\/code> text file in the <code>D:\\mongodb<\/code> folder. And within <code>mongod.conf<\/code> file, I defined the <a href=\"http:\/\/www.mongodb.org\/display\/DOCS\/File+Based+Configuration\" title=\"File based configurations for MongoDB\" target=\"_blank\">configuration settings<\/a> that I want my MongoDB database daemon to take:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n# Folder for data storage \r\ndbpath = D:\\mongodb\\data \r\n\r\n# Indicate that log records are appended to existing log file\r\nlogappend = true \r\n\r\n# Log file that will contain the log records\r\nlogpath = D:\\mongodb\\log\\mongod.log\r\n\r\n# Database server listens on port 31234 \r\nport = 31234 \r\n\r\n# The log records should be detailed\r\nverbose = true \r\n<\/pre>\n<p>To conclude the configuration phase, I created the <code>D:\\mongodb\\data<\/code> and <code>D:\\mongodb\\log<\/code> folders as <code>MongoDB<\/code> will fail to install as a windows service if the specified folders does not exist.<\/p>\n<h3>The magical command to install MongoDB as a windows service<\/h3>\n<p>Within the <code>D:\\mongodb<\/code>, I looked for the <code>bin\\mongod.exe<\/code> binary, which is the MongoDB database process. Apart from serving as the MongoDB database process, <code>mongod.exe<\/code> also came with the feature for installing itself as a windows service.   <\/p>\n<p>With that, I navigate to the bin folder with my command prompt and execute the following command-line string:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmongod.exe --install -f d:\\mongodb\\mongod.conf \r\n<\/pre>\n<p>The process ended with the message: \"all output going to: D:\\mongodb\\log\\mongod.log\".<\/p>\n<p>And inside D:\\mongodb\\log\\mongod.log, I saw the following lines:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nSat Jul 21 17:24:20 BackgroundJob starting: DataFileSync\r\nCreating service MongoDB.\r\nService creation successful.\r\nService can be started from the command line via 'net start &quot;MongoDB&quot;'.\r\nSat Jul 21 17:24:21 dbexit: \r\nSat Jul 21 17:24:21 shutdown: going to close listening sockets...\r\nSat Jul 21 17:24:21 shutdown: going to flush diaglog...\r\nSat Jul 21 17:24:21 shutdown: going to close sockets...\r\nSat Jul 21 17:24:21 shutdown: waiting for fs preallocator...\r\nSat Jul 21 17:24:21 shutdown: lock for final commit...\r\nSat Jul 21 17:24:21 shutdown: final commit...\r\nSat Jul 21 17:24:21 shutdown: closing all files...\r\nSat Jul 21 17:24:21 closeAllFiles() finished\r\nSat Jul 21 17:24:21 dbexit: really exiting now\r\n<\/pre>\n<p>With that, the MongoDB windows service was installed successfully and I was able to see it with <code>services.msc<\/code>. <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/ph\/img\/blog\/posts\/mongodb-as-a-windows-service-in-services.msc.jpg\" alt=\"MongoDB installed as a windows service in services.msc\"><\/p>\n<h3>Some other posts which you may interest you<\/h3>\n<ul>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/how-to-retrieve-the-username-of-the-user-who-logged-onto-windows-from-windows-service\/\" title=\"How to retrieve the username of the user who logged onto Windows from windows service\" target=\"_blank\">How to retrieve the username of the user who logged onto Windows from windows service<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/my-experience-with-system-data-sqlite-in-c\/\" title=\"My experience with System.Data.SQLite in C#\" target=\"_blank\">My experience with System.Data.SQLite in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/a-mechanism-for-serving-http-requests-in-c\/\" title=\"A mechanism for serving HTTP requests in C#\" target=\"_blank\">A mechanism for serving HTTP requests in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/how-to-save-and-load-objects-to-and-from-file-in-c\/\" title=\"How to save and load objects to and from file in C#\" target=\"_blank\">How to save and load objects to and from file in C#<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/how-to-ensure-that-your-user-only-runs-one-instance-of-your-c-program\/\" title=\"How to ensure that your user only runs one instance of your C# program\" target=\"_blank\">How to ensure that your user only runs one instance of your C# program<\/a><\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-45\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-45&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-45&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F253&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>After using <a href=\"http:\/\/www.mongodb.org\/\" title=\"Link to MongoDB homepage\" target=\"_blank\">MongoDB<\/a> for quite a while, I realised that I had been repeating the starting up of the MongoDB database daemon whenever I reboot my PC. Manually starting the MongoDB database daemon via my command prompt whenever I want to work on my projects is inefficient.  <\/p>\n<p>Hence making MongoDB run as a windows service automatically when my computer starts up is one way to avoid repeating myself. In this post, I document how I install my MongoDB database as a windows service.<\/p>\n","protected":false},"author":1,"featured_media":1218,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[154,152,153],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/mongoDB-logo.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-45","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/253"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=253"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/253\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1218"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}