<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:media="http://search.yahoo.com/mrss/"><channel><title><![CDATA[MongoDB - Bacon Applications]]></title><description><![CDATA[Follow me while I share with the world useful things I have learned and projects I am working on.]]></description><link>http://www.baconapplications.com/</link><image><url>http://www.baconapplications.com/favicon.png</url><title>MongoDB - Bacon Applications</title><link>http://www.baconapplications.com/</link></image><generator>Ghost 3.7</generator><lastBuildDate>Thu, 20 Mar 2025 11:06:43 GMT</lastBuildDate><atom:link href="http://www.baconapplications.com/tag/mongodb/rss/" rel="self" type="application/rss+xml"/><ttl>60</ttl><item><title><![CDATA[Installing and Securing MongoDB on Windows]]></title><description><![CDATA[Quickly learn how to install and secure MongoDB on Windows for development.  Also learn how to create a secure log in and a new database.]]></description><link>http://www.baconapplications.com/installing-and-securing-mongodb-on-windows/</link><guid isPermaLink="false">5e535ef4ba89970001e0b05f</guid><category><![CDATA[MongoDB]]></category><dc:creator><![CDATA[Randy Bacon]]></dc:creator><pubDate>Mon, 19 Jan 2015 22:53:41 GMT</pubDate><media:content url="http://www.baconapplications.com/content/images/2020/02/cover-1.jpg" medium="image"/><content:encoded><![CDATA[<!--kg-card-begin: markdown--><img src="http://www.baconapplications.com/content/images/2020/02/cover-1.jpg" alt="Installing and Securing MongoDB on Windows"><p>Generally I have found that for the sake of speed, installing MongoDB on Windows isn't the best solution in production.  However for development, this might be your only or best option. Having a local installed MongoDB is convenient and useful for quickly prototyping concepts.</p>
<h3 id="downloadmongodb">Download MongoDB</h3>
<p>To install MongoDB first download and run the installer from <a href="http://www.mongodb.org/downloads" target="_blank">http://www.mongodb.org/downloads</a>.</p>
<p>Run the installer and advance to the following screen and click &quot;Custom&quot;:</p>
<p><img src="http://www.baconapplications.com/content/images/posts/mongodb-windows/customize_installer.png" alt="Installing and Securing MongoDB on Windows"></p>
<p>Expand the tree and select all options except &quot;Router&quot;.  You will only need to install the MongoDB router for a distributed DB and chances are for development you will not need this option.  If you think you might need this option, go ahead and install it.  Before clicking &quot;Next&quot; - select the root item and click &quot;Browse...&quot; to choose a new directory on a root drive (such as C:/).  I <em>highly</em> recommend this as it will make running command line tools much easier without having to use an administrator command prompt.  I will be using E:/mongoDB for my install.</p>
<p><img src="http://www.baconapplications.com/content/images/posts/mongodb-windows/select_options.png" alt="Installing and Securing MongoDB on Windows"></p>
<p>To finish the installation, click &quot;Next&quot; and &quot;Install&quot;.</p>
<h3 id="testingtheinstall">Testing the Install</h3>
<p>Once the installer has finished, you should test the install.  First you should create a data and log directory for the install.  I will use E:/mongoDB/data and E:/mongoDB/logs for both.</p>
<p>Once you have created these directories, open a <a href="http://windows.microsoft.com/en-us/windows/command-prompt-faq#1TC=windows-8" target="_blank">command prompt</a> and direct it to your install directory - E:/mongoDB/bin.  The bin directory is the root for all install MongoDB related programs and tools.  Run the following command to start MongoDB:</p>
<pre>
	E:\mongodb\bin\mongod --dbpath E:\mongodb\data
</pre>
<p>Leave this command prompt window running and start another command prompt and again direct it to your MongoDB directory.  Run this command to connect to the MongoDB server:</p>
<pre>
	E:\mongodb\bin\mongo
</pre>
<p>You should now see a prompt.  Try running the command &quot;show databases&quot; to see if the server is running correctly.</p>
<p><img src="http://www.baconapplications.com/content/images/posts/mongodb-windows/mongo_first_connect.png" alt="Installing and Securing MongoDB on Windows"></p>
<p>To exit this connection, type exit.  To end the server session, enter control+C.</p>
<h3 id="settinguptheconfigurationformongodb">Setting up the Configuration for MongoDB</h3>
<p>Before setting up MongoDB to run as a service, you should take a minute to set up the default configuration for your install.  Navigate to your install directory and create a mongod.cfg file.  Add the following lines to this file:</p>
<script src="https://gist.github.com/baconapplications/eb6d970ab893b4413c99.js"></script>
<p>Test your new db configuration via the following:</p>
<pre>
	E:\mongodb\bin\mongod --config=E:\mongodb\mongod.cfg
</pre>
<p>In another command prompt window connect to the service via the IP and port as follows:</p>
<pre>
	E:\mongodb\bin\mongo 127.0.0.1:37011 
</pre>
<h3 id="settingupmongodbtorunasaservice">Setting up MongoDB to Run as a Service</h3>
<p>You can use the installed MongoDB application itself to set up MongoDB to run as a service, but I like to have a little more control over the service install.  Use the following line to set up the service.  NOTE: You will need to run this command from an administrator command prompt.</p>
<pre>
sc.exe create MongoDB binPath= "\"E:\mongoDB\bin\mongod.exe --service --config=E:\mongoDB\mongod.cfg\" DisplayName= "MongoDB" start= "auto"
</pre>
<p>Once the service is created, start the service from the Services control panel or use the following to start the service:</p>
<pre>
net start MongoDB
</pre>
<p>Again, using the command prompt above, connect to the service using the mongo executable on port 37011.</p>
<p>To delete the service, run the following from an administrator command prompt:</p>
<pre>
net stop MongoDB
sc delete MongoDB
</pre>
<h3 id="troubleshootingserviceinstall">Troubleshooting Service Install</h3>
<p>If you followed the steps above and you receive an error message, make sure the line to add a service is entered exactly as above.  The spacing and quotes are important for the service to be added correctly.</p>
<h3 id="securingyourmongodbinstall">Securing Your MongoDB Install</h3>
<p>Security is not enforced in the default install of MongoDB.  If you want to enable security, follow the next steps.  I like to enable security on my install so that my development environment will be closer to production.</p>
<p>First log into your install and create a DB admin user:</p>
<pre>
mongo 127.0.0.1:37011/admin
use admin
db.createUser(
  {
    user: "mongoadmin",
    pwd: "p@ssw0rd",
    roles:
    [
      {
        role: "userAdminAnyDatabase",
        db: "admin"
      }
    ]
  }
)
</pre>
<p>Once you have added the mongoadmin user, quit mongo and add the following to the mongod.cfg you created earlier.</p>
<pre>
auth=true
</pre>
<p>After adding the above to the cfg file, restart the MongoDB service.  Now you will need to use the username and password to access the databases.  If you try to connect to the MongoDB service without credentials and run the &quot;show databases&quot; command, you will see an error:</p>
<p><img src="http://www.baconapplications.com/content/images/posts/mongodb-windows/invalid_creds.png" alt="Installing and Securing MongoDB on Windows"></p>
<p>However using the following to connect, you will be able view the databases:</p>
<pre>
mongo 127.0.0.1:37011/admin --username mongoadmin --password
</pre>
<p><img src="http://www.baconapplications.com/content/images/posts/mongodb-windows/valid_creds.png" alt="Installing and Securing MongoDB on Windows"></p>
<h3 id="creatinganewdatabase">Creating a New Database</h3>
<p>Now you finally have your MongoDB service up and running.  The next step is to create a new database with a unique user.  First log into the admin db with your admin user:</p>
<pre>
mongo 127.0.0.1:37011/admin --username mongoadmin --password
</pre>
<p>Next use the following commands to create a new database with the mongoadmin user and a database user:</p>
<pre>
use testDB1
db.createUser(
  {
    user: "mongoadmin",
    pwd: "p@ssw0rd",
    roles:
    [
      {
        role: "dbOwner",
        db: "testDB1"
      }
    ]
  }
)

db.createUser(
  {
    user: "mydbuser",
    pwd: "test1234",
    roles:
    [
      {
        role: "readWrite",
        db: "testDB1"
      }
    ]
  }
)
</pre>
<p>To test the created user, quit the current mongo shell and log back in using the new database user as follows:</p>
<pre>
mongo 127.0.0.1:37011/testDB1 --username mydbuser
</pre>
<p>Finally create a new collection and return the result</p>
<pre>
db.test.insert({"name":"test value"})
db.test.find()
</pre>
<h3 id="conclusion">Conclusion</h3>
<p>Hopefully you found this guide helpful.  The MongoDB install guide is actually quite detailed now and can be found at <a href="http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/" target="blank">http://docs.mongodb.org/manual/tutorial/install-mongodb-on-windows/</a>.  Until next time - happy coding!</p>
<!--kg-card-end: markdown-->]]></content:encoded></item></channel></rss>