{"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
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>\nEnable Auth<\/h2>\n
Next we enable authentication via the use of the –auth\u00a0 switch.<\/p>\n
mongo --auth --config \/etc\/mongo.conf<\/pre>\nYou 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>\nCreate 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>\nDjango<\/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>\nNext 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":"\n
How to Enable Authentication and Create DB Users in MongoDB - Fir3net<\/title>\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\t\n\t\n\t\n