您的位置:首页 > 数据库 > Mongodb

mongodb sharding配置

2016-09-21 10:49 288 查看

mongodb集群配置

1Mongo使用sharding集群

mongodb副本集群

mongos1

192.168.20.137

mongos2

192.168.20.138

mongo_config1

192.168.20.139

mongo_config2

192.168.20.129

mongo_config3

192.168.20.128

mongod_shard1_replicaset1

192.168.20.127

mongod_shard1_replicaset2

192.168.20.126

mongod_shard1_replicaset3

192.168.20.125

mongod_shard2_replicaset1

192.168.20.123

mongod_shard2_replicaset2

192.168.20.122

mongod_shard2_replicaset3

192.168.20.121



2下载mongodb源码包

[root@~]#wgethttps://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.2.9.tgz

3解压缩,并拷贝到/data目录下

tar-zxvfmongodb-linux-x86_64-rhel70-3.2.9.tgz

mvmongodb-linux-x86_64-rhel70-3.2.9/data/mongodb

4新建mongodb目录

mkdir-p/data/mongodb/etc/

mkdir-p/data/mongodb/var/log/

mkdir
/data/mongodb/run

mkdir
/data/mongodb/data

mkdir
/data/mongodb/logs

groupaddmongodb

useradd-g
mongodb-s/sbin/nologin-Mmongodb

chown-Rmongodb.mongodb
/data/mongodb

5mongod配置文件文件:

vim/data/mongodb/etc/mongodb.cnf

systemLog:

verbosity:0

quiet:false

syslogFacility:user

path:
"/data/mongodb/logs/mongod.log"

logAppend:true

logRotate:rename

destination:file

timeStampFormat:iso8601-utc

storage:

dbPath:"/data/mongodb/data"

indexBuildRetry:true

journal:

enabled:true

directoryPerDB:true

syncPeriodSecs:60

engine:wiredTiger

wiredTiger:

engineConfig:

cacheSizeGB:1

statisticsLogDelaySecs:0

journalCompressor:snappy

directoryForIndexes:true

collectionConfig:

blockCompressor:snappy

indexConfig:

prefixCompression:true

processManagement:

fork:true

pidFilePath:
"/data/mongodb/run/mongod.pid"

net:

bindIp:0.0.0.0

port:27017

http:

enabled:true

RESTInterfaceEnabled:true

operationProfiling:

slowOpThresholdMs:100

mode:"slowOp"

security:

authorization:"disabled"

replication:

oplogSizeMB:1024

replSetName:"mongod_shard1_im_test"###同一个sharding相同,不同的shardingrelSetName不通

5.
设置开机启动

[root@~]#vim/etc/rc.local

[root@~]#/data/mongodb/bin/mongod-f
/data/mongodb/etc/mongodb.cnf--rest

6启动mongodb

/data/mongodb/bin/mongod-f
/data/mongodb/etc/mongodb.cnf–rest

7.配置副本集群

[root@etc]#/data/mongodb/bin/mongo#登陆到mongodb中

查看副本集状态

>rs.status()

{

"info"
:"runrs.initiate(...)ifnotyetdonefortheset",

"ok"
:0,

"errmsg"
:"noreplsetconfighasbeenreceived",

"code"
:94

}

进入到admin数据库

>useadmin

进行cfg配置
,最外层的_id表示replicaset的名字,members里包含的是所有节点的地址以及优先级。优先级最高的即成为主节点

>
cfg={_id:"mongod_shard1_im_test",members:[{_id:0,host:'192.168.20.127:27017',priority:10},
{_id:1,host:'192.168.20.126:27017',priority:5},{_id:2,host:'192.168.20.125:27017',priority:1}]}

进行初始化

>rs.initiate(cfg)

{"ok":1}

显示了每个节点的健康状况,名称,启动的时间,节点的类型等。查看当前副本集的配置表:

{

"_id"
:"mongod_shard1_im_test",

"version"
:1,

"protocolVersion"
:NumberLong(1),

"members"
:[

{

"_id"
:0,

"host"
:"192.168.20.127:27017",

"arbiterOnly"
:false,

"buildIndexes"
:true,

"hidden"
:false,

"priority"
:10,

"tags"
:{

},

"slaveDelay"
:NumberLong(0),

"votes"
:1

},

{

"_id"
:1,

"host"
:"192.168.20.126:27017",

"arbiterOnly"
:false,

"buildIndexes"
:true,

"hidden"
:false,

"priority"
:5,

"tags"
:{

},

"slaveDelay"
:NumberLong(0),

"votes"
:1

},

{

"_id"
:2,

"host"
:"192.168.20.125:27017",

"arbiterOnly"
:false,

"buildIndexes":
true,

"hidden"
:false,

"priority"
:1,

"tags"
:{

},

"slaveDelay"
:NumberLong(0),

"votes"
:1

}

],

"settings"
:{

"chainingAllowed"
:true,

"heartbeatIntervalMillis"
:2000,

"heartbeatTimeoutSecs"
:10,

"electionTimeoutMillis"
:10000,

"getLastErrorModes"
:{

},

"getLastErrorDefaults"
:{

"w"
:1,

"wtimeout"
:0

},

"replicaSetId"
:ObjectId("57dfb646ed538f91da25889a")

}

}

再次查看副本集状态

mongod_shard1_im_test:PRIMARY>
rs.status()

{

"set"
:"mongod_shard1_im_test",

"date"
:ISODate("2016-09-19T09:58:17.947Z"),

"myState"
:1,

"term"
:NumberLong(1),

"heartbeatIntervalMillis"
:NumberLong(2000),

"members"
:[

{

"_id"
:0,

"name"
:"192.168.20.127:27017",

"health"
:1,

"state"
:1,

"stateStr"
:"PRIMARY",

"uptime"
:15779,

"optime"
:{

"ts"
:Timestamp(1474279005,1),

"t"
:NumberLong(1)

},

"optimeDate"
:ISODate("2016-09-19T09:56:45Z"),

"infoMessage"
:"couldnotfindmembertosyncfrom",

"electionTime"
:Timestamp(1474279004,1),

"electionDate"
:ISODate("2016-09-19T09:56:44Z"),

"configVersion"
:1,

"self"
:true

},

{

"_id"
:1,

"name"
:"192.168.20.126:27017",

"health"
:1,

"state"
:2,

"stateStr"
:"SECONDARY",

"uptime"
:103,

"optime"
:{

"ts"
:Timestamp(1474279005,1),

"t"
:NumberLong(1)

},

"optimeDate"
:ISODate("2016-09-19T09:56:45Z"),

"lastHeartbeat"
:ISODate("2016-09-19T09:58:16.547Z"),

"lastHeartbeatRecv"
:ISODate("2016-09-19T09:58:16.332Z"),

"pingMs"
:NumberLong(0),

"syncingTo"
:"192.168.20.127:27017",

"configVersion"
:1

},

{

"_id"
:2,

"name"
:"192.168.20.125:27017",

"health"
:1,

"state"
:2,

"stateStr"
:"SECONDARY",

"uptime"
:103,

"optime"
:{

"ts"
:Timestamp(1474279005,1),

"t"
:NumberLong(1)

},

"optimeDate"
:ISODate("2016-09-19T09:56:45Z"),

"lastHeartbeat"
:ISODate("2016-09-19T09:58:16.547Z"),

"lastHeartbeatRecv"
:ISODate("2016-09-19T09:58:16.332Z"),

"pingMs"
:NumberLong(0),

"syncingTo"
:"192.168.20.127:27017",

"configVersion"
:1

}

],

"ok"
:1

}

8mongoconfigserver配置

mongo_config1

192.168.20.139

mongo_config2

192.168.20.129

mongo_config3

192.168.20.128

默认端口27019

具体配置同上,配置文件中添加
configsvr=true

systemLog:

quiet:false

path:/data/mongodb/logs/mongod.log

logAppend:false

destination:file

processManagement:

fork:true

pidFilePath:/data/mongodb/run/mongod.pid

net:

bindIp:0.0.0.0

port:27019

maxIncomingConnections:65536

wireObjectCheck:true

ipv6:false

storage:

dbPath:/data/mongodb/data

indexBuildRetry:true

journal:

enabled:true

directoryPerDB:false

engine:mmapv1

syncPeriodSecs:60

mmapv1:

quota:

enforced:false

maxFilesPerDB:8

smallFiles:true

journal:

commitIntervalMs:100

operationProfiling:

slowOpThresholdMs:100

mode:off

sharding:

clusterRole:
configsvr

启动加入到rc.local中

echo“/data/mongodb/bin/mongod-f
/data/mongodb/etc/mongodb.cnf--rest”
>>/etc/rc.local

9.mongos配置

mongos1

192.168.20.137

mongos2

192.168.20.138

mongodb.cnf

systemLog:

quiet:false

path:/data/mongodb/logs/mongod.log

logAppend:false

destination:file

processManagement:

fork:true

pidFilePath:/data/mongodb/run/mongod.pid

net:

bindIp:0.0.0.0

port:27017

maxIncomingConnections:65536

wireObjectCheck:true

ipv6:false

sharding:

configDB:
192.168.20.139:27019,192.168.20.129:27019,192.168.20.128:27019

启动

/data/mongodb/bin/mongos-f/data/mongodb/etc/mongodb.cnf

10.配置sharding

把replicasets2个副本集群,加入到sharding中

连接mongos服务器进行配置(任意)

[root@shard2
bin]#./mongo--host192.168.20.137:27017

mongos>sh.help()

mongos>
sh.help()

sh.addShard(host)server:portOR
setname/server:port

sh.enableSharding(dbname)enablesshardingonthe
databasedbname

sh.shardCollection(fullName,key,unique)shardsthecollection

sh.splitFind(fullName,find)splitsthechunkthatfindisin
atthemedian

sh.splitAt(fullName,middle)splitsthechunkthatmiddleis
inatmiddle

sh.moveChunk(fullName,find,to)movethechunkwhere'find'isto
'to'(nameofshard)

sh.setBalancerState(<boolonor
not>)turnsthebalanceronoroff
true=on,false=off

sh.getBalancerState()returntrueifenabled

sh.isBalancerRunning()returntrueifthebalancer
hasworkinprogressonanymongos

sh.disableBalancing(coll)disablebalancingonone
collection

sh.enableBalancing(coll)re-enablebalancingonone
collection

sh.addShardTag(shard,tag)addsthetagtotheshard

sh.removeShardTag(shard,tag)removesthetagfromtheshard

sh.addTagRange(fullName,min,max,tag)tagsthespecifiedrangeofthegiven
collection

sh.removeTagRange(fullName,min,max,tag)removesthetaggedrangeofthegiven
collection

sh.status()printsageneraloverviewof
thecluster

注意:
把mongod_shard1_im_test和
mongod_shard2_im_test加入到集群中,副本集中的其他机器自动加入了。添加replica
set作为分片:

mongos>
sh.addShard("mongod_shard1_im_test/192.168.20.127:27017")

{"shardAdded":
"mongod_shard1_im_test","ok":1}

mongos>
sh.addShard("mongod_shard2_im_test/192.168.20.120:27017")

{"shardAdded":
"mongod_shard2_im_test","ok":1}

通过sh.status()查看

mongos>sh.status()

---ShardingStatus---

shardingversion:{

"_id"
:1,

"minCompatibleVersion"
:5,

"currentVersion"
:6,

"clusterId"
:ObjectId("57e0d5c4eb504a0629599e18")

}

shards:

{"_id":
"mongod_shard1_im_test",
"host":
"mongod_shard1_im_test/192.168.20.125:27017,192.168.20.126:27017,192.168.20.127:27017"
}

{"_id":"mongod_shard2_im_test","host":
"mongod_shard2_im_test/192.168.20.120:27017,192.168.20.121:27017,192.168.20.122:27017"
}

activemongoses:

"3.2.9"
:2

balancer:

Currently
enabled:yes

Currently
running:no

Failed
balancerroundsinlast5attempts:0

Migration
Resultsforthelast24hours:

No
recentmigrations

databases:

11.数据库启动分区功能,默认,不启动,都回保存到主sharding上

testdb启动Sharding功能

mongos>sh.enableSharding("testdb")

{"ok":1}

12.对collections进行顺序分区

一个数据库中有多个collections,未必所有的collection都分区,

要想让collections使用分区功能,必须使用sh.sharCollection功能简单,明确说明对那个表进行分区,并

指明分区的key

例:对testdb.sutable(全名),name和Age进行分区

mongos>
sh.shardCollection("testdb.sutable",{age:1,name:1})

建立testdb数据库,并且对sutable表插入数据

usetestdb

for(i=1;i<1000;i++)db.sutable.insert({name:"User"+i,age:i,sex:
"boy",book:["book1","book2"]})

查看分片状态

mongos>useadmin

mongos>
db.runCommand({listshards:1})




删除一个sharding:


db.runCommand({"removeshard"
:"mongod_shard1_im_test/192.168.20.125:27017,192.168.20.126:27017,192.168.20.127:27017"});




补充:对有数据的数据进行分片

usetest
for(i=50000;i<60000;i++)db.messages.insert({"packType":i,"fromName":i,"toName":"boy",roomId:i,"creationDate":i,"stanza":i,"deleteFlag":i,"day":i})

需要手动创建索引
db.messages.ensureIndex({"roomId":1,"creationDate":1,"deleteFlag":1})

指出对那个表的key进行分区
sh.shardCollection("test.messages",{roomId:1,creationDate:1,deleteFlag:1})

删除索引
#db.messages.dropIndex({"roomId":1})

写入数据
for(i=50000;i<60000;i++)db.messages.insert({"packType":i,"fromName":i,"toName":"boy",roomId:i,"creationDate":i,"stanza":i,"deleteFlag":i,"day":i})

查看
sh.status()

对没有数据的数据库进行分片准备:

sh.enableSharding("im_message_db")
sh.shardCollection("im_message_db.messages",{roomId:1,creationDate:1,deleteFlag:1})

useim_message_db

for(i=1;i<6000;i++)db.messages.insert({"packType":i,"fromName":i,"toName":"boy",roomId:i,"creationDate":i,"stanza":i,"deleteFlag":i,"day":i})
sh.status()

此方法,不用对collection建立索引。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: