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.
Tip
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 = 192.168.168.231port = 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 192.168.168.231/test-u anbob-p mongomongodb Shell version:2.6.0connecting to:192.168.168.231/test> dbtest
Recommend MongoDB Client:
Robomongo and Umongo
Related Posts:
- How to install the MongoDB 2.6 on Linux
Nosql
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
Url:http://www.anbob.com/archives/2268.html