{"componentChunkName":"component---src-templates-post-tsx","path":"/posts/2019/07/mongo-digitalocean/","result":{"data":{"markdownRemark":{"fields":{"slug":"/2019/07/mongo-digitalocean/"},"frontmatter":{"title":"Hosting MongoDB version 4 server on DigitalOcean","tag":["mongodb","digitalocean"],"image":"https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSe5Mr66_-cGVpyv_4SIkxAx1h0thctF1Ri8th9kS86mWy20wuQ"},"correctedDateEpoch":1561914000000,"html":"<p>My gripe comes from the limitation with free tier and lower tiers of MongoDB Atlas. (<a href=\"https://www.quora.com/unanswered/Where-can-I-host-MongoDB-with-allowDiskUse-true\">Where can I host MongoDB with allowDiskUse=true?</a>) So, I have decided to host my own MongoDB on DigitalOcean.</p>\n<!-- excerpt_separator -->\n<h2>Setting up a droplet and installing MongoDB</h2>\n<p>So, this is the only relatively easy part. It can be installed with either</p>\n<ul>\n<li><a href=\"https://pages.news.digitalocean.com/AF2V0vEI30XV5290060l08D\">One click install</a></li>\n<li>Setting up a server from scratch</li>\n</ul>\n<p>Since one click install is too easy, yet I don't have a luck with it too much, I am not going to discuss this one. One click install comes with MongoDB 4 already.</p>\n<p>To setup a server from scratch, I chose Ubuntu 19.04.</p>\n<p>Then, choose a cheapest tier. This is hidden beneath the left arrow.</p>\n<p>For authentication, use SSH. I am using macOS, so I can create SSH key with <code>ssh-keygen -t rsa</code>. After the code is generated, copy the key with <code>cat ~/.ssh/id_rsa.pub</code>, but <code>cat</code> doesn't work on my machine, so I use <code>code</code> (Visual Studio Code CLI) instead. (<code>nano</code> should also works.)</p>\n<p>Then, press \"Create\".</p>\n<p>Then, after the droplet is created, you will see the IP address. Log in to the server with the terminal.</p>\n<pre><code>$ ssh root@SERVER_IP_ADDRESS\n</code></pre>\n<p>To install MongoDB 4, follow the following steps -- , but if you have installed MongoDB 3, you have additional steps to take -- <a href=\"https://askubuntu.com/a/1054323/5156\">https://askubuntu.com/a/1054323/5156</a></p>\n<h2>Create users on MongoDB and set permissions</h2>\n<ul>\n<li>Run <code>mongo</code></li>\n<li>Create user \"admin\", by typing this in Mongo Shell</li>\n</ul>\n<pre><code>use admin\ndb.createUser(\n  {\n    user: \"myUserAdmin\",\n    pwd: \"abc123\",\n    roles: [ { role: \"userAdminAnyDatabase\", db: \"admin\" }, \"readWriteAnyDatabase\" ]\n  }\n)\n</code></pre>\n<ul>\n<li>DO NOT shutdown the <code>mongod</code> instance</li>\n<li>Login to admin <code>mongo --port 27017 -u \"myUserAdmin\" --authenticationDatabase \"admin\" -p</code></li>\n<li>Create additional users</li>\n</ul>\n<pre><code>use test\ndb.createUser(\n  {\n    user: \"myTester\",\n    pwd: \"xyz123\",\n    roles: [ { role: \"readWrite\", db: \"test\" },\n             { role: \"read\", db: \"reporting\" } ]\n  }\n)\n</code></pre>\n<ul>\n<li><code>nano /etc/mongod.conf</code>, enable <code>security:</code>, and add <code>authorization: enabled</code></li>\n<li>Allow your IP address on <code>ufw</code> </li>\n<li>If you use WiFi, or something that IP address varies, you might consider \"Allow from anywhere\"</li>\n</ul>\n<p>That's it. Now you can login to MongoDB from outside via <code>mongodb://USERNAME:PASSWORD@SERVER_IP_ADDRESS:27017/test</code></p>\n<p>You might consider using SSH tunneling.</p>"}},"pageContext":{"slug":"/2019/07/mongo-digitalocean/"}}}