最近花了一些时间学习了下MongoDB数据库,感觉还是比较全面系统的,涉及了软件安装、客户端操作、安全认证、副本集和分布式集群搭建,以及使用Spring Data连接MongoDB进行数据操作,收获很大。特此记录,以备查看。
文章目录:
本文记录如何开启MongoDB认证、添加用户
1、MongoDB权限管理简单说明
MongoDB中的每个数据库有一些用户(得创建),这些用户有的只能操作自己所属库的表,有的可以操作其他库的表,这取决于它拥有的角色。
一个用户可以有多个角色,角色包含若干权限,权限又拥有资源、操作。
简单来说就是 用户 — 角色 — 权限 的权限管理体系。
权限资源就不做详细介绍了,因为创建用户使用内置角色就足够了,不太可能去自己去创建角色
看一下内置角色
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、创建超级管理员
1 [root@xugf-test4 ~]# mongo2 >use admin3 switched to db admin4 >db.createUser({5 ... user: "admin",6 ... pwd: "123456",7 ... roles: [{role: "root", db: "admin"}],8 ... mechanisms: ["SCRAM-SHA-1"]9 ... })10 Successfully added user: {11 "user" : "admin",12 "roles": [13 {14 "role" : "root",15 "db" : "admin"
16 }17 ],18 "mechanisms": [19 "SCRAM-SHA-1"
20 ]21 }
修改mongo.conf配置文件,开启权限认证功能,auth属性设置true
1 [mongo@xugf-test4 ~]$ cat /etc/mongo.conf2 dbpath=/data/mongo/db/
3 logpath=/data/mongo/log/mongodb.log4 bind_ip_all=true
5 fork=true
6 auth=true
重启mongodb
3、身份认证的两种方式
再使用mongo连接,进行操作时会提示未认证
1 [root@xugf-test4 ~]# mongo2 >show dbs3 2018-10-12T10:16:00.683+0800E QUERY [js] Error: listDatabases failed:{4 "ok" : 0,5 "errmsg" : "command listDatabases requires authentication",6 "code" : 13,7 "codeName" : "Unauthorized"
8 } :9 _getErrorWithCode@src/mongo/shell/utils.js:25:13
10 Mongo.prototype.getDBs@src/mongo/shell/mongo.js:67:1
11 shellHelper.show@src/mongo/shell/utils.js:876:19
12 shellHelper@src/mongo/shell/utils.js:766:15
13 @(shellhelp2):1:1
此时,有两种方式进行客户端认证:
1)在连接时使用--authenticationDatabase选项指定认证数据库,使用-u选项指定用户名,使用-p指定密码
1 [root@xugf-test4 ~]# mongo -u admin -p --authenticationDatabase admin2 MongoDB shell version v4.0.2
3 Enter password:4 MongoDB server version: 4.0.2
5 >show dbs6 admin 0.000GB7 config 0.000GB8 local 0.000GB
2)在连接后切换到认证数据库后,使用db.auth("username", "password")进行认证
1 [root@xugf-test4 ~]# mongo2 MongoDB shell version v4.0.2
3 connecting to: mongodb://127.0.0.1:27017
4 MongoDB server version: 4.0.2
5 >use admin6 switched to db admin7 > db.auth("admin", "123456")8 1
9 >show dbs10 admin 0.000GB11 config 0.000GB12 local 0.000GB
4、添加数据库管理员
给test库添加一个数据库管理员testAdmin
1 >use test2 switched to db test3 >db.createUser({4 ... user: "testAdmin",5 ... pwd: "123456",6 ... roles: [{role: "dbOwner", db: "test"}],7 ... mechanisms: ["SCRAM-SHA-1"]8 ... })9 Successfully added user: {10 "user" : "testAdmin",11 "roles": [12 {13 "role" : "dbOwner",14 "db" : "test"
15 }16 ],17 "mechanisms": [18 "SCRAM-SHA-1"
19 ]20 }
使用testAdmin连接
1 [root@xugf-test4 ~]# mongo -u testAdmin -p --authenticationDatabase test2 >db3 test4 >db.getUsers()5 [6 {7 "_id" : "test.testAdmin",8 "user" : "testAdmin",9 "db" : "test",10 "roles": [11 {12 "role" : "dbOwner",13 "db" : "test"
14 }15 ],16 "mechanisms": [17 "SCRAM-SHA-1"
18 ]19 }20 ]
5、添加数据库读写用户
给test库添加一个xugf用户
1 >use test2 switched to db test3 >db.createUser({4 ... user: "xugf",5 ... pwd: "123456",6 ... roles: [{role: "readWrite", db: "test"}],7 ... mechanisms: ["SCRAM-SHA-1"]8 ... })9 Successfully added user: {10 "user" : "xugf",11 "roles": [12 {13 "role" : "readWrite",14 "db" : "test"
15 }16 ],17 "mechanisms": [18 "SCRAM-SHA-1"
19 ]20 }
使用xugf连接
1 [root@xugf-test4 ~]# mongo -u xugf -p 123456 --authenticationDatabase test2 MongoDB shell version v4.0.2
3 connecting to: mongodb://127.0.0.1:27017
4 MongoDB server version: 4.0.2
5 >
6 >db7 test