主页 > 新闻资讯 > 大数据培训:MongoDB单实例权限控制

大数据培训:MongoDB单实例权限控制

作者:张老师 浏览次数: 2021-06-28 16:49
在MongoDB的安全管理上,权限控制是非常重要的一个部分,要确保数据安全,对于权限的分级控制,是实际开发场景下需要去解决的需求。今天的大数据培训分享,我们就主要来讲讲,MongoDB单实例权限控制。

大数据培训:MongoDB单实例权限控制

1、添加用户和权限

1)按照普通无授权认证的配置先启动mongoDB

# 原来的配置
[root@mongo ~]# cat /mongodb/conf/mongod.conf
systemLog:
   destination: file
   path: "/mongodb/log/mongod.log"
   logAppend: true
storage:
   dbPath: "/mongodb/data/db"
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: localhost,10.0.0.104
   port: 27017

# 按之前未开启认证的方式(不添加 --auth 参数)来启动MongoDB服务
[root@mongo ~]# /usr/local/mongodb/bin/mongod -f /mongodb/conf/mongod.conf

2)登录mongoDB,创建两个管理员用户,一个是系统的超级管理员 myroot ,一个是admin库的管理用户myadmin

# 登录mongoDB
[root@mongo ~]# /usr/local/mongodb/bin/mongo --host 10.0.0.104 --port 27017

# 切换到admin库
> use admin
switched to db admin

# 创建系统超级用户 myroot,设置密码123456,设置角色root
> db.createUser({user:"myroot",pwd:"123456",roles:["root"]})
Successfully added user: { "user" : "myroot", "roles" : [ "root" ] }

# 创建专门用来管理admin库的账号myadmin,只用来作为用户权限的管理
> db.createUser({user:"myadmin",pwd:"123456",roles: [{role:"userAdminAnyDatabase",db:"admin"}]})
Successfully added user: {
"user" : "myadmin",
"roles" : [
  {
   "role" : "userAdminAnyDatabase",
   "db" : "admin"
  }
]
}

# 查看创建的用户情况
> db.system.users.find()
{ "_id" : "admin.myroot", "userId" : UUID("6847a67e-83f4-4814-8788-fcaf2f762213"), "user" : "myroot", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "WC4RWaRK/X2EPpwTBsWZ/w==", "storedKey" : "GOZ8vYQg5+OyDy6lAxpSDwy4P3E=", "serverKey" : "/JwZiE8wuaUBdTTvt7DKk9xCfO0=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "DoRJ6cUdM6fgBZP2f9Tt4LXYfXG9KZPdud8P/w==", "storedKey" : "6EuV7DL7BUb3Jryhy1/IEa2tea4m2VXyVWZGVL1ixqA=", "serverKey" : "Yxyb075ydXKTNw4n7u1gE2RNySRCSLnUDrDo+AWklYo=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
{ "_id" : "admin.myadmin", "userId" : UUID("b51f3539-1330-45e3-a900-427a7288e59c"), "user" : "myadmin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "hDj8Ut45FrpCtC/FplL6VA==", "storedKey" : "ntc0mhpVMLcACL2ilsXM0ICXcN0=", "serverKey" : "76lqeGWwcpU+xH5pT0EVDYe0fxI=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "E/Z7PRNRC96MPTC0P4FM+zveDk3FzZJVt7DAeQ==", "storedKey" : "xlsvkIBeHbCaoAm+FwacosJ6Qj1/mz+On4NWKe2OTZM=", "serverKey" : "xEDF21/TV6ubfV2Oak5aTNnoCK8FWbsq9oNVf9mJ2SQ=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }

# 删除用户
> db.dropUser("myadmin")
true
> db.system.users.find()
{ "_id" : "admin.myroot", "userId" : UUID("6847a67e-83f4-4814-8788-fcaf2f762213"), "user" : "myroot", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "WC4RWaRK/X2EPpwTBsWZ/w==", "storedKey" : "GOZ8vYQg5+OyDy6lAxpSDwy4P3E=", "serverKey" : "/JwZiE8wuaUBdTTvt7DKk9xCfO0=" }, "SCRAM-SHA-256" : { "iterationCount" : 15000, "salt" : "DoRJ6cUdM6fgBZP2f9Tt4LXYfXG9KZPdud8P/w==", "storedKey" : "6EuV7DL7BUb3Jryhy1/IEa2tea4m2VXyVWZGVL1ixqA=", "serverKey" : "Yxyb075ydXKTNw4n7u1gE2RNySRCSLnUDrDo+AWklYo=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }

# 修改密码
> db.changeUserPassword("myroot", "123456")
注意:

(1)本案例创建了两个用户,分别对应超管和专门用来管理用户的角色,事实上,你只需要一个用户即可。如果你对安全要求很高,防止超管泄漏,则不要创建超管用户。

(2)和其它数据库(MySQL)一样,权限的管理都差不多一样,也是将用户和权限信息保存到数据库对应的表中。Mongodb存储所有的用户信息在admin 数据库的集合system.users中,保存用户名、密码和数据库信息。

(3)如果不指定数据库,则创建的指定的权限的用户在所有的数据库上有效,如 {role: "userAdminAnyDatabase", db:""}

3)认证测试

> use admin
switched to db admin
> db.auth("myroot","12345")
Error: Authentication failed.
0
> db.auth("myroot","123456")
1

4)创建普通用户

创建普通用户可以在没有开启认证的时候添加,也可以在开启认证之后添加,但开启认证之后,必须使用有操作admin库的用户登录认证后才能操作。底层都是将用户信息保存在了admin数据库的集合system.users中。

如果开启了认证后,登录的客户端的用户必须使用admin库的角色,如拥有root角色的myadmin用户,再通过myadmin用户去创建其他角色的用户。

# 创建(切换)将来要操作的数据库articledb
> use articledb
switched to db articledb

# 创建用户,拥有articledb数据库的读写权限readWrite,密码是123456
> db.createUser({user: "lawrence", pwd: "123456", roles: [{ role: "readWrite", db: "articledb" }]})
Successfully added user: {
"user" : "lawrence",
"roles" : [
  {
   "role" : "readWrite",
   "db" : "articledb"
  }
]
}

# 测试是否可用
> db.auth("lawrence","123456")
1

2、服务端开启认证和客户端连接登录

1)关闭启动的服务

# 方式一:杀死进程

[root@mongo ~]# ps -ef|grep mongo
root       6498      1  0 09:48 ?        00:00:18 mongod -f /mongodb/conf/mongod.conf
root      39619   1529  0 10:46 pts/0    00:00:00 grep --color=auto mongo
[root@mongo ~]# kill -2 6498

# 方式二:localhost登录并db.shutdownServer()

> db.shutdownServer()
shutdown command only works with the admin database; try 'use admin'
> use admin
switched to db admin
> db.shutdownServer()
2021-04-02T10:47:02.996+0800 E QUERY    [js] Error: shutdownServer failed: {
"ok" : 0,
"errmsg" : "shutdown must run from localhost when running db without auth",
"code" : 13,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js:25:13
DB.prototype.shutdownServer@src/mongo/shell/db.js:453:1
@(shell):1:1
>
[root@mongo ~]# /usr/local/mongodb/bin/mongo --host 127.0.0.1 --port 27017
> use admin
switched to db admin
> db.shutdownServer()
server should be down...
2021-04-02T10:47:48.479+0800 I NETWORK  [js] trying reconnect to 127.0.0.1:27017 failed
2021-04-02T10:47:48.479+0800 I NETWORK  [js] reconnect 127.0.0.1:27017 failed failed

主要需要几个条件:

必须是在admin库下执行该关闭服务命令。
如果没有开启认证,必须是从localhost登陆的,才能执行关闭服务命令。
非localhost、通过远程登录的,必须有登录且必须登录用户有对admin操作权限才以。

2)开启认证的方式启动服务

有两种方式开启权限认证启动服务:一种是参数方式,一种是配置文件方式。

方式一:参数方式

# 在启动时指定参数 --auth,不推荐
[root@mongo ~]# /usr/local/mongodb/bin/mongod -f /mongodb/conf/mongod.conf --auth
方式二:配置文件方法

# 在配置文件中开启认证
[root@mongo ~]# vim /mongodb/conf/mongod.conf
systemLog:
   destination: file
   path: "/mongodb/log/mongod.log"
   logAppend: true
storage:
   dbPath: "/mongodb/data/db"
   journal:
      enabled: true
processManagement:
   fork: true
net:
   bindIp: localhost,10.0.0.104
   port: 27017
security:
   #开启授权认证
   authorization: enabled
  
# 启动
[root@mongo ~]# /usr/local/mongodb/bin/mongod -f /mongodb/conf/mongod.conf

3)客户端连接

有两种认证方式,一种是先登录,在mongo shell中认证;一种是登录时直接认证。

方式一:在mongoDB shell中认证

# 先登录在mongoDB shell中认证
> use admin
switched to db admin
> db.system.users.find()
Error: error: {
"ok" : 0,
"errmsg" : "command find requires authentication",
"code" : 13,
"codeName" : "Unauthorized"
}
> db.auth("myroot","123456")
1
> db.system.users.find()

# 退出shell,在登录,否者报错:too many users are authenticated
> use articledb
switched to db articledb
> db.comment.find()
Error: error: {
"ok" : 0,
"errmsg" : "command find requires authentication",
"code" : 13,
"codeName" : "Unauthorized"
}
> db.auth("lawrence","123456")
1
> db.comment.find()

方式二:连接时直接认证

# 对admin数据库进行登录认证和相关操作
[root@mongo ~]# /usr/local/mongodb/bin/mongo --host 10.0.0.104 --port 27017 --authenticationDatabase admin -u myroot -p 123456
> show dbs
admin      0.000GB
articledb  0.000GB
config     0.000GB
local      0.000GB
> use admin
switched to db admin
> db.system.users.find()

# 对articledb数据库进行登录认证和相关操作
[root@mongo ~]# /usr/local/mongodb/bin/mongo --host 10.0.0.104 --port 27017 --authenticationDatabase articledb -u lawrence -p 123456
> db
test
> use articledb
switched to db articledb
> db.comment.find()

关于大数据培训,MongoDB单实例权限控制,以上就为大家做了基本的讲解了。MongoDB的权限控制部分,涉及到的体系还是比较复杂,也需要大家多去深入理解和掌握。成都加米谷大数据,专业大数据培训机构,大数据开发、数据分析与挖掘,零基础班本月正在招生中,课程大纲及试学视频,可联系客服获取!
热点排行
推荐文章
立即申请>>