欢迎投稿

今日深度:

mongodb3.X版本用户认证步骤,mongodb3.x认证步骤

mongodb3.X版本用户认证步骤,mongodb3.x认证步骤


mongodb从3.0版本开始加入了SCRAM-HAS-1方式,但是PHP里不支持,支持的2.X版本里的MONGODB-CR模式,这个就需要修改下配置了。有人说在配置文档里加 setParameter

setParameter:  
    authenticationMechanisms: MONGODB-CR

需要注意一点,配置文件的缩进是用空格的,制表符会报错的。

但是没有起作用,可能针对3.2之前的版本可以吧,也或者某些细节没注意到,然后继续靠网络和文档,发现了另一种方法,需要下面6步骤:

为了兼容 3.0之前的版本,需要进行下面几步
1、在不开启auth的模式进入mongo
use admin;
db.createUser({user:'shang',pwd:'shang',roles:[{role:'root',db:'admin'}]});//因为不执行这一步,db.system.version里不会有数据
2、修改 authcurrentVersion
var schema = db.system.version.findOne({'_id':'authSchema'});
schema.currentVersion=3;
db.system.version.save(schema);
3、需要drop掉之前使用 SCRAM-HAS-1 认证的用户 shang
db.dropUser('shang');
4、以不开启auth的模式重启后创建新的用户
db.shutdownServer();
mongo.exe
use admin;
db.createUser({user:'shang',pwd:'shang',roles:[{role:'root',db:'admin'}]});//现在创建的是使用 MONGODB-CR认证的用户
5、以开启 auth的模式重启
use admin;
db.auth('shang', 'shang');
6、现在还可以创建其他用户
use robo;
db.createUser({user:'robo',pwd:'robo',roles:[{role:'readWrite',db:'robo'}]});//经测试,使用Robomongo连接,选择两种认证都可以联通

后来发现起始修改的是 参数 authSchemaVersion 不知道直接在配置文件里加入下面的代码可行吗?可以找时间试试,经证实不可以,会提示 “BadValue: Cannot use --setParameter to set "authSchemaVersion" at startup”。

setParameter:
    authSchemaVersion: 3

下面还有关于配置文件和使用配置文件安装到window服务和启动的命令

配置文件 mongo.cfg
systemLog:
    destination: file
    path: D:/mongodb/data/log/mongod.log
storage:
    dbPath: D:/mongodb/data/db
net:
    bindIp: 127.0.0.1
    port: 27017
security:  
    authorization: enabled
setParameter:  
    authenticationMechanisms: MONGODB-CR
    enableLocalhostAuthBypass: false
    logLevel: 4

使用配置文件启动和安装到window服务
mongod.exe --config D:/mongodb/mongod.cfg --install
net start MongoDB
net stop MongoDB

最后说下关于认证里面的角色

基本的角色 
userAdminAnyDatabase 这个角色拥有分配角色和用户的权限,但没有查写的缺陷
root  这是超级管理员
readWrite  有读写权限
read    有读权限

关闭服务
db.shutdownServer();

www.htsjk.Com true http://www.htsjk.com/DB2/20577.html NewsArticle mongodb3.X版本用户认证步骤,mongodb3.x认证步骤 mongodb从3.0版本开始加入了SCRAM-HAS-1方式,但是PHP里不支持,支持的2.X版本里的MONGODB-CR模式,这个就需要修改下配置了。有人说在配置文档里...
评论暂时关闭