首页 > 代码库 > How to put username &password in MongoDB(Security&Authentication)?(配置用户认证在MongoDB)

How to put username &password in MongoDB(Security&Authentication)?(配置用户认证在MongoDB)

Default do not need username and password authenticate when access mongoDB ,I want to set up the user name & password for my mongoDB. so that any remote access will ask for the user name & password. one way is following:

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

tip:
The username & password will work the same for mongodump and mongoexport.

[mongo@db231 bin]$ mongoMongoDB shell version: 2.6.0connecting to: test> dbs2014-04-30T15:38:24.804+0800 ReferenceError: dbs is not defined> show dbsadmin  (empty)local  0.078GBtest   0.078GB> use adminswitched to db admin> db.addUser(‘root‘,‘mongo‘);WARNING: The ‘addUser‘ shell helper is DEPRECATED. Please use ‘createUser‘ insteadSuccessfully added user: { "user" : "root", "roles" : [ "root" ] }> dbadmin> show dbs2014-04-30T15:46:32.070+0800 listDatabases failed:{        "ok" : 0,        "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }",        "code" : 13} at src/mongo/shell/mongo.js:47> [mongo@db231 bin]$ mongo MongoDB shell version: 2.6.0connecting to: testError while trying to show server startup warnings: not authorized on admin 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" : 13} at src/mongo/shell/query.js:131> db.auth(‘root‘,‘mongo‘);1> show collections;system.indexessystem.userssystem.version> db.system.users.find();{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "MONGODB-CR" : "7bc9aa6753e5241290fd85fece372bd8" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }TIP:Deprecated since version 2.6: Use db.createUser() and db.updateUser() 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" : "clusterAdmin",                        "db" : "admin"                },                {                        "role" : "readAnyDatabase",                        "db" : "admin"                },                "readWrite"        ]}> show collectionsfs.chunksfs.filessystem.indexestesttab> use adminswitched to db admin> db.system.users.find();{ "_id" : "admin.root", "user" : "root", "db" : "admin", "credentials" : { "MONGODB-CR" : "7bc9aa6753e5241290fd85fece372bd8" }, "roles" : [ { "role" : "root", "db" : "admin" } ] }{ "_id" : "test.anbob", "user" : "anbob", "db" : "test", "credentials" : { "MONGODB-CR" : "870c3c636f8f34ab73c5974df971190f" }, "roles" : [ { "role" : "clusterAdmin", "db" : "admin" }, { "role" : "readAnyDatabase", "db" : "admin" }, { "role" : "readWrite", "db" : "test" } ] }> exit[mongo@db231 bin]$ mongoMongoDB shell version: 2.6.0connecting to: testError while trying to 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, errmsg: "auth failed", code: 18 }0> db.auth(‘anbob‘,‘mongo‘)1[root@db231 log]# vi /etc/mongodb.conf # mongodb.conf# Where to store the data.# Note: if you run mongodb as a non-root user (recommended) you may# need to create and set permissions for this directory manually,# e.g., if the parent directory isn‘t mutable by the mongodb user.dbpath=/data/db#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[mongo@db231 bin]$ mongod --shutdownkilling process with pid: 4227[mongo@db231 bin]$ mongod --config /etc/mongodb.conf about to fork child process, waiting until server is ready for connections.forked process: 4867child process started successfully, parent exiting[mongo@db231 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 16:17 ?        00:00:00 mongod --config /etc/mongodb.confmongo     4879  3874  0 16:18 pts/2    00:00:00 ps -ef[mongo@db231 bin]$ 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 MongoDB 2.6 on linux

Nosql

对不起,这篇文章暂时关闭评论。

 

上一篇: Backup\Restore MongoDB using mongoexport, mongoimport, mongodump, mongorestore(MongoDB备份恢复)

下一篇: How to install MongoDB 2.6 on linux

url:http://www.anbob.com/archives/2268.html