单机环境下的用户授权模块配置
来源:互联网 发布:淘宝商品品牌词库 编辑:程序博客网 时间:2024/05/21 11:16
MongoDB数据库默认情况下是没有做权限控制的,只要能够连接所开放的端口就能进行访问,而且拥有root级别的权限;对于生产环境而言是极不安全的,所以需要建立用户,进行授权控制。
单机环境下的用户授权模块配置:
MongoDB的社区版本中有两个模块可以控制用户的访问:
首先验证下非配置认证模块的访问:
[root@fo169 bin]# ./mongoMongoDB shell version: 3.0.7connecting to: testServer has startup warnings:2015-10-29T15:12:14.257+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-10-29T15:12:14.257+0800 I CONTROL [initandlisten]> show dbslocal 0.000GB
在没有配置的情况下,登录到数据库后,可以做任何操作。
配置认证模块及重启服务:
编写了一个启动配置文件:mongodb.conf(文件中标红部分就为auth的授权模块)
[root@fo169 bin]# cat mongodb.conf systemLog: destination: file path: "/data/auth/log/mongod.log" logAppend: truestorage: journal: enabled: true dbPath: "/data/auth/db" directoryPerDB: true engine: wiredTiger wiredTiger: engineConfig: cacheSizeGB: 4 directoryForIndexes: true journalCompressor: zlib collectionConfig: blockCompressor: zlib indexConfig: prefixCompression: truenet: port: 27017processManagement: fork: truesecurity: authorization: enabled
创建授权用户(超级管理员):
MongoDB在V3.0版本之后内置了root 角色,也就是结合了readWriteAnyDatabase、dbAdminAnyDatabase、userAdminAnyDatabase、clusterAdmin4个角色权限,类似于Oracle的sysdba角色,但是MongoDB的超级管理员用户名称是可以随便定义的:
[root@fo169 bin]# ./mongoMongoDB shell version: 3.0.7connecting to: testServer has startup warnings: 2015-10-30T16:24:36.127+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.2015-10-30T16:24:36.127+0800 I CONTROL [initandlisten] > use adminswitched to db admin> db.createUser(... {... user: "ljaiadmin",... pwd: "123456",... roles: [ { role: "root", db: "admin" } ]... }... )Successfully added user: { "user" : "ljaiadmin", "roles" : [ { "role" : "root", "db" : "admin" } ]}
这样就创建好一个ljaiadmin的超级管理员用户,创建全局用户或者超级用户,需要在MongoDB的admin数据库中创建(在其他库也可以创建,但是没有该角色功能),重启完mongod进程后,接下来做一下权限的验证:
[root@fo169 bin]# ./mongoMongoDB shell version: 3.0.7connecting to: test> show dbs (注:此时查看已提示没有授权执行listDatabases命令了)2015-10-30T16:41:31.131+0800 E QUERY Error: listDatabases failed:{ "ok" : 0, "errmsg" : "not authorized on admin to execute command { listDatabases: 1.0 }", "code" : 13} at Error (<anonymous>) at Mongo.getDBs (src/mongo/shell/mongo.js:47:15) at shellHelper.show (src/mongo/shell/utils.js:630:33) at shellHelper (src/mongo/shell/utils.js:524:36) at (shellhelp2):1:1 at src/mongo/shell/mongo.js:47> use adminswitched to db admin> db.auth('ljaiadmin','123456') (注:切换到admin用户进行授权验证)1> show dbs (注:验证完成后,就可以读写等操作)admin 0.000GBlocal 0.000GBtest100 0.000GBtest2 0.000GB> use test2switched to db test2> show tablestesttest2> db.test2.find(){ "_id" : ObjectId("5632cf116207909a76446af7"), "name" : "1" }> db.test2.drop()true> db.dropDatabase(){ "dropped" : "test2", "ok" : 1 }> show dbsadmin 0.000GBlocal 0.000GBtest100 0.000GB> use test100switched to db test100> db.test111.insert({"test":"test"})WriteResult({ "nInserted" : 1 })> db.test111.find(){ "_id" : ObjectId("56332db373f771b3d95638bb"), "test" : "test" }> use adminswitched to db admin> show users{ "_id" : "admin.ljaiadmin", "user" : "ljaiadmin", "db" : "admin", "roles" : [ { "role" : "root", "db" : "admin" } ]}>
创建普通用户
用可以对test123数据库读写的rwtest123用户为例:
> use test123switched to db test123> db.createUser(... {... user: "rwtest123",... pwd: "123456",... roles: [ { role: "readWrite", db: "test123" } ]... }... )Successfully added user: { "user" : "rwtest123", "roles" : [ { "role" : "readWrite", "db" : "test123" } ]}#所建的rwtest123用户可以在test123数据库中进行增删改查操作,但是其他操作就不行了>db.auth('rwtest123','123456')switched to db test123> db.test123.insert({"test":"test"})WriteResult({ "nInserted" : 1 })> db.test123.find(){ "_id" : ObjectId("563332ebc8a59ae4fe96bbf5"), "test" : "test" }> db.test123.drop()true> use test100switched to db test100> db.test100.find()Error: error: { "$err" : "not authorized for query on test100.test100", "code" : 13 }>
配置参考:
MongoDB数据库的用户权限控制权限还是比较多的,有系统自带的,已经定义好的角色,也可以自己定义角色权限,需要根据业务需要进行权限分配:
自带角色的说明(一般内置的角色基本上就可以满足生产环境需求了):
https://docs.mongodb.org/manual/core/security-built-in-roles/
用户自行定义角色的说明:
https://docs.mongodb.org/manual/core/security-user-defined-roles/
用户管理配置的说明
https://docs.mongodb.org/manual/reference/method/#user-management-methods
更多MongoDB相关内容可以看看以下的有用链接:
MongoDB 3.0 正式版发布下载 http://www.linuxidc.com/Linux/2015-03/114414.htm
CentOS编译安装MongoDB http://www.linuxidc.com/Linux/2012-02/53834.htm
CentOS 编译安装 MongoDB与mongoDB的php扩展 http://www.linuxidc.com/Linux/2012-02/53833.htm
CentOS 6 使用 yum 安装MongoDB及服务器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htm
Ubuntu 13.04下安装MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htm
MongoDB入门必读(概念与实战并重) http://www.linuxidc.com/Linux/2013-07/87105.htm
Ubunu 14.04下MongoDB的安装指南 http://www.linuxidc.com/Linux/2014-08/105364.htm
《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htm
Nagios监控MongoDB分片集群服务实战 http://www.linuxidc.com/Linux/2014-10/107826.htm
基于CentOS 6.5操作系统搭建MongoDB服务 http://www.linuxidc.com/Linux/2014-11/108900.htm
MongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里
本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-11/124725.htm
- 单机环境下的用户授权模块配置
- 配置MOSS2007beta2的单机开发环境(下)
- 单机伪分布下的hadoop+spark 环境配置
- Storm 单机环境的安装与配置
- Storm 单机环境的安装与配置
- LINUX下的用户访问授权
- Hadoop单机环境配置
- Hadoop单机环境配置
- Hadoop单机环境配置
- Storm单机环境配置
- Storm单机环境配置
- hadoop单机环境配置
- Windows环境下solrCloud 6.x的配置安装(单机模拟多服务器) - zookeeper部署
- Windows环境下solrCloud 6.x的配置安装(单机模拟多服务器) - solrClound部署
- CentOS 6.4单机环境下安装配置Storm
- Redis单机、集群环境下配置及模板工具类
- Windows单机环境下配置MySQL多实例
- 在ubuntu下搭建hadoop环境(单机配置)
- SharedPreferences
- java集合类总结
- 第一个jsp和servlet留影
- Linux中文显示乱码?如何设置centos显示中文
- vb作业
- 单机环境下的用户授权模块配置
- 第 0000 题
- Android APP终极瘦身
- Android中Toast如何在子线程中调用
- Linux操作系统中模块操作相关命令解释
- struts2配置文件
- 1.样式表的一些bug, 2.如何通过键盘事件插入spinbox,3.不改变系统编码的前提下,单独转换某一字体编码,4.多色彩颜色失败
- Linux环境下安装R语言
- make error: libneon.a: could not read symbols: Bad value