赞
踩
MongoDB 服务默认是没有账号密码的,客户端连接上即可进行各种操作。如果在配置文件中,指定了auth=true
,即开启了认证,那么客户端连接后需要认证才能执行操作。
默认情况下,MongoDB 是没有管理员账户的,所以我们需要在安装好 MongoDB之后,在admin
数据库中使用db.createUser()
命令添加管理员帐号或其他角色账号。
MongoDB 有一些内置的角色,如下:
创建用户的语法如下:
// 创建一个用户,在指定的数据库有指定的角色权限,其中自定义信息属性是可选的
db.createUser({
user:"用户名",
pwd:"密码",
roles:[{role:"角色",db:"数据库"},...],
customData: {自定义信息}
})
首先创建一个管理员用户,赋予userAdminAnyDatabase
角色,此角色可以在任何数据库下拥有userAdmin
权限,即可以在任何数据库下管理用户。注意,只能在 admin 数据库中创建此角色用户。
> use admin
switched to db admin
> db.createUser({user:"admin",pwd:"123456",roles:[{role:"userAdminAnyDatabase",db:"admin"}]})
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
}
>
然后使用db.auth("用户名","密码")
进行认证。注意,每一个用户都需要在创建这个用户的认证库下进行认证。
> use admin
switched to db admin
> db.auth('admin','123456')
这时,我们就可以使用 admin 账号管理(创建,删除等)其他用户了。
如下,创建一个用户 chenpi,在数据库 chenpidb 上有读写权限。这也是我们一般为不同业务创建的专属用户。
> use chenpidb
switched to db chenpidb
> db.createUser({user:"chenpi",pwd:"123456",roles:[{role:"readWrite",db:"chenpidb"}]})
Successfully added user: {
"user" : "chenpi",
"roles" : [
{
"role" : "readWrite",
"db" : "chenpidb"
}
]
}
使用 chenpi 用户连接 MongoDB,切换到认证库下进行认证,只有进行证成功之后才可以进行操作。
[root@chenpihost ~]# mongo MongoDB shell version v4.2.17 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("91153167-b4ab-4078-ba6d-44a6f98492e3") } MongoDB server version: 4.2.17 > use chenpidb switched to db chenpidb > show tables Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus > db.person.insert({"name":"ChenPi"}) WriteCommandError({ "ok" : 0, "errmsg" : "command insert requires authentication", "code" : 13, "codeName" : "Unauthorized" }) > db.auth('chenpi','123456') 1 > db.person.insert({"name":"ChenPi"}) WriteResult({ "nInserted" : 1 }) >
注意,chenpi 用户只有在 chenpidb 数据库下的读写操作,对其他数据库没有权限。
> use chenpidb1
switched to db chenpidb1
> show tables
Warning: unable to run listCollections, attempting to approximate collection names by parsing connectionStatus
> db.person.insert({"name":"ChenPi"})
WriteCommandError({
"ok" : 0,
"errmsg" : "not authorized on chenpidb1 to execute command { insert: \"person\", ordered: true, lsid: { id: UUID(\"91153167-b4ab-4078-ba6d-44a6f98492e3\") }, $db: \"chenpidb1\" }",
"code" : 13,
"codeName" : "Unauthorized"
})
>
上面我们创建的 admin 用户只有管理用户的权限,它没有进行数据库操作的权限,如下所示:
> db.person.insert({"name":"ChenPi"})
WriteCommandError({
"ok" : 0,
"errmsg" : "not authorized on admin to execute command { insert: \"person\", ordered: true, lsid: { id: UUID(\"f3ba4ad3-b89e-447f-85ca-255c48c97355\") }, $db: \"admin\" }",
"code" : 13,
"codeName" : "Unauthorized"
})
>
如果一个用户需要有管理用户的权限,还需要用操作数据库的权限,例如插入表数据,创建索引等,可以创建 root 角色用户,它是超级用户,拥有所有权限。
> use admin
switched to db admin
> db.createUser({user:"root",pwd:"123456",roles:[{role:"root",db:"admin"}]})
Successfully added user: {
"user" : "root",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
>
使用 root 用户登录,就可以进行任何操作了,如下所示:
> use admin switched to db admin > db.auth('root','123456') 1 > db.system.users.findOne({}) { "_id" : "admin.admin", "userId" : UUID("7c4292ad-3a9a-4591-9ad5-53e56c72f725"), "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "1m0m35AVNStekFqyXTPXEQ==", "storedKey" : "PbxbrSfXpM1mEOTY4OyjvhoHSF8=", "serverKey" : "HaIm8KBGJZ+92JYJTRoDwNFzQ20=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "AHAYN8l4TrFOg9RzkReUMHA/WkyWpHN6qejcjw==", "storedKey" : "/IaLQADTe/unVKrpKuZ3lgMmIJzqHlK8H/s4pTLSuQo=", "serverKey" : "OBx9qg/JwSAPwXLgzKygq/T6+2qg7Yzxml6VlQCEudQ=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } > use db1 switched to db db1 > db.person.insert({"name":"ChenPi"}) WriteResult({ "nInserted" : 1 })
我们创建用户时,可以添加一些自定义信息,用于说明此用户,如下:
db.createUser({
user:"chenpiadmin1",
pwd:"123456",
roles:[{role:"userAdminAnyDatabase",db:"admin"}],
customData: {"name":"陈皮","age":18},
aoligei: {"name":"陈皮","age":18}
})
这样,我们查看用户信息时,会展示这些自定义的信息,如下:
> db.system.users.find({"user":"chenpiadmin"}).pretty() { "_id" : "admin.chenpiadmin", "userId" : UUID("7d83c9db-d696-4ebe-843c-1ccbe42c984f"), "user" : "chenpiadmin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Gv65j7XoE6KAvSlSjGNGuQ==", "storedKey" : "4rTKkwQG7uRgmCgrB/KXgf4NgT8=", "serverKey" : "YzQ41ABpWTdX/ZKNjCT94NxdSwo=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "J4DonJjsKkKvxULZcYNZisAOLCQAcCQ10xjTBg==", "storedKey" : "uCr6S2NNnLxxz6RKmpFwDwSRLK4tQ7waqkH4fhoPXzo=", "serverKey" : "OAokzaylYh/SdwRAgDVS7PLkulZ/OmH8PAlhIyZKp2A=" } }, "customData" : { "name" : "陈皮", "age" : 18 }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } >
每一个用户都是在特定库(认证库)下创建的,我们可以使用拥有 userAdmin 权限的用户,查看不同数据库下的用户列表。
可以使用show users
命令查看。例如,查看 admin 数据库下的用户列表。
> use admin switched to db admin > show users { "_id" : "admin.admin", "userId" : UUID("7c4292ad-3a9a-4591-9ad5-53e56c72f725"), "user" : "admin", "db" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } { "_id" : "admin.chenpiadmin", "userId" : UUID("7d83c9db-d696-4ebe-843c-1ccbe42c984f"), "user" : "chenpiadmin", "db" : "admin", "customData" : { "name" : "陈皮", "age" : 18 }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }
切换到 chenpidb 数据库,查看此数据库下的用户列表。
> use chenpidb switched to db chenpidb > show users { "_id" : "chenpidb.chenpi", "userId" : UUID("2b80f57b-031d-4621-aa72-0895972302ca"), "user" : "chenpi", "db" : "chenpidb", "roles" : [ { "role" : "readWrite", "db" : "chenpidb" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] }
在 admin 数据库下,有个system.users
表,此记录着 MongoDB 所有用户信息,如下:
> use admin switched to db admin > show tables system.users system.version > db.system.users.find({}).pretty() { "_id" : "admin.admin", "userId" : UUID("7c4292ad-3a9a-4591-9ad5-53e56c72f725"), "user" : "admin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "1m0m35AVNStekFqyXTPXEQ==", "storedKey" : "PbxbrSfXpM1mEOTY4OyjvhoHSF8=", "serverKey" : "HaIm8KBGJZ+92JYJTRoDwNFzQ20=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "AHAYN8l4TrFOg9RzkReUMHA/WkyWpHN6qejcjw==", "storedKey" : "/IaLQADTe/unVKrpKuZ3lgMmIJzqHlK8H/s4pTLSuQo=", "serverKey" : "OBx9qg/JwSAPwXLgzKygq/T6+2qg7Yzxml6VlQCEudQ=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } { "_id" : "chenpidb.chenpi", "userId" : UUID("2b80f57b-031d-4621-aa72-0895972302ca"), "user" : "chenpi", "db" : "chenpidb", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "MijUlGFqsm85lYz9gktVYA==", "storedKey" : "WVXWAcLnNsjT1gVIt4s7MoNBdSo=", "serverKey" : "qUdJDl+XQJMKu72KH0MN3IUrlrk=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "ms06p++btiQEdRkshyQd5WRZ3wNmPUiHG/ikag==", "storedKey" : "D6JNgxrjK1SFvGUP+BZ4gk8I9f4u/cZ3mubpTRM4BBA=", "serverKey" : "IydgVAgPN8x0lhws3HmXQQoIuFyyF+3HolCv7lBUP8U=" } }, "roles" : [ { "role" : "readWrite", "db" : "chenpidb" } ] } { "_id" : "admin.chenpiadmin", "userId" : UUID("7d83c9db-d696-4ebe-843c-1ccbe42c984f"), "user" : "chenpiadmin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "Gv65j7XoE6KAvSlSjGNGuQ==", "storedKey" : "4rTKkwQG7uRgmCgrB/KXgf4NgT8=", "serverKey" : "YzQ41ABpWTdX/ZKNjCT94NxdSwo=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "J4DonJjsKkKvxULZcYNZisAOLCQAcCQ10xjTBg==", "storedKey" : "uCr6S2NNnLxxz6RKmpFwDwSRLK4tQ7waqkH4fhoPXzo=", "serverKey" : "OAokzaylYh/SdwRAgDVS7PLkulZ/OmH8PAlhIyZKp2A=" } }, "customData" : { "name" : "陈皮", "age" : 18 }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] } >
更新用户角色语法如下。首先登录具有 userAdmin 或 userAdminAnyDatabase 或 root 角色权限的账号,然后切换到被修改用户的认证库下进行操作。
db.updateUser("用户名",{roles:[{角色信息}]})
我们可以对已经存在的用户,更新它的角色信息,例如对 chenpi 用户再添加对 chenpidb 数据库的 dbAdmin 权限。
> use chenpidb switched to db chenpidb > show users { "_id" : "chenpidb.chenpi", "userId" : UUID("2b80f57b-031d-4621-aa72-0895972302ca"), "user" : "chenpi", "db" : "chenpidb", "roles" : [ { "role" : "readWrite", "db" : "chenpidb" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } > db.updateUser("chenpi",{roles:[{role:"readWrite",db:"chenpidb"},{role:"dbAdmin",db:"chenpidb"}]}) > show users { "_id" : "chenpidb.chenpi", "userId" : UUID("2b80f57b-031d-4621-aa72-0895972302ca"), "user" : "chenpi", "db" : "chenpidb", "roles" : [ { "role" : "readWrite", "db" : "chenpidb" }, { "role" : "dbAdmin", "db" : "chenpidb" } ], "mechanisms" : [ "SCRAM-SHA-1", "SCRAM-SHA-256" ] } >
首先登录具有 userAdmin 或 userAdminAnyDatabase 或 root 角色权限的账号,然后切换到被修改用户的认证库下进行操作。
修改用户密码有2种语法,如下所示。
db.updateUser("用户名",{pwd:"新密码"})
db.changeUserPassword("用户名","新密码")
例如,对 chenpi 用户进行修改密码,如下:
> use chenpidb
switched to db chenpidb
> db.updateUser("chenpi",{pwd:"123456789"})
> db.changeUserPassword('chenpi','123456')
首先登录具有 userAdmin 或 userAdminAnyDatabase 或 root 角色权限的账号,然后切换到被删除用户的认证库下进行操作。
删除用户语法如下:
db.dropUser('用户名')
我们删除 admin 数据库下的 chenpi 用户,如下:
> use admin
switched to db admin
> db.dropUser('chenpi')
true
本次分享到此结束啦~~
如果觉得文章对你有帮助,点赞、收藏、关注、评论,您的支持就是我创作最大的动力!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。