How to put username &password in MongoDB (security&authentication)? (Configure user authentication in MongoDB)

Source: Internet
Author: User
Tags deprecated install mongodb mongodb client parent directory mongodump

Default do is need username and password authenticate when Access MongoDB, I want to set up the user name & password For my MongoDB. So, any remote access would ask for the user name & password. One is following:

Shutdown Server and exit
Restart Mongod with–auth option or using config file.

The username & password would work the same for Mongodump and Mongoexport.

[[email protected] bin]$ mongomongodb shell version:2.6.0connecting to:test> dbs2014-04-30t15:38:24.804+0800 Referenceerror:dbs isn't defined> show dbsadmin (empty) local 0.078GBtest 0.078gb> use adminswitched to DB admi N> db.adduser (' root ', ' MONGO '); Warning:the ' AddUser ' shell Helper is DEPRECATED. Please use ' createUser ' insteadsuccessfully added User: {"user": "Root", "roles": ["root"]}> dbadmin> show DBS  2014-04-30t15:46:32.070+0800 listdatabases failed:{"OK": 0, "errmsg": "Not authorized in admin to execute command {listdatabases:1.0} "," Code ": at src/mongo/shell/mongo.js:47> [[[Email protected] bin]$ Mon Go MongoDB Shell version:2.6.0connecting To:testerror while trying to show server startup Warnings:not authorized on AD Min to execute command {getLog: ' startupwarnings '}> use adminswitched to DB admin> show collections;2014-04-30t15: 48:02.980+0800 error: {"$err": "Not authorized for query On Admin.system.namespaces "," code ": @ src/mongo/shell/query.js:131> Db.auth (' root ', ' MONGO ');1> show C Ollections;system.indexessystem.userssystem.version> Db.system.users.find (); {"_id": "Admin.root", "User": "Root", "db": "admin", "credentials": {"MONGODB-CR": "7BC9AA6753E5241290FD85FECE372BD 8 "}," Roles ": [{" Role ":" Root "," db ":" Admin "}]}tip:deprecated since version 2.6:use Db.createuser () and DB.UPDA Teuser () instead of Db.adduser () to add users to MongoDB.db.createUser ({"User": "Anbob", "pwd": "MONGO" , "Roles": [{role: "Clusteradmin", DB: "Admin"}, {role: " Readanydatabase ", DB:" admin "}," ReadWrite "]}, {                  W: "Majority", wtimeout:5000}) > Use testswitched to DB test> db.createuser ({"User": "Anbob",...                  "pwd": "MONGO", ... "Roles": [ {role: "Clusteradmin", DB: "admin"},...                              {role: "Readanydatabase", DB: "admin"},... "ReadWrite" ...]                },... {w: "Majority", wtimeout:5000}); Successfully added User: {"user": "Anbob", "roles": [{"Role": "C Lusteradmin "," db ":" Admin "}, {" Role ":" Re Adanydatabase "," db ":" Admin "}," ReadWrite "]}> show Coll Ectionsfs.chunksfs.filessystem.indexestesttab> use adminswitched to DB admin> db.system.users.find (); {"_id": "Admin.root", "User": "Root", "db": "admin", "credentials": {"MONGODB-CR": "7BC9AA6753E5241290FD85FECE372BD 8 "}," Roles ": [{" Role ":" Root "," db ":" Admin "}]} {" _id ":" Test.anbob "," User ":" Anbob "," DB ":" Test "," credent IALs ": {" MONGODB-CR ":" 870c3c636f8f34ab73c5974df971190f "}," Roles ": [{" Role ":" Clusteradmin "," db ":" Admin "}, {" Role ":" Readanydatabase "," DB "  : "Admin"}, {"Role": "ReadWrite", "DB": "Test"}]}> exit[[email protected] bin]$ mongomongodb Shell version: 2.6.0connecting To:testerror while trying-show server startup Warnings:not authorized on Admin to execute command { GetLog: "Startupwarnings"}> db.auth (' anbob ', ' MONGO ');1> show collections; Fs.chunksfs.filessystem.indexestesttab> use testswitched to DB test> db.auth (' root ', ' MONGO ') error:18 {ok:0.0, er Rmsg: "Auth failed", code:18}0> db.auth (' anbob ', ' MONGO ') 1[[email protected] log]# vi/etc/mongodb.conf # Mongod b.conf# Where to store the data.# note:if your run MongoDB as a non-root user (recommended) You may# need to create and SE T permissions for this directory manually,# e.g., if the parent directory isn ' t mutable by the MongoDB user.dbpath=/data/d B#where to loglogpath=/var/log/mongodb/mongodb.loglogappend=true# TuRN on/off security.  OFF is currently the Default#noauth = Trueauth = Truefork = Truebind_ip = = 27017quiet = Truejournal = True[[email protected] bin]$ mongod--shutdownkilling process with pid:4227[[email protected] bin]$ Mongod-- Config/etc/mongodb.conf about to fork child process, waiting until server was ready for connections.forked process:4867ch   ILD Process started successfully, parent exiting[[email protected] bin]$ ps-ef|grep mongo|grep-v greproot 3873 3839 0 10:32 pts/2 00:00:00 su-mongomongo 3874 3873 0 10:32 pts/2 00:00:00-bashmongo 4867 1 0 1        6:17? 00:00:00 mongod--config/etc/mongodb.confmongo 4879 3874 0 16:18 pts/2 00:00:00 ps-ef[[email protected] Bi  n]$ MONGO anbob-p mongomongodb Shell version:2.6.0connecting to:> dbtest

Recommend MongoDB Client:
Robomongo and Umongo

Related Posts:
    • How to install the MongoDB 2.6 on Linux


Sorry, this article temporarily closes the comment.

Previous post: Backup\restore MongoDB using Mongoexport, Mongoimport, Mongodump, Mongorestore (mongodb backup recovery)

Next post: How to install MongoDB 2.6 on Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.