{"id":967,"date":"2016-02-06T15:34:19","date_gmt":"2016-02-06T15:34:19","guid":{"rendered":"https:\/\/fir3netwp.gmsrrpobkbd.com\/2016\/02\/06\/how-to-enable-authentication-and-create-db-users-in-mongodb\/"},"modified":"2021-07-31T11:19:36","modified_gmt":"2021-07-31T11:19:36","slug":"how-to-enable-authentication-and-create-db-users-in-mongodb","status":"publish","type":"post","link":"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html","title":{"rendered":"How to Enable Authentication and Create DB Users in MongoDB"},"content":{"rendered":"

By default authentication is not enabled within Mongo.\u00a0 In this article we will show you how to enable authentication , create an account for global mongo administration and also create an account for database level authentication.<\/p>\n

Create UserAdmin<\/h2>\n

First of all we go into the mongo shell and create our admin user.<\/p>\n

use admin\r\ndb.createUser(\r\n  {\r\n    user: \"admin\",\r\n    pwd: \"password\",\r\n    roles: [ { role: \"root\", db: \"admin\" } ]\r\n  }\r\n)\r\nexit;<\/pre>\n

Enable Auth<\/h2>\n

Next we enable authentication via the use of the –auth\u00a0 switch.<\/p>\n

mongo --auth --config \/etc\/mongo.conf<\/pre>\n

You can use the following command to check to see that auth is enabled\u00a0db.getUsers()<\/span><\/p>\n

> db.getUsers()\r\n2016-10-24T12:35:16.962+0100 Error: not authorized on test to execute command { usersInfo: 1.0 \r\n} at src\/mongo\/shell\/db.js:1363<\/pre>\n

Create DB Users<\/h2>\n

Finally lets create another user and assign it to a single database.<\/p>\n

db.auth('admin','password')\r\nuse example\r\ndb.createUser(\r\n  {\r\n    user: \"example\",\r\n    pwd: \"password\",\r\n    roles: [ { role: \"readWrite\", db: \"example\" } ]\r\n  }\r\n)<\/pre>\n

Django<\/h2>\n

In order to connect to Mongo with your new user you must first install pymongo,<\/p>\n

pip install pymongo==2.8.1<\/pre>\n

Next add the following is added to your settings.py<\/span><\/p>\n

import mongoengine\r\n\r\n_MONGODB_USER = 'example'\r\n_MONGODB_PASSWD = 'password'\r\n_MONGODB_HOST = '127.0.0.1'\r\n_MONGODB_NAME = 'example'\r\n\r\nmongoengine.connect(_MONGODB_NAME, host='mongodb:\/\/%s:%s@%s:27017\/%s' % (_MONGODB_USER,\r\n                                                                         _MONGODB_PASSWD,\r\n                                                                         _MONGODB_HOST,\r\n                                                                         _MONGODB_NAME))<\/pre>\n","protected":false},"excerpt":{"rendered":"

By default authentication is not enabled within Mongo.\u00a0 In this article we will show you how to enable authentication , create an account for global mongo administration and also create an account for database level authentication. Create UserAdmin First of all we go into the mongo shell and create our admin user. use admin db.createUser( … Read more<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[67],"tags":[],"yoast_head":"\nHow to Enable Authentication and Create DB Users in MongoDB - Fir3net<\/title>\n<meta name=\"description\" content=\"By default authentication is not enabled within Mongo.\u00a0 In this article we will show you how to enable authentication , create an account for global mongo\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Enable Authentication and Create DB Users in MongoDB - Fir3net\" \/>\n<meta property=\"og:description\" content=\"By default authentication is not enabled within Mongo.\u00a0 In this article we will show you how to enable authentication , create an account for global mongo\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html\" \/>\n<meta property=\"og:site_name\" content=\"Fir3net\" \/>\n<meta property=\"article:published_time\" content=\"2016-02-06T15:34:19+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-07-31T11:19:36+00:00\" \/>\n<meta name=\"author\" content=\"Rick Donato\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rick Donato\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html\"},\"author\":{\"name\":\"Rick Donato\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\"},\"headline\":\"How to Enable Authentication and Create DB Users in MongoDB\",\"datePublished\":\"2016-02-06T15:34:19+00:00\",\"dateModified\":\"2021-07-31T11:19:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html\"},\"wordCount\":131,\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"articleSection\":[\"MongoDB\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html\",\"url\":\"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html\",\"name\":\"How to Enable Authentication and Create DB Users in MongoDB - Fir3net\",\"isPartOf\":{\"@id\":\"https:\/\/www.fir3net.com\/#website\"},\"datePublished\":\"2016-02-06T15:34:19+00:00\",\"dateModified\":\"2021-07-31T11:19:36+00:00\",\"description\":\"By default authentication is not enabled within Mongo.\u00a0 In this article we will show you how to enable authentication , create an account for global mongo\",\"breadcrumb\":{\"@id\":\"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.fir3net.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Operating Systems\",\"item\":\"https:\/\/www.fir3net.com\/os\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Databases\",\"item\":\"https:\/\/www.fir3net.com\/os\/databases\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"MongoDB\",\"item\":\"https:\/\/www.fir3net.com\/os\/databases\/mongodb\"},{\"@type\":\"ListItem\",\"position\":5,\"name\":\"How to Enable Authentication and Create DB Users in MongoDB\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.fir3net.com\/#website\",\"url\":\"https:\/\/www.fir3net.com\/\",\"name\":\"Fir3net\",\"description\":\"Keeping you in the know\",\"publisher\":{\"@id\":\"https:\/\/www.fir3net.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.fir3net.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.fir3net.com\/#organization\",\"name\":\"Fir3net\",\"url\":\"https:\/\/www.fir3net.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"contentUrl\":\"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png\",\"width\":390,\"height\":88,\"caption\":\"Fir3net\"},\"image\":{\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037\",\"name\":\"Rick Donato\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g\",\"caption\":\"Rick Donato\"},\"description\":\"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders.\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Enable Authentication and Create DB Users in MongoDB - Fir3net","description":"By default authentication is not enabled within Mongo.\u00a0 In this article we will show you how to enable authentication , create an account for global mongo","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html","og_locale":"en_US","og_type":"article","og_title":"How to Enable Authentication and Create DB Users in MongoDB - Fir3net","og_description":"By default authentication is not enabled within Mongo.\u00a0 In this article we will show you how to enable authentication , create an account for global mongo","og_url":"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html","og_site_name":"Fir3net","article_published_time":"2016-02-06T15:34:19+00:00","article_modified_time":"2021-07-31T11:19:36+00:00","author":"Rick Donato","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Rick Donato","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html#article","isPartOf":{"@id":"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html"},"author":{"name":"Rick Donato","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037"},"headline":"How to Enable Authentication and Create DB Users in MongoDB","datePublished":"2016-02-06T15:34:19+00:00","dateModified":"2021-07-31T11:19:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html"},"wordCount":131,"publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"articleSection":["MongoDB"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html","url":"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html","name":"How to Enable Authentication and Create DB Users in MongoDB - Fir3net","isPartOf":{"@id":"https:\/\/www.fir3net.com\/#website"},"datePublished":"2016-02-06T15:34:19+00:00","dateModified":"2021-07-31T11:19:36+00:00","description":"By default authentication is not enabled within Mongo.\u00a0 In this article we will show you how to enable authentication , create an account for global mongo","breadcrumb":{"@id":"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.fir3net.com\/Databases\/Mongodb\/how-to-enable-authentication-and-create-db-users-in-mongodb.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.fir3net.com\/"},{"@type":"ListItem","position":2,"name":"Operating Systems","item":"https:\/\/www.fir3net.com\/os"},{"@type":"ListItem","position":3,"name":"Databases","item":"https:\/\/www.fir3net.com\/os\/databases"},{"@type":"ListItem","position":4,"name":"MongoDB","item":"https:\/\/www.fir3net.com\/os\/databases\/mongodb"},{"@type":"ListItem","position":5,"name":"How to Enable Authentication and Create DB Users in MongoDB"}]},{"@type":"WebSite","@id":"https:\/\/www.fir3net.com\/#website","url":"https:\/\/www.fir3net.com\/","name":"Fir3net","description":"Keeping you in the know","publisher":{"@id":"https:\/\/www.fir3net.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.fir3net.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.fir3net.com\/#organization","name":"Fir3net","url":"https:\/\/www.fir3net.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","contentUrl":"https:\/\/www.fir3net.com\/wp-content\/uploads\/Fir3net-Background-Logo-compressed.png","width":390,"height":88,"caption":"Fir3net"},"image":{"@id":"https:\/\/www.fir3net.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/ab35009601b7687ee1c5310be6038037","name":"Rick Donato","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.fir3net.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d75d69a54c0ca3b32c24c3a9703b623c?s=96&d=mm&r=g","caption":"Rick Donato"},"description":"Rick Donato is a Network Automation Architect\/Evangelist and the founder of Packet Coders."}]}},"_links":{"self":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/967"}],"collection":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/comments?post=967"}],"version-history":[{"count":0,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/posts\/967\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/media?parent=967"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/categories?post=967"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.fir3net.com\/wp-json\/wp\/v2\/tags?post=967"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}