mongo Rep set +sharding 配置

mongo Rep set +sharding 配置
http://www.ityouknow.com/mongodb/2017/08/05/mongodb-cluster-setup.html)
一 、拓扑http://www.jb51.net/article/92847.htm
在这里插入图片描述
二、准备
1、mongo 版本为3.2.6
2、安装
yum install mongodb-org mongodb-org-shell mongodb-org-server
3、分配端口
mongos:27017 10.0.24.10
configser:21000 10.0.24.10
shard1:27017 10.0.24.11/10.0.24.12
shard2:27018 10.0.24.22/10.0.24.23
三、配置
1、config-server启动
mongod --configsvr --dbpath /home/mongodb/config/data --port 2100 --logpath /home/mongodb/config/log/config.log --fork
或者使用配置文件
[root@uat-bondmcf001 ~]# cat /etc/configsvr.conf
systemLog:
destination: file
path: /var/log/mongodb/configsvr.log
logAppend: true
processManagement:
fork: true
pidFilePath: “/var/lib/mongo/configsvr.pid”
net:
port: 21000
storage:
dbPath: “/var/lib/mongo/”
engine: wiredTiger
journal:
enabled: true
security:
keyFile: /var/lib/mongo/keyfile

clusterAuthMode: “keyFile”

sharding:
clusterRole: configsvr
注:3.2版本后如果有多个conf服务器要做成副本集的形式且configdb配置为
【 sharding:
configDB: configs/dev_cmf_mongo001:27018,dev_cmf_mongo002:27018】
configs为副本集名称
2.mongos启动
mongos --configdb 10.0.24.10:2100 --port 2000 --logpath /home/mongodb/mongos/log/mongos.log --fork
或者使用配置文件
[root@uat-bondmcf001 ~]# cat /etc/mongos.conf
systemLog:
destination: file
path: /var/log/mongodb/mongos.log
logAppend: true
processManagement:
fork: true
pidFilePath: /var/lib/mongo/mongos.pid
net:
port: 27017
sharding:
configDB: uat-bondmcf001:21000
security:
keyFile: /var/lib/mongo/keyfile

clusterAuthMode: “keyFile”

3.shard 启动
每台分片服务器都启动
shard1(两台机器都如此配置)
mongod --shardsvr --replSet shard1 --port 27017 --dbpath /home/mongodb/shard1/data --logpath /home/mongodb/shard1/log/shard1.log --fork --journal --logappend --syncdelay 30 --rest
或者使用配置文件
[root@uat-bondmon001 ~]# cat /etc/mongod.conf

mongod.conf

systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
storage:
dbPath: /var/lib/mongo
journal:
enabled: true
processManagement:
fork: true # fork and run in background
pidFilePath: /var/run/mongodb/mongod.pid # location of pidfile
net:
port: 27017
security:
keyFile: /var/lib/mongo/keyfile
replication:
oplogSizeMB: 2048
replSetName: “shard1”
sharding:
clusterRole: shardsvr
shard2
mongod --shardsvr --replSet shard2 --port 27018 --dbpath /home/mongodb/shard2/data --logpath /home/mongodb/shard2/log/shard2.log --fork --journal --logappend --syncdelay 30 --rest
配置文件根据shard1文件修改
注:配置文件 “security” 要注释掉,集群搭建完成后在同意开启
4.创建副本集
连接shard服务器
shard1
mongo 10.0.24.11:27017/admin
>config = {_id:“shard1”,members:[ {_id:0,host:“10.0.24.11:27017”,priority:100}, {_id:1,host:“10.0.24.12:27017”,priority:99}, ]}
>rs.initiate(config);
shard2
mongo 10.0.24.22:27017/admin
>config = {_id:“shard2”,members:[ {_id:0,host:“10.0.24.22:27018”,priority:100}, {_id:1,host:“10.0.24.23:27018”,priority:99}, ]}
>rs.initiate(config);
5.添加分片到集群
连接mongos
mongo 10.0.24.10:27017/admin
db.runCommand( { addshard :“shard1/10.0.24.11:27017,10.0.24.12:27017” } )
db.runCommand( { addshard :“shard2/10.0.24.22:27018,10.0.24.23:27018” } )
6.查看状态及应用
>use admin
>db.runCommand({listshards : 1 }); #查看分片状态
>sh.status() #查看shard信息
test库开启分片
db.runCommand({enablesharding:“test”});
test库mtable1表开启分片
db.runCommand({shardcollection:“test.mtable1”,key:{id:1}});
7.创建用户
创建超级管理员用户
db.createUser({user: “admin”,pwd: “123456”,roles: [ “root”, “userAdminAnyDatabase”, “userAdmin”, “dbOwner”, “db” : “admin” ]})
创建一个root超级用户,在admin库上认证后,可以操作所有db
db.createUser(
{
“user” : “root”,
“pwd” : “123456_2017”,
roles: [
{ “role” : “readWriteAnyDatabase”, “db” : “admin” },
{ “role” : “dbAdminAnyDatabase”, “db” : “admin” }
]
}
)
8.认证
openssl rand -base64 755 > /var/lib/mongo/qa_mongodb.keyfile
#秘钥文件权限必须为X00 ,不能有等号,不能大于1024位,必须一致
将上部配置文件注释掉的security打开
重启服务
登陆mongos方式
mongo 10.0.24.10:27017 -u “admin” -p “123456” --authenticationDatabase “admin”
四、测试
1、向test库中存入数据db
sh.enableSharding(“test”) #启用分片
sh.shardCollection(“test.Log”, { id: 1}) #Log集合启用分片
use tests
插入10000条数据
for(var i = 1; i <= 20; i++){
db.Log.save({id:i,“message”:“message”+i});
}
查看分片结果
db.Log.stats()
在这里插入图片描述
发现1个分片 保存了1条,另一个分片保存了剩下的所有记录,这是shard key的策略造成的

那我们修改一下sharding key的策略为hashed,重新插入数据
db.Log.drop()
sh.shardCollection(“test.Log”,{ id: “hashed”}) #修改为hashed规则
重新插入
for(var i = 1; i <= 30000; i++){
db.Log.save({id:i,“message”:“message”+i});
}
查看分片结果
db.Log.stats()
在这里插入图片描述
在这里插入图片描述
数据存储均衡
重要
注:shard对集合进行分片时,集合不能有非是shard key的唯一性索引
即:在shard环境下,可以对shard key指定唯一性(unique:true),其他索引就不行了(不以shard key为前缀的),会失败
查看索引
db.collections-name.getIndexes()
创建索引(以_id为key 规则为hashed)
sh.collections-name.createIndex({_id : “hashed”})《老版mongo》
db.collections-name.ensureIndex({_id : “hashed”})
集合启用分片(以_id规则为hashed的 索引为shard key)
sh.shardCollection(“DB-name.collections-name”,{_id: “hashed”})

(以下为摘抄内容)
从mongos中删除shard
db.runCommand({removeShard:“shard2/10.0.24.22:27018,10.0.24.23:27018”})
{
“msg” : “draining started successfully”,
“state” : “started”,
“shard” : “rs0”,
“ok” : 1
}
sh.status()
— Sharding Status —
sharding version: { “_id” : 1, “version” : 3 }
shards:
{ “_id” : “rs0”, “draining” : true, “host” : “rs0/192.168.9.216:27017,192.168.9.216:27018,192.168.9.216:27019” }
{ “_id” : “rs1”, “host” : “rs1/192.168.9.216:27020,192.168.9.216:27021,192.168.9.216:27022” }
{ “_id” : “rs2”, “host” : “rs2/192.168.9.216:27023,192.168.9.216:27024,192.168.9.216:27025” }
databases:
{ “_id” : “admin”, “partitioned” : false, “primary” : “config” }
副本集一直处在draining状态,需要再次执行命令
db.rumCommand({removeShard:“rs0”})
补充 需要移除的shard中有数据
如果shard2中有数据移除会报错
mongos> db.runCommand( { removeShard: “rs0” } )
{
“msg” : “draining ongoing”,
“state” : “ongoing”,
“remaining” : {
“chunks” : NumberLong(0),
“dbs” : NumberLong(1)
},
“note” : “you need to drop or movePrimary these databases”,
“dbsToMove” : [
“crm”
],
“ok” : 1
}
要将数据库移除
mongos> db.runCommand( { movePrimary: “crm”, to: “shard1” })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值