{"id":238,"date":"2020-09-24T10:22:03","date_gmt":"2020-09-24T08:22:03","guid":{"rendered":"https:\/\/blog.remyblom.nl\/?p=238"},"modified":"2020-11-17T10:20:56","modified_gmt":"2020-11-17T09:20:56","slug":"enabling-authentication-on-mongodb","status":"publish","type":"post","link":"https:\/\/blog.remyblom.nl\/?p=238","title":{"rendered":"Enabling authentication on MongoDB"},"content":{"rendered":"\n<p>When I first <a href=\"https:\/\/blog.remyblom.nl\/?p=220\">installed MongoDB on my OpenBSD<\/a> box I didn&#8217;t care about authentication, it was behind the firewall, purely for testing purposes. I first wanted to have some fun with it. But now I became curious and wanted to things right, so I enabled authentication.<\/p>\n\n\n\n<p>In order to do so we first need to create an admin user:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mongo\n> use admin\n> db.createUser({ user: \"admin\", pwd: passwordPrompt(), roles: &#91; { role: \"userAdminAnyDatabase\", db: \"admin\" }, \"readWriteAnyDatabase\" ] });\n> exit<\/code><\/pre>\n\n\n\n<p>After that enable authentication in <code>\/etc\/mongodb.conf<\/code> by adding:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>security:\n  authorization: enabled<\/code><\/pre>\n\n\n\n<p>Restart <code>mongod<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ doas rcctl restart mongod<\/code><\/pre>\n\n\n\n<p>After that you can connect to mongo with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ mongo --authenticationDatabase \"admin\" -u \"admin\" -p<\/code><\/pre>\n\n\n\n<p>And now we can create additional users for our applications:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>> use my_db\n> db.createUser({ \"user\": \"db_user\", \"pwd\": passwordPrompt(), roles: &#91;{ role: \"readWrite\", db: \"my_db\" }]})<\/code><\/pre>\n\n\n\n<p>And lastly we implement the credentials in our php-project:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\n$mongo = new MongoDB\\Driver\\Manager(\"mongodb:\/\/db_user:password@localhost:27017\/my_db\");<\/code><\/pre>\n\n\n\n<p>And we are set to go!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When I first installed MongoDB on my OpenBSD box I didn&#8217;t care about authentication, it was behind the firewall, purely for testing purposes. I first wanted to have some fun with it. But now I became curious and wanted to things right, so I enabled authentication. In order to do so we first need to &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.remyblom.nl\/?p=238\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Enabling authentication on MongoDB&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,15,10,4,6,2],"tags":[],"class_list":["post-238","post","type-post","status-publish","format-standard","hentry","category-code","category-config","category-lemp","category-openbsd","category-php","category-vps"],"_links":{"self":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts\/238","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=238"}],"version-history":[{"count":2,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions"}],"predecessor-version":[{"id":240,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=\/wp\/v2\/posts\/238\/revisions\/240"}],"wp:attachment":[{"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=238"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=238"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.remyblom.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=238"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}