沙漠网

系统IT运维技术站

mongodb3副本集以及权限的配置

注:以下操作均在mongodb3的版本下执行,不适用mongodb2.*的版本
安装并依次启动mongodb

mongod --port=27017 --dbpath=/data/mongodb/data --logpath=/data/mongodb/log/mongodb.log --logappend --replSet=rs1  --fork

mongod --port=27018 --dbpath=/data/mongodb2/data --logpath=/data/mongodb2/log/mongodb.log --logappend --replSet=rs1  --fork

mongod --port=27019 --dbpath=/data/mongodb3/data --logpath=/data/mongodb3/log/mongodb.log --logappend --replSet=rs1  --fork
以上是在一台机器中启用了三个实例,端口分别为:27017,27018,27019,其中--replSet=rs1 的意思是三个实例组成的副本集名称为rs1


初始化副本集

use admin

config={_id:"rs1", members:[ {_id:0,host:"127.0.0.1:27017"}, {_id:1,host:"127.0.0.1:27018"}, {_id:2,host:"127.0.0.1:27019"} ]}

rs.initiate(config)

rs.status() //查看状态


在另外两台从库执行

use admin

db.getMongo().setSlaveOk()


至此,三台机器组成的副本集配置完成


如果要添加副本集机器,执行

rs.add("192.168.27.215:10004");


删除副本集机器

rs.remove("192.168.27.215:10004")


查看Replica Set状态信息

rs.conf();


权限部分

添加超级管理员

>use admin

>db.createUser({user:"root",pwd:"123456",roles:["root"]})  

登录

> use admin  

switched to db admin  

> db.auth("root","123456")


启用认证

openssl rand -base64 753 >mongodb-keyfile  //生产KEY文件

将KEY文件放在各自的机器中,并保持600权限

修改启动脚本后面追加启动参数,开启认证功能:

--keyFile=/data/mongodb/mongodb-keyfile --auth


添加管理员(管理员只能管理用户,查看数据库,查看数据库内的集合,却不能查看和操作集合内的详细内容)

use admin

db.createUser({user:'user',pwd:'123456',roles:['userAdminAnyDatabase','dbAdminAnyDatabase']}

登录

> use admin  

switched to db admin  

> db.auth("user","123456") 


单数据库读写

> use admin

db.createUser({user:"daixh001",pwd:"123456",roles:[{role:"readWrite",db:"daixh001"}]}) 

登录

> use admin  

switched to db admin  

> db.auth("dy11","123") 


单数据库读

> use admin

db.createUser({user:"dy11",pwd:"123",roles:[{role:"read",db:"text1"}]}) 

登录

> use admin  

switched to db admin  

> db.auth("dy11","123") 


多数据库读写

> use admin

db.createUser({user:"text3",pwd:"123",roles:[{role:"readWrite",db:"text3"},{role:"readWrite",db:"text4"}]})  


修改密码

use admin  

db.changeUserPassword("username", "xxx")


查看用户信息

use admin

db.runCommand({usersInfo:"userName"})


删除用户

use admin

db.dropUser()


以下是roles中的权限说明:

read 指定数据库的只读权限,拥有以下权限:

aggregate,checkShardingIndex,cloneCollectionAsCapped,collStats

count,dataSize,dbHash,dbStats,distinct,filemd5

geoNear,geoSearch,geoWalk,group

mapReduce (inline output only.),text (beta feature.)

readWrite 拥有指定数据库的读写权限,除了具有read权限,还拥有以下权限:

cloneCollection (as the target database.),convertToCapped

create (and to create collections implicitly.)

drop(),dropIndexes,emptycapped,ensureIndex()

findAndModify,mapReduce (output to a collection.)

renameCollection (within the same database.)

read和readWrite主要就是对库中表的操作权限

dbAdmin 指定数据库的管理权限

clean,collMod,collStats,compact,convertToCapped

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

indexStats,profile,reIndex,renameCollection (within a single database.),validate

userAdmin 指定数据库的用户管理权限

clusterAdmin 集群管理权限(副本集、分片、主从等相关管理)

addShard,closeAllDatabases,connPoolStats,connPoolSync,_cpuProfilerStart

_cpuProfilerStop,cursorInfo,diagLogging,dropDatabase

enableSharding,flushRouterConfig,fsync,db.fsyncUnlock()

getCmdLineOpts,getLog,getParameter,getShardMap,getShardVersion

hostInfo,db.currentOp(),db.killOp(),listDatabases,listShards

logRotate,moveChunk,movePrimary,netstat,removeShard,unsetSharding

repairDatabase,replSetFreeze,replSetGetStatus,replSetInitiate

replSetMaintenance,replSetReconfig,replSetStepDown,replSetSyncFrom

resync,serverStatus,setParameter,setShardVersion,shardCollection

shardingState,shutdown,splitChunk,splitVector,split,top,touch

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

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

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

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


下方列出系统内置角色名称:

Database User Roles 普通用户角色


read

readWrite

Database Administration Roles 管理员角色


dbAdmin        可以管理数据库

dbOwner        单数据库最大权限,dbAdmin,userAdmin

userAdmin     可管理当前数据库用户

 

Cluster Administration Roles  管理员角色

clusterAdmin

clusterManager

clusterMonitor

hostManager

Backup and Restoration Roles  备份和恢复角色


backup

restore

All-Database Roles 所有数据库角色


readAnyDatabase       在admin下建立,可以读取所有数据库的信息

readWriteAnyDatabase  在admin下建立,可以读写所有数据库的信息

userAdminAnyDatabase  在admin下建立,可以管理所有数据库的用户

dbAdminAnyDatabase    在admin下建立,可以管理所有数据库的信息(类似于所有数据库的dbAdmin账户)



PHP连接使用代码:


$server = "192.168.75.132:27017,192.168.75.132:27018,192.168.75.132:27019";// 可以只有一部分,如两台的信息

$server = "192.168.75.132:27018,192.168.75.132:27019";


$options = [

    'readPreference' => MongoClient::RP_SECONDARY_PREFERRED,

    'replicaSet' => 'rs',//要连接的集群名称

];

$user = 'wayne';

$passwd = 'wayne';

$dbname = 'wayne_com';

$collectName = 'test_list';


$dsn = "mongodb://$user:$passwd@{$server}/{$dbname}";

$mongo = new MongoClient($dsn, $options);


$mongo->selectDB($dbname);

$coll = $mongo->selectCollection($dbname, $collectName);


$where = ['name'=>'tong'];

$cursor = $coll->find([])->limit(5);

if ($cursor) {    

    foreach ($cursor as $doc) {

        var_dump($doc);

    }

} else {

    echo 'empty data';

}


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

«    2023年11月    »
12345
6789101112
13141516171819
20212223242526
27282930
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接
  • RainbowSoft Studio Z-Blog
  • 订阅本站的 RSS 2.0 新闻聚合

Powered By Z-BlogPHP 1.7.3

Copyright Daixh.com Rights Reserved. 京ICP备09012299号-1

联系我:home#daixh.com (#改@)