mongodb密码和传统数据如mysql等有些区别:
- mongodb的用户名和密码是基于特定数据库的,而不是基于整个系统的。所有所有数据库db都需要设置密码
mongodb设置管理用户和密码:
show dbs
在mongodb新版本里并没有admin数据库,但是并不妨碍第2步操作。use admin进入admin数据库创建管理员账户
db.createUser({ user: "useradmin", pwd: "adminpassword", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
mongodb中的用户是基于身份role的,该管理员账户的 role是 userAdminAnyDatabase。 ‘userAdmin’代表用户管理身份,’AnyDatabase’ 代表可以管理任何数据库。验证第3步用户添加是否成功
db.auth("useradmin", "adminpassword")如果返回1,则表示成功。exit退出系统db.auth()方法理解为 用户的验证功能修改配置
1 | sudo vi /etc/mongod.conf |
找到
1 | #security: |
取消注释,修改为:
security:authorization: enabled #注意缩进,缩进参照配置文件其他配置。缩进错误可能第6步重启不成功。
重启mongodb
sudo service mongod restart进入mongodb,用第3步的 管理员账户登录,用该账户创建其他数据库管理员账号
use admindb.auth("useradmin", "adminpassword")
新建你需要管理的mongodb 数据的账号密码。
#必须要use否则创建的不生效,连接也不生效,否则连接的时候只能用admin的数据库去连rotbo3T,但也可以看到自己的Database,也可以操作数据库
use yourdatabasedb.createUser({ user: "youruser", pwd: "yourpassword", roles: [{ role: "dbOwner", db: "yourdatabase" }] })
rote:dbOwner 代表数据库所有者角色,拥有最高该数据库最高权限。比如新建索引等
新建数据库读写账户
use yourdatabasedb.createUser({ user: "youruser2", pwd: "yourpassword2", roles: [{ role: "readWrite", db: "yourdatabase" }] })该用户用于该数据的读写,只拥有读写权限。
现在数据的用户名和密码就建好了。
可以使用:mongodb://youruser2:yourpassword2@localhost/yourdatabase来链接验证
ps aux|grep mongo1
2
3
4
5mongod 3609 0.2 8.6 1087064 87484 ? Sl 13:32 0:30 /usr/bin/mongod -f /etc/mongod.conf
root 4150 0.0 0.0 112724 988 pts/0 R+ 17:10 0:00 grep --color=auto mongo
没起服务之前
root 3574 0.0 0.0 112724 988 pts/0 R+ 13:28 0:00 grep --color=auto mongo
验证安全认证:
use admin
witched to db admin
show dbs –没有认证查看数据库报错
014-09-14T13:28:45.953+0800 listDatabases failed:{
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases:
.0 }”,
“code” : 13
at src/mongo/shell/mongo.js:47db.auth(“super”,”super”) —认证后再次查看ok
1
show dbs
dmin 0.078GB
ocal 0.078GB
est 0.078GB
angwei 0.078GB普通用户认证
show dbs –没有认证查看数据
014-09-14T13:31:19.265+0800 listDatabases failed:{
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases:
.0 }”,
“code” : 13
at src/mongo/shell/mongo.js:47
db.auth(“test”,”test”)
1
show dbs –认证后查看数据库还报错,原因这个用户属于test不属于admin
014-09-14T13:33:30.062+0800 listDatabases failed:{
“ok” : 0,
“errmsg” : “not authorized on admin to execute command { listDatabases:
.0 }”,
“code” : 13
at src/mongo/shell/mongo.js:47
E:\mongodb\bin>mongo 127.0.0.1:27019
MongoDB shell version: 2.6.4
connecting to: 127.0.0.1:27019/test
db.mycol.insert({“id”:222}) –没有认证情况插入文档失败
WriteResult({
“writeError” : {
“code” : 13,
“errmsg” : “not authorized on test to execute command { insert:
“mycol\”, documents: [ { _id: ObjectId(‘5415292f131751676caa7881’), id: 222.0 }
], ordered: true }”
}
})
db.auth(“test”,”test”) –认证后插入文档成功
1
db.mycol.insert({“id”:222})
riteResult({ “nInserted” : 1 })
只读用户认证
E:\mongodb\bin>mongo 127.0.0.1:27019
MongoDB shell version: 2.6.4
connecting to: 127.0.0.1:27019/test
db.mycol.find() –没有认证查询失败
rror: { “$err” : “not authorized for query on test.mycol”, “code” : 13 }
db.auth(“readonly”.”readonly”)
014-09-14T13:38:16.265+0800 SyntaxError: Unexpected string
db.auth(“readonly”,”readonly”)
1
db.mycol.find() –认证后查询成功
“_id” : ObjectId(“5415294b131751676caa7882”), “id” : 222 }db.mycol.insert({“id”:5555}) –只读认证后,插入文档失败,原因用户是只读的
WriteResult({
“writeError” : {
“code” : 13,
“errmsg” : “not authorized on test to execute command { insert:
“mycol\”, documents: [ { _id: ObjectId(‘541529ead090e8f5c50762b9’), id: 5555.0
], ordered: true }”
}
})