mongodb_权限

来源:互联网 发布:apm性能监控 java 编辑:程序博客网 时间:2024/05/19 01:06

mongoDB权限

  1. 通过help()查看帮助
  2. 找到创建用户命令,创建用户
  3. 停止MongoDB服务,并卸载remove
  4. 添加–auth参数重装mongo服务,mongod … –auth
  5. 启动服务后就添加了权限
  6. 在PHP或者python中使用时URI:mongodb://用户名:密码@127.0.0.1:27017
---------MONGODB命令----------> use adminswitched to db admin> db.help()DB methods:        db.adminCommand(nameOrDocument) - switches to 'admin' db, and runs command [ just calls db.runCommand(...) ]        db.auth(username, password)        db.cloneDatabase(fromhost)        db.commandHelp(name) returns the help for the command        db.copyDatabase(fromdb, todb, fromhost)        db.createCollection(name, { size : ..., capped : ..., max : ... } )        db.createView(name, viewOn, [ { $operator: {...}}, ... ], { viewOptions } )        db.createUser(userDocument)        db.currentOp() displays currently executing operations in the db        db.dropDatabase()        db.eval() - deprecated        db.fsyncLock() flush data to disk and lock server for backups        db.fsyncUnlock() unlocks server following a db.fsyncLock()        db.getCollection(cname) same as db['cname'] or db.cname        db.getCollectionInfos([filter]) - returns a list that contains the names and options of the db's collections        db.getCollectionNames()        db.getLastError() - just returns the err msg string        db.getLastErrorObj() - return full status object        db.getLogComponents()        db.getMongo() get the server connection object        db.getMongo().setSlaveOk() allow queries on a replication slave server        db.getName()        db.getPrevError()        db.getProfilingLevel() - deprecated        db.getProfilingStatus() - returns if profiling is on and slow threshold        db.getReplicationInfo()        db.getSiblingDB(name) get the db at the same server as this one        db.getWriteConcern() - returns the write concern used for any operations on this db, inherited from server object if set        db.hostInfo() get details about the server's host        db.isMaster() check replica primary status        db.killOp(opid) kills the current operation in the db        db.listCommands() lists all the db commands        db.loadServerScripts() loads all the scripts in db.system.js        db.logout()        db.printCollectionStats()        db.printReplicationInfo()        db.printShardingStatus()        db.printSlaveReplicationInfo()        db.dropUser(username)        db.repairDatabase()        db.resetError()        db.runCommand(cmdObj) run a database command.  if cmdObj is a string, turns it into { cmdObj : 1 }        db.serverStatus()        db.setLogLevel(level,<component>)        db.setProfilingLevel(level,<slowms>) 0=off 1=slow 2=all        db.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the db        db.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the db        db.setVerboseShell(flag) display extra information in shell output        db.shutdownServer()        db.stats()        db.version() current version of the server    ------------使用createUser----------    > db.createUser({user:'root',pwd:'root',roles:['userAdminAnyDatabase']})Successfully added user: { "user" : "root", "roles" : [ "userAdminAnyDatabase" ] }>

卸载服务命令

mongod --dbpath=xx --logpath=xx --remove

安装服务设置权限

mongod --dbpath=xx --logpath=xx --install --auth

登录、退出命令

db.auth(user,pwd)db.logout

创建一个数据库新用户用db.createUser()方法,如果用户存在则返回一个用户重复错误。

  • 语法:
db.createUser(user, writeConcern)    user这个文档创建关于用户的身份认证和访问信息;    writeConcern这个文档描述保证MongoDB提供写操作的成功报告。· user文档,定义了用户的以下形式:{ user: "<name>",  pwd: "<cleartext password>",  customData: { <any information> },  roles: [    { role: "<role>", db: "<database>" } | "<role>",    ...  ]}

user文档字段介绍:
user字段,为新用户的名字;
pwd字段,用户的密码;
cusomData字段,为任意内容,例如可以为用户全名介绍;
roles字段,指定用户的角色,可以用一个空数组给新用户设定空角色;
在roles字段,可以指定内置角色和用户定义的角色。

Built-In Roles(内置角色):1. 数据库用户角色:read、readWrite;2. 数据库管理角色:dbAdmin、dbOwner、userAdmin;3. 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;4. 备份恢复角色:backup、restore;5. 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase6. 超级用户角色:root  // 这里还有几个角色间接或直接提供了系统超级用户的访问(dbOwner 、userAdmin、userAdminAnyDatabase)7. 内部角色:__systemPS:关于每个角色所拥有的操作权限可以点击上面的内置角色链接查看详情。

· writeConcern文档(官方说明)
w选项:允许的值分别是 1、0、大于1的值、”majority”、;
j选项:确保mongod实例写数据到磁盘上的journal(日志),这可以确保mongd以外关闭不会丢失数据。设置true启用。
wtimeout:指定一个时间限制,以毫秒为单位。wtimeout只适用于w值大于1。

例如:在products数据库创建用户accountAdmin01,并给该用户admin数据库上clusterAdmin和readAnyDatabase的角色,products数据库上readWrite角色。use productsdb.createUser( { "user" : "accountAdmin01",                 "pwd": "cleartext password",                 "customData" : { employeeId: 12345 },                 "roles" : [ { role: "clusterAdmin", db: "admin" },                             { role: "readAnyDatabase", db: "admin" },                             "readWrite"                             ] },               { w: "majority" , wtimeout: 5000 } )验证:mongo -u accountAdmin01 -p yourpassward --authenticationDatabase products
原创粉丝点击