@zero1036
2017-03-28T08:47:33.000000Z
字数 2117
阅读 2574
Mongodb
查看mongod.conf配置:cat /data/mongodb/conf/mongod.conf
dbpath=/data/mongodb/data/logpath=/data/mongodb/logs/mongodb.loglogappend=truefork=truejournal=trueoplogSize=10000#smallfiles=true#noprealloc=truedirectoryperdb=trueauth=true #auth配置为true,要求登录身份授权#keyFile=/data/mongodb/conf/keyfile#replSet=repset
cmd:./mongo -host 192.168.1.7272 -port 27017 -u tgor -p 12345 --authenticationDatabase admin。
-host:host-port:端口-u:用户名--authenticationDatabase:授权数据库,非常重要,参考下文--authenticationDatabase与-d区别在于,前者是指登录用户所属数据库;后者是登录用户的授权数据库。
参考备份工具mongodump以下例子:用户tgor必须是admin库创建的成员,且具有db1库的读写权限;
./mongodump --host 192.168.1.7272 --port 27017 --authenticationDatabase admin -d db1 -u tgor -p 12345 -c ActivityResult -o /data/testswitched to db admin> db.getUsers()[{"_id" : "admin.tgor", //tgor是admin的成员"user" : "tgor","db" : "admin","roles" : [{"role" : "dbAdmin","db" : "admin"},{"role" : "dbAdmin","db" : "db1"}]}]
反之,如果指定所属数据库没有指定用户,则会报错auth failed。
./mongodump --host 192.168.1.7272 --port 27017 --authenticationDatabase db1 -d db1 -u tgor -p 12345 -c ActivityResult -o /data/test
use admindb.createUser({user: "tgor",pwd: "12345",roles: [ { role: "__system", db: "admin" },{role: "dbAdmin", db: "MissionV2"} ]})
创建成功返回结果:
Successfully added user: {"user" : "ppmoney","roles" : [{"role" : "__system","db" : "admin"},{"role" : "dbAdmin","db" : "MissionV2"}]}
| 项目 | 价格 |
|---|---|
| readAnyDatabase | 对所有数据库中的collection可读,同时包含listDatabases权限 |
| readWriteAnyDatabase | 对所有数据库中的collection可读且可写,同时包含listDatabases权限 |
| userAdminAnyDatabase | 对所有数据库拥有userAdmin角色,同时包含listDatabases权限 |
| dbAdminAnyDatabase | 对所有数据库拥有dbAdmin角色,同时包含listDatabases权限 |
| cluster | 相关的权限 clusterMonitor、hostManager、clusterManager、clusterAdmin |
| root | 包含 readWriteAnyDatabase, dbAdminAnyDatabase, userAdminAnyDatabase 和 clusterAdmin 等角色。 但不能访问system. 开头的collection(root does not include any access to collections that begin with the system. prefix.) |
| __system | 超级管理员 |
db.createUser({user: "ppmoney",pwd: "Da3LzMogT88DjDleiE8",roles: [ { role: "__system", db: "admin" },{role: "dbAdmin", db: "MissionV2"} ]})
db.getUsers()[{"_id" : "admin.ppmoney","user" : "tgor","db" : "admin","roles" : [{"role" : "__system","db" : "admin"},{"role" : "dbAdmin","db" : "db1"}]}]