设置管理用户
[root@localhost ~]# mongo
MongoDB shell version: 2.4.14
connecting to: test
> use admin
switched to db admin
> db.addUser("admin","123456")
{
"user" : "admin",
"readOnly" : false,
"pwd" : "95ec4261124ba5951720b199908d892b",
"_id" : ObjectId("5764ab3203036a2746eb64de")
}
> exit
bye
[root@localhost ~]#
启用安全认证并重启服务
[root@localhost ~]# vi /etc/mongodb.conf
# Turn on/off security. Off is currently the default
#noauth = true
#auth = true
auth = true
[root@localhost ~]# service mongod restart
Stopping mongod: [ OK ]
Starting mongod: [ OK ]
[root@localhost ~]#
登录管理数据库并认证
> use admin
switched to db admin
> db.auth("admin","123456")
1
>
> use linuxcache
switched to db linuxcache
> db.addUser("harvey","12345678")
{
"user" : "harvey",
"readOnly" : false,
"pwd" : "a3e0f380ad93c10d8e2561cb0442bc4f",
"_id" : ObjectId("5764aceb2b5adef8e142afc4")
}
>
查看当前使用数据库
> db
linuxcache
>
查看当前数据库用户
> show users
{
"_id" : ObjectId("5764aceb2b5adef8e142afc4"),
"user" : "harvey",
"readOnly" : false,
"pwd" : "a3e0f380ad93c10d8e2561cb0442bc4f"
}
>
添加一个只读用户
> use linuxcache
switched to db linuxcache
> db.addUser("tom","123","true")
{
"user" : "tom",
"readOnly" : "true",
"pwd" : "4441076092a2c8a4171c33ac937a4861",
"_id" : ObjectId("5764add92b5adef8e142afc5")
}
>
> show users
{
"_id" : ObjectId("5764aceb2b5adef8e142afc4"),
"pwd" : "5021efc96881624a47ab89a0bc1fde9b",
"readOnly" : false,
"user" : "harvey"
}
{
"_id" : ObjectId("5764add92b5adef8e142afc5"),
"user" : "tom",
"readOnly" : "true",
"pwd" : "4441076092a2c8a4171c33ac937a4861"
}
>
修改用户密码
> use linuxcache
switched to db linuxcache
> db.auth("harvey","12345678")
1
> db.changeUserPassword("harvey","87654321")
>