1、官网下载安装
Download MongoDB Community Server | MongoDBDownload MongoDB Community Server non-relational database to take your next big project to a higher level!https://www.mongodb.com/try/download/community
2、官网下载shell工具,否则装好后无法创建admin账号和修改密码进行不了验证操作
MongoDB Shell Download | MongoDBThe MongoDB Shell is a modern command-line experience, full with features to make it easier to work with your database. Free download. Try now!https://www.mongodb.com/try/download/shell
进入后都先选择环境,然后直接下载
3、安装好会默认启动,验证是否启动可以在浏览器访问下面的网址
http://localhost:27017/
启动成功:
4、启动安装的shell工具
先切换到admin库
use admin
然后先创建admin用户和密码
db.createUser({ user: 'admin', pwd: '123456', roles: [{ role: 'userAdminAnyDatabase', db: 'admin'}]});
然后编辑配置文件:
启用鉴权:
security:
authorization: enabled
然后重启服务:
插入一个cmd命令添加服务的示例
1、 管理员权限打开cmd窗口
2、注册服务命令:sc create 服务名 binpath= 程序所在路径 type= own start= auto displayname= 服务显示名称
3、将MongoDb插入后可以用下面的方式启动
启动: net start mongodb
停止:2 net stop mongodb
不想麻烦还可以用命令行直接关闭
db.shutdownServer()
或者
bin / mongod--port = 27017--dbpath =/ soft / mongodb / mongodb / data--shutdown
5、再次进入shell工具直接输入admin然后回车
先鉴权:
db.auth("admin","123456");
可以的话说明鉴权成功,用其他工具链接也需要输入账号密码了
6、相关授权命令
MongoDB角色有:
数据库用户角色:read、readWrite;
数据库管理角色:dbAdmin、dbOwner、userAdmin;
集群管理角色:clusterAdmin、clusterManager、clusterMonitor、hostManager;
备份恢复角色:backup、restore;
所有数据库角色:readAnyDatabase、readWriteAnyDatabase、userAdminAnyDatabase、dbAdminAnyDatabase
超级用户角色:root;
MongoDb权限有:
read: 允许用户读取指定数据库
readWrite: 允许用户读写指定数据库
dbAdmin:允许用户在指定数据库中执行管理函数,如索引创建、删除,查看统计或访问
system.profile
userAdmin:允许用户向system.users集合写入,可以找指定数据库里创建、删除和管理用户
clusterAdmin:只在admin数据库中可用,具有所有分片和复制集相关函数的管理权限。
readAnyDatabase:只在admin数据库中可用,具有所有数据库的读权限
readWriteAnyDatabase:只在admin数据库中可用,具有所有数据库的读写权限
userAdminAnyDatabase:只在admin数据库中可用,具有所有数据库的userAdmin权限
dbAdminAnyDatabase:只在admin数据库中可用,具有所有数据库的dbAdmin权限。
root:只在admin数据库中可用。超级账号,超级权限。
创建普通用户:
db.createUser(
{
user: "user",
pwd: "password",
roles: [{ role: "readWrite",db:”user”}],
} )
创建管理员:
db.createUser(
{
user: "DbUser",
pwd: "DbPwd“,roles:["readWriteAnyDatabase", "dbAdminAnyDatabase"]
})
修改对应用户的权限:
db.updateUser( "user", { customData: { info: "测试修改权限" }, roles: [ { role: "clusterAdmin", db: "admin" }] } )
收回权限:
db.revokeRolesFromUser( "user", [ { role: "read", db: "admin" } ] )
修改密码:
db.changeUserPassword("user", "12345678")
对某个用户的某个数据库的某个操作进行限制:
db.createRole({role: "test",privileges: [{ resource: { db: "testdb", collection: "test1" }, actions: ["find", "update"] },{ resource: { db: "testdb", collection: "test2" }, actions: ["find"] },],roles: []})
查询数据库下的用户:
db.system.users.find()
删除用户:
db.dropUser(“user”)
mongod.conf配置文件
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: D:\mongodb\data
journal:
#日志操作间隔
commitIntervalMs: 500
#保留的日志的最小时长
oplogMinRetentionHours: 6
wiredTiger:
engineConfig:
#所有数据的内部缓存的最大大小
cacheSizeGB: 2
#压缩方式,比zlib快但大
journalCompressor: snappy
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: D:\mongodb\log\mongod.log
traceAllExceptions: true
# network interfaces
net:
port: 27017
bindIp: 127.0.0.1
wireObjectCheck: true
#processManagement:
security:
authorization: enabled
#慢速操作时间阈值
operationProfiling:
slowOpThresholdMs: 300
#操作日志大小
replication:
oplogSizeMB: 10
#sharding:
## Enterprise-Only Options:
#auditLog: