{"id":20322,"date":"2016-04-21T09:53:21","date_gmt":"2016-04-21T15:23:21","guid":{"rendered":"https:\/\/www.solutionanalysts.com\/blog\/how-to-do-user-authentication-in-mongodb-prevent-others-to-access-mongodb\/"},"modified":"2023-08-16T23:07:24","modified_gmt":"2023-08-17T04:37:24","slug":"how-to-do-user-authenticat-in-mongodb-prevent-others-to-access-mongodb","status":"publish","type":"post","link":"https:\/\/www.solutionanalysts.com\/blog\/how-to-do-user-authenticat-in-mongodb-prevent-others-to-access-mongodb\/","title":{"rendered":"How to do user authentication in MongoDB, Prevent others to access MongoDB ?"},"content":{"rendered":"<p>MongoDB, a cross-platform document-oriented database. Classified as a NoSQL database, MongoDB eschews the traditional table-based relational database structure in favor of JSON-like documents with dynamic schemas (MongoDB calls the format BSON), making the integration of data in certain types of applications easier and faster.<\/p>\n<p>Here I am going to explain how to apply user-name and password(authenticate) to mongoDB&#8217;s database.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"1_Open_shell_command_Fire_this_command\"><\/span>#1. Open shell command. Fire this command<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>command &#8211; mongo<br \/>\noutput-<br \/>\nMongoDB shell version: 3.0.4<br \/>\nconnecting to: test<\/p>\n<h3><span class=\"ez-toc-section\" id=\"2\"><\/span>#2.<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>command &#8211; show dbs;<br \/>\noutput-<br \/>\nadmin 0.078GB<br \/>\nlocal 0.078GB<br \/>\nauth_db 0.078GB<\/p>\n<h3><span class=\"ez-toc-section\" id=\"3\"><\/span>#3.<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>command &#8211; use auth_db;<br \/>\noutput-<br \/>\nswitched to auth_db<\/p>\n<p>Here, we have selected auth_db Database for apply authentication.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"4_Fire_query\"><\/span>#4. Fire query :<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>db.createUser({user:&#8221;YOUR_USERNAME&#8221;, pwd:&#8221;YOUR_PWD&#8221;,roles:[&#8220;readWrite&#8221;,&#8221;dbAdmin&#8221;]})<\/p>\n<p>We have created User of database &#8216;auth_db&#8217;. You can set your user-name and password to above query.<\/p>\n<p>Make sure authentication is true. You can set authentication = true in MongoDB.conf file.<\/p>\n<ul>\n<li><strong>Option 1:<\/strong> In mongod.conf, Remove comment and make it true.<br \/>\nauth=true;- restart mongoDB &#8211; sudo service mongod restart<\/li>\n<li><strong>Option 2 :<\/strong> command to run mongodb with auth true :- mongod -auth<\/li>\n<\/ul>\n<h3><span class=\"ez-toc-section\" id=\"5_In_database_config_files_in_Nodejs\"><\/span>#5. In database config files in Nodejs<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><code>var mongoose = require('mongoose');<br \/>\nvar uri = 'mongodb:\/\/localhost:27017\/auth_db';<br \/>\nvar options = {<br \/>\nuser: 'YOUR_USERNAME',<br \/>\npass: 'YOUR_PWD'<br \/>\n}<br \/>\nmongoose.connect(uri, options, function(err, result){<br \/>\nconsole.log('err --&gt;' + JSON.stringify(err));<br \/>\n});<br \/>\n<\/code><\/p>\n<p>Without user-name and password, would not able to perform an operation with the database. If you don&#8217;t want to use authentication to database just make auth=true is commented in MongoDB.conf file or start mongo without -auth i.e mongo . Please do not forget restart MongoDB.<\/p>\n<p>You can drop a comment for any questions or feedback. We Will get back to you soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MongoDB, a cross-platform document-oriented database. Classified as a NoSQL database, MongoDB eschews<\/p>\n","protected":false},"author":1,"featured_media":20323,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[132],"tags":[],"class_list":["post-20322","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-big-data"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20322","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/comments?post=20322"}],"version-history":[{"count":1,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20322\/revisions"}],"predecessor-version":[{"id":32814,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/posts\/20322\/revisions\/32814"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media\/20323"}],"wp:attachment":[{"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/media?parent=20322"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/categories?post=20322"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.solutionanalysts.com\/blog\/wp-json\/wp\/v2\/tags?post=20322"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}