本文大都网上参考的,我只是整理了一下

一默认配置情况

1.MongoDB安装时不添加任何参数,默认是没有权限验证的,任何用户都可以登录进来,而且登录的用户可以对数据库任意操作而且可以远程访问数据库,需以--auth参数启动。

2.在刚安装完毕的时候MongoDB都默认有一个admin数据库,此时admin数据库是空的,没有记录权限相关的信息。当admin.system.users一个用户都没有时,即使

mongod启动时添加了--auth参数,如果没有在admin数据库中添加用户,此时不进行任何认证还是可以做任何操作(不管是否是以--auth 参数启动),直到在

admin.system.users中添加了一个用户。

3.MongoDB的访问分为连接和权限验证,即使以--auth参数启动还是可以不使用用户名连接数据库,但是不会有任何的权限进行任何操作

4.admin数据库中的用户名可以管理所有数据库,其他数据库中的用户只能管理其所在的数据库。

二安装安全

1,运行账户

windows下可以使用network service 或者新建一个用户,使用默认的USERS组,然后添加给予数据库文件及日志存储目录的写权限,并建议取消对cmd等程序的

执行权限。

linux下新建一个账户,给予程序的执行权限和数据库文件及日志目录的读写权限,并建议取消对sh等程序的执行权限

groupadd mongodb

useradd -g mongodb -s /sbin/nologin -d /dev/null mongodb   //-d 指定家目录为/dev/null

chown -R mongodb:mongodb /data/mongodb/

chown -R mongodb:mongodb /usr/local/mongodb/

2,文件安全

应赋予 MongoDB 相关的文件合适的权限,防止被其它用户非授权访问或篡改。建议按照下面的建议实施权限控制:,

# 配置文件只允许属主读取和修改、属组读取

/usr/local/mongodb/mongodb.conf(640)

# 数据目录只允许属主读取和修改

/usr/local/mongodb/data/(600)

# 日志文件只允许属主读取和修改、属组读取

/usr/local/mongodb/log/mongodb.log(640)

三安装后的实践

1. 为数据库增加管理员

use admin

db.createUser({

>user:'userName',

pwd:'password',

roles:[{role:'userAdminAnyDatabase',db:'admin'}]

})

2.修改配置文件

vim /etc/mongod.conf

启用用户认证

auth=true

设置监听地址

bind_ip=192.168.31.139

最好在网卡上绑定一个新IP,并关闭ping,只开放一个高端端口

设置监听端口

port=33333

在2.6以后这2个接口默认是关闭的

nohttpinterface=true

rest = false

3.重启服务生效

service mongod restart

4. 为普通数据库创建用户

use test db.createUser({user:'admin',pwd:'admin',roles:[{role:'readWrite',db:'test'}]})

在为test数据库创建用户之前,你必须先使用前面创建的管理员账号登录

role这里有两个规则:readWrite表示可读可写,read表示为只能读

5.修改和删除账户

use admin

db.auth('name','pass') //身份认证

db.removeUser('admin')

运行删除admin这个账号

use admin

db.updateUser({user:'admin',pwd:'123456'})

更改密码为123456

6.禁止javascript脚本执行

MongoDB对js做了一定的扩展。 db.eval(code) 实际上底层执行的是 db.$cmd*系列js代码

如果程序可以不使用javascript的话,关闭执行

vim /etc/mongod.conf

noscripting=true

重启服务生效

注,下面的命令都是调用js接口

rs1:PRIMARY> db.$cmd.help()db.$cmd.help()

DBCollection help

db.$cmd.find().help() - show DBCursor help

db.$cmd.bulkWrite( operations, <optional params> ) - bulk execute write operations, optional parameters are: w, wtimeout, j

db.$cmd.count( query = {}, <optional params> ) - count the number of documents that matches the query, optional parameters are: limit, skip, hint, maxTimeMS

db.$cmd.copyTo(newColl) - duplicates collection by copying all documents to newColl; no indexes are copied.

db.$cmd.convertToCapped(maxBytes) - calls {convertToCapped:'$cmd', size:maxBytes}} command

db.$cmd.createIndex(keypattern[,options])

db.$cmd.createIndexes([keypatterns], <options>)

db.$cmd.dataSize()

db.$cmd.deleteOne( filter, <optional params> ) - delete first matching document, optional parameters are: w, wtimeout, j

db.$cmd.deleteMany( filter, <optional params> ) - delete all matching documents, optional parameters are: w, wtimeout, j

db.$cmd.distinct( key, query, <optional params> ) - e.g. db.$cmd.distinct( 'x' ), optional parameters are: maxTimeMS

db.$cmd.drop() drop the collection

db.$cmd.dropIndex(index) - e.g. db.$cmd.dropIndex( "indexName" ) or db.$cmd.dropIndex( { "indexKey" : 1 } )

db.$cmd.dropIndexes()

db.$cmd.ensureIndex(keypattern[,options]) - DEPRECATED, use createIndex() instead

db.$cmd.explain().help() - show explain help

db.$cmd.reIndex()

db.$cmd.find([query],[fields]) - query is an optional query filter. fields is optional set of fields to return.

e.g. db.$cmd.find( {x:77} , {name:1, x:1} )

db.$cmd.find(...).count()

db.$cmd.find(...).limit(n)

db.$cmd.find(...).skip(n)

db.$cmd.find(...).sort(...)

db.$cmd.findOne([query], [fields], [options], [readConcern])

db.$cmd.findOneAndDelete( filter, <optional params> ) - delete first matching document, optional parameters are: projection, sort, maxTimeMS

db.$cmd.findOneAndReplace( filter, replacement, <optional params> ) - replace first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument

db.$cmd.findOneAndUpdate( filter, update, <optional params> ) - update first matching document, optional parameters are: projection, sort, maxTimeMS, upsert, returnNewDocument

db.$cmd.getDB() get DB object associated with collection

db.$cmd.getPlanCache() get query plan cache associated with collection

db.$cmd.getIndexes()

db.$cmd.group( { key : ..., initial: ..., reduce : ...[, cond: ...] } )

db.$cmd.insert(obj)

db.$cmd.insertOne( obj, <optional params> ) - insert a document, optional parameters are: w, wtimeout, j

db.$cmd.insertMany( [objects], <optional params> ) - insert multiple documents, optional parameters are: w, wtimeout, j

db.$cmd.mapReduce( mapFunction , reduceFunction , <optional params> )

db.$cmd.aggregate( [pipeline], <optional params> ) - performs an aggregation on a collection; returns a cursor

db.$cmd.remove(query)

db.$cmd.replaceOne( filter, replacement, <optional params> ) - replace the first matching document, optional parameters are: upsert, w, wtimeout, j

db.$cmd.renameCollection( newName , <dropTarget> ) renames the collection.

db.$cmd.runCommand( name , <options> ) runs a db command with the given name where the first param is the collection name

db.$cmd.save(obj)

db.$cmd.stats({scale: N, indexDetails: true/false, indexDetailsKey: <index key>, indexDetailsName: <index name>})

db.$cmd.storageSize() - includes free space allocated to this collection

db.$cmd.totalIndexSize() - size in bytes of all the indexes

db.$cmd.totalSize() - storage allocated for all data and indexes

db.$cmd.update( query, object[, upsert_bool, multi_bool] ) - instead of two flags, you can pass an object with fields: upsert, multi

db.$cmd.updateOne( filter, update, <optional params> ) - update the first matching document, optional parameters are: upsert, w, wtimeout, j

db.$cmd.updateMany( filter, update, <optional params> ) - update all matching documents, optional parameters are: upsert, w, wtimeout, j

db.$cmd.validate( <full> ) - SLOW

db.$cmd.getShardVersion() - only for use with sharding

db.$cmd.getShardDistribution() - prints statistics about data distribution in the cluster

db.$cmd.getSplitKeysForChunks( <maxChunkSize> ) - calculates split points over all chunks and returns splitter function

db.$cmd.getWriteConcern() - returns the write concern used for any operations on this collection, inherited from server/db if set

db.$cmd.setWriteConcern( <write concern doc> ) - sets the write concern for writes to the collection

db.$cmd.unsetWriteConcern( <write concern doc> ) - unsets the write concern for writes to the collection

7,使用防火墙限制连接的来源IP

四附:

1,常见不合理配置用户权限

仅仅使用一个高权限用户(如root)来执行所有操作

给一个用户多于他需要的权限

使用弱密码或者多个账号同用一个密码

删除数据库后没有删除相应的用户

2:副本集认证

副本集总体思路是用户名、密码和keyfile文件,keyfile需要各个副本集服务启动时加载而且要是同一文件,然后在操作库是需要用户名、密码

KeyFile文件必须满足条件:

(1)至少6个字符,小于1024字节

(2)认证时候不考虑文件中空白字符

(3)连接到副本集的成员和mongos进成的keyfile文件内容必须一样

(4)必须是base64编码,但是不能有等号

(5)文件权限必须是x00,也就是说,不能分配任何权限给group成员和other成员

五MongoDB中用户的角色说明

1. read角色

数据库的只读权限,包括:

aggregate,checkShardingIndex,cloneCollectionAsCapped,collStats,count,dataSize,dbHash,dbStats,distinct,filemd5,mapReduce (inline output only.),text (beta feature.)geoNear,geoSearch,geoWalk,group

2. readWrite角色

数据库的读写权限,包括:

read角色的所有权限

cloneCollection (as the target database.),convertToCapped,create (and to create collections implicitly.),renameCollection (within the same database.)findAndModify,mapReduce (output to a collection.)

drop(),dropIndexes,emptycapped,ensureIndex()

3. dbAdmin角色

数据库的管理权限,包括:

clean,collMod,collStats,compact,convertToCappe

create,db.createCollection(),dbStats,drop(),dropIndexes

ensureIndex(),indexStats,profile,reIndex

renameCollection (within a single database.),validate

4. userAdmin角色

数据库的用户管理权限

5. clusterAdmin角色

集群管理权限(副本集、分片、主从等相关管理),包括:

addShard,closeAllDatabases,connPoolStats,connPoolSync,_cpuProfilerStart_cpuProfilerStop,cursorInfo,diagLogging,dropDatabase

shardingState,shutdown,splitChunk,splitVector,split,top,touchresync

serverStatus,setParameter,setShardVersion,shardCollection

replSetMaintenance,replSetReconfig,replSetStepDown,replSetSyncFrom

repairDatabase,replSetFreeze,replSetGetStatus,replSetInitiate

logRotate,moveChunk,movePrimary,netstat,removeShard,unsetSharding

hostInfo,db.currentOp(),db.killOp(),listDatabases,listShardsgetCmdLineOpts,getLog,getParameter,getShardMap,getShardVersion

enableSharding,flushRouterConfig,fsync,db.fsyncUnlock()

6. readAnyDatabase角色

任何数据库的只读权限(和read相似)

7. readWriteAnyDatabase角色

任何数据库的读写权限(和readWrite相似)

8. userAdminAnyDatabase角色

任何数据库用户的管理权限(和userAdmin相似)

9. dbAdminAnyDatabase角色

任何数据库的管理权限(dbAdmin相似)

mongodb安全整理的更多相关文章

  1. mongodb基础整理篇————常规操作[二]

    前言 简单整理一下常规操作. 正文 虽然一般说写代码看的是思想,但是呢,如果不知道mongodb 有哪些常用的操作,那么你怎么能知道mongodb是否符合你的需求,比如说如果聚合功能都没有,你得自己写 ...

  2. mongodb基础整理篇————副本概念篇[外篇]

    前言 副本集整理. 开始逐步把mongodb博客补齐了. 正文 什么是副本集 副本集是一组服务器,其中一个是用于处理写入操作的主节点,还有多个用于保存主节点的数据副本的从节点. 如果主节点崩溃了,则从 ...

  3. 【面试虐菜】—— MongoDB知识整理

    为什么我们要使用MongoDB? 特点: 高性能.易部署.易使用,存储数据非常方便.主要功能特性有: 面向集合存储,易存储对象类型的数据. 模式自由. 支持动态查询. 支持完全索引,包含内部对象. 支 ...

  4. 现在开始学习WPF了,mongodb在整理一下

    回忆一下自己学习mongodb的过程 1安装 2增删改查 3数据类型转换 4GridFS 5权限管理--开启权限之前先建立一个超级用户(admin库中),开启权限,用该用户登陆,进入admin数据库( ...

  5. mongodb菜鸟整理 2 C#Driver使用

    一下载 从官网上下载... 二 引用 下载完了将其解压到某个文件夹内,打开vs建立一个工程 右键引用,找到刚才解压的目录,把里面说有的dll文件全部添加就好 三 命名空间 我都是先打然后alt+shi ...

  6. mongodb菜鸟整理

    一,mongodb的安装与连接 从官网下载后,解压或者安装到某个目录下 1  首先需要自己创建一个db文件夹 ,用于存放数据库的数据 然后还需要创建一个log文件夹,里面需要自己创建一个mongodb ...

  7. MongoDB——待整理

    MongoDB mongoose——http://mongoosejs.com/ npm i mongoose Mongoose 通过外键与另一张表建立关联:Mongoose Populate 基本使 ...

  8. mongoDB用法整理

    1. mongoDB UI工具, Studio 3T,用Non_Commercial的版本就足够. 2. 查询某字段长度大于特定值的 db.test.find({ F_DAQDATA: { $type ...

  9. MongoDB 查询整理

    查询所有sql:  select * from table_namemongodb:   db.getCollection('期刊论文').find({}) 如上图所示,获取期刊论文collectio ...

随机推荐

  1. python http通信实现

    http协议通信需要httpServer和httpClient. 在python中 -- httpServer的实现类是server.py文件,要跟实现tcp,udp Server的文件sockets ...

  2. SQL Server中的聚集索引(clustered index) 和 非聚集索引 (non-clustered index)

    本文转载自  http://blog.csdn.net/ak913/article/details/8026743 面试时经常问到的问题: 1. 什么是聚合索引(clustered index) / ...

  3. SQL Server(三)

    一.数据库操作 create database 数据库名称 ——创建drop database 数据库名称 ——删除use 数据库名称 ——使用go 两条SQL语句之间分隔 二.表的操作 create ...

  4. 删除ue4中c++类

    http://gad.qq.com/program/translateview/7190281 删除一个C++类 该方法是从UE4的answerhub上摘选的.本教程介绍了从项目中删除一个C++类所需 ...

  5. 解决webSocke客户端连接服务端返回400错误

    原因: nginx使用了转发,头信息没设置全,问题出现在nginx的配置文件 解决办法: 修改nginx.conf配置文件,在linux终端敲入vim /etc/nginx/nginx.conf,找到 ...

  6. python 之 函数 生成器

    5.10 生成器 函数内有yield关键字,再调用函数就不会立刻执行函数体代码,会得到一个返回值,该返回值就是生成器,生成器本质就是迭代器 def chicken():    print('===== ...

  7. 浅谈JAVA GUI中,AWT与Swing的区别、联系及优缺点

    浅谈JAVA GUI中,AWT与Swing的区别.联系及优缺点 A.区别 1.发布的时间 AWT是在JDK 1.0版本时提出的 Swing是在AWT之后提出的(JAVA 2) 2. ”重量” AWT是 ...

  8. JMeter(5) JMeter之BeanShell使用

    BeanShell介绍 BeanShell是用Java写成的,一个小型的.免费的.可以下载的.嵌入式的Java源代码解释器,具有对象脚本语言特性.本篇只记录一下基本的使用.有以下五个组件: Beans ...

  9. puthon-进程间通信-队列和管道

    ********进程间通信-队列和管道******** ****进程间通信-队列和管道 IPC(Inter-Process Communication) ****队列 **概念介绍 创建贡献的进程队列 ...

  10. 转 dos 下的 find 和 重定向 and 删除

    1.find /i "ora-" *.* > check.log del /Q .\log\*.* 附录: 我对findstr是如此的依赖,以至于当我向各位讲解find命令的 ...