MongoDB和Java(7):MongoDB用户管理
最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装、客户端操作、安全认证、副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很大。特此记录,以备查看。
文章目录:
MongoDB和Java(1):Linux下的MongoDB安装
MongoDB和Java(2):普通用户启动mongod进程
MongoDB和Java(4):Spring Data整合MongoDB(XML配置)
MongoDB和Java(5):Spring Data整合MongoDB(注解配置)
MongoDB和Java(6):Spring Data整合MongoDB副本集、分片集群
本文记录如何开启MongoDB认证、添加用户
1、MongoDB权限管理简单说明
MongoDB中的每个数据库有一些用户(得创建),这些用户有的只能操作自己所属库的表,有的可以操作其他库的表,这取决于它拥有的角色。
一个用户可以有多个角色,角色包含若干权限,权限又拥有资源、操作。
简单来说就是 用户 — 角色 — 权限 的权限管理体系。
关于角色、权限、资源等官网有文档:
https://docs.mongodb.com/manual/reference/built-in-roles/
https://docs.mongodb.com/manual/reference/resource-document/
https://docs.mongodb.com/manual/reference/privilege-actions/
权限资源就不做详细介绍了,因为创建用户使用内置角色就足够了,不太可能去自己去创建角色
看一下内置角色
Database User Roles
read、readWrite
Database Administration Roles
dbAdmin、dbOwner、userAdmin,其中dbOwner权限最高
Cluster Administration Roles
clusterAdmin、clusterManager、clusterMonitor、hostManager
Backup and Restoration Roles
backup、restore
All-Database Roles
readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
Superuser Roles
root
2、创建超级管理员
[root@xugf-test4 ~]# mongo
> use admin
switched to db admin
> db.createUser({
... user: "admin",
... pwd: "",
... roles: [{role: "root", db: "admin"}],
... mechanisms: ["SCRAM-SHA-1"]
... })
Successfully added user: {
"user" : "admin",
"roles" : [
{
"role" : "root",
"db" : "admin"
}
],
"mechanisms" : [
"SCRAM-SHA-1"
]
}
修改mongo.conf配置文件,开启权限认证功能,auth属性设置true
[mongo@xugf-test4 ~]$ cat /etc/mongo.conf
dbpath=/data/mongo/db/
logpath=/data/mongo/log/mongodb.log
bind_ip_all=true
fork=true
auth=true
重启mongodb
3、身份认证的两种方式
再使用mongo连接,进行操作时会提示未认证
[root@xugf-test4 ~]# mongo
> show dbs
--12T10::00.683+ E QUERY [js] Error: listDatabases failed:{
"ok" : ,
"errmsg" : "command listDatabases requires authentication",
"code" : ,
"codeName" : "Unauthorized"
} :
_getErrorWithCode@src/mongo/shell/utils.js::
Mongo.prototype.getDBs@src/mongo/shell/mongo.js::
shellHelper.show@src/mongo/shell/utils.js::
shellHelper@src/mongo/shell/utils.js::
@(shellhelp2)::
此时,有两种方式进行客户端认证:
1)在连接时使用--authenticationDatabase选项指定认证数据库,使用-u选项指定用户名,使用-p指定密码
[root@xugf-test4 ~]# mongo -u admin -p --authenticationDatabase admin
MongoDB shell version v4.0.2
Enter password:
MongoDB server version: 4.0.
> show dbs
admin .000GB
config .000GB
local .000GB
2)在连接后切换到认证数据库后,使用db.auth("username", "password")进行认证
[root@xugf-test4 ~]# mongo
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.
> use admin
switched to db admin
> db.auth("admin", "") > show dbs
admin .000GB
config .000GB
local .000GB
4、添加数据库管理员
给test库添加一个数据库管理员testAdmin
> use test
switched to db test
> db.createUser({
... user: "testAdmin",
... pwd: "",
... roles: [{role: "dbOwner", db: "test"}],
... mechanisms: ["SCRAM-SHA-1"]
... })
Successfully added user: {
"user" : "testAdmin",
"roles" : [
{
"role" : "dbOwner",
"db" : "test"
}
],
"mechanisms" : [
"SCRAM-SHA-1"
]
}
使用testAdmin连接
[root@xugf-test4 ~]# mongo -u testAdmin -p --authenticationDatabase test
> db
test
> db.getUsers()
[
{
"_id" : "test.testAdmin",
"user" : "testAdmin",
"db" : "test",
"roles" : [
{
"role" : "dbOwner",
"db" : "test"
}
],
"mechanisms" : [
"SCRAM-SHA-1"
]
}
]
5、添加数据库读写用户
给test库添加一个xugf用户
> use test
switched to db test
> db.createUser({
... user: "xugf",
... pwd: "",
... roles: [{role: "readWrite", db: "test"}],
... mechanisms: ["SCRAM-SHA-1"]
... })
Successfully added user: {
"user" : "xugf",
"roles" : [
{
"role" : "readWrite",
"db" : "test"
}
],
"mechanisms" : [
"SCRAM-SHA-1"
]
}
使用xugf连接
[root@xugf-test4 ~]# mongo -u xugf -p --authenticationDatabase test
MongoDB shell version v4.0.2
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.
>
> db
test
最新文章
- Spring MVC+FreeMarker简介
- Python模块和包
- Java设计模式6:策略模式
- 简单二维元胞自动机 MATLAB实现
- eclipse文本域内只能输入繁体中文
- 【BZOJ】1108: [POI2007]天然气管道Gaz
- Codeforces Round #291 (Div. 2)
- 如何修改 UINavigationController、UINavigationBar 中 navigationItem 左侧 “返回” 按钮的名称
- 并查集专辑 (poj1182食物链,hdu3038, poj1733, poj1984, zoj3261)
- 那些年我们一起改过的bug
- Ubuntu与Centos在登陆安全方面的比较
- easyui中对数据的判断来显示,formatter控制
- 11 vs2015 连接oracle 11g 数据库及相关问题
- cx_Oracle读取Oracle数据库中文乱码问题解决
- J - Joseph and Tests Gym - 102020J (二分+线段树)
- ThinkPHP框架学习(二)
- 【并查集】BZOJ4551-[Tjoi2016&;Heoi2016]树
- pandas2
- Greenplum query Oracle via DLINK
- navicat连接PostgreSQL报:column “rolcatupdate” does not exist ...错误的解决办法