本篇文章内容主要是mongodb添加用户密码,以及添加后一些管理相关的操作变化。
创建用户
mongo localhost:27017 #连接数据库 show dbs #显示数据库 use admin #切换到admin数据库 db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]}) use testdb #切换到testdb数据库 db.createUser({user:"testUser",pwd:"123456",roles:[{role:"readWrite",db:"testdb"}]}) db.auth("testUser","123456") 1 #验证账户是否创建成功
role是指该用户的角色,可以理解为有哪些权限,常用加粗如下:
- 数据库用户角色:read、readWrite;
- 数据库管理角色:dbAdmin、dbOwner、userAdmin;
- 集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
- 备份恢复角色:backup、restore;
- 所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
- 超级用户角色:root (只能在admin数据库创建)
- 内部角色:__system
使用访问控制重新启动数据库
开启访问控制有两种方式,一种是–auth参数重启mongod服务。
mongod --dbpath 存放数据库文件夹路径 --auth
另一种是修改数据库安装目录下配置文件config.conf
security: authorization: enabled
然后重新启动mongodb服务。
使用账户密码登录数据库
mongo localhost:27017/testdb -u testUser -p 123456
使用账户密码备份数据库
mongodump /h localhost:27017 /u testUser /p 123456 /d testdb /o "C:\backup"
账户操作相关的命令
db.getUser("testUser") #展示用户信息 db.changeUserPassword("render","lzy123456",) #修改用户密码 db.dropUser("render",{w:"majority",wtimeout:5000}) #删除用户
涉及到账户操作的命令必须在关闭访问控制的情况下登录数据库才能修改。
mongodb的认证方式
mongodb中有两种认证方式,分别是 MONGODB-CR 和 (SCRAM)SHA-1 。
其中 MONGODB-CR 比较旧,在新版mongodb中默认都是 (SCRAM)SHA-1 方式,那么假如因为兼容性等原因导致必须选择 MONGODB-CR 方式,那么下面提供一种方法。
db.dropUser("testUser",{w:"majority",wtimeout:5000}) #删除用户 use admin #使用admin数据库 db.system.version.update({'_id':'authSchema'},{$set:{'currentVersion':3}}) #修改system.version文档里面的authSchema版本为3(旧版本) db.system.version.find() #查看是否修改成功 { "_id" : "authSchema", "currentVersion" : 3 } use testdb #回到testdb数据库 db.createUser({user:'testUser',pwd:'123456',roles:[{role:'dbOwner',db:'testdb'}]}) #重新创建用户 db.getUser("testUser",{showCredentials:true}) #查看认证方式
另外说一下关于deadline如何使用加密后的数据库,参考这里。
主要就是数据库配置文件 connection.ini 需要修改几个参数。
Authenticate=True Username=testUser
还有需要注意的文档中有一处与实际不符,是数据库配置文件connection.ini中并没有Password这一参数,取而代之的是PasswordHash。
这样其实是好事,避免密码直接写在配置文件里面被人轻松获取。
值得说明的是,这个 PasswordHash 不是拿密码找网站工具转换填写的,也和mongodb中db.getUser(“render”,{showCredentials:true})命令展示出的hash password无关,而是deadline根据输入的密码进行加密存储的。
正确的填写姿势如下:
deadlinecommand UpdateDatabaseSettings C:\DeadlineRepository10 MongoDB 10.10.10.10 deadline10db 27017 -1 False True testUser 123456 "" False
CMD中运行完后,就能看到connection.ini文件中已经正确转换和填写完成Passwordhash等参数。
再放个win平台的数据库可视化工具robomongo。
真爱的第一个征兆,
在男孩身上是胆怯,
在女孩身上是大胆。
《悲惨世界》
——雨果


评论
889518 531049Hello! I would wish to supply a large thumbs up for your excellent info you could have here about this post. Ill be coming back to your weblog internet site for further soon. 260390
394406 455738When I initially commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get several e-mails with the same comment. Is there any way you can remove me from that service? Thanks! 9033
461157 71893Nice weblog! Only dilemma is im running Firefox on Debian, and the website is looking slightly.. weird! Perhaps you could want to test it to see for yourself. 979987
825199 484614This sort of wanting to come to a difference in her or his lifestyle, initial typically Los angeles Excess weight weightloss scheme is really a large running in as it reached that strive. weight loss 182192
998616 24491Top rated lad speeches and toasts, as nicely toasts. may possibly very nicely be supplied taken into consideration generating at the party consequently required to be slightly much more cheeky, humorous with instructive on top of this. finest man speeches funny 44539
224344 217649dress shops that offer discounts are extremely common in our location and i always shop at them,. 511064