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

mongodb3.2.8高可用集群安装

2017-04-08 17:47 211 查看
mongodb3.2.8高可用集群安装

15个实例(3台主机)
192.168.175.29 192.168.175.30 192.168.175.35
mongos1 mongos2 mongos3
config server config server config server
shard1 shard1副本 shard1仲裁
shard2 shard2副本 shard2仲裁
shard3 shard3副本 shard3仲裁

端口设置 mongos为 20000, config server 为 21000, shard1为 22001 , shard2为22002, shard3为22003

前期环境:
下载mongodb-linux-x86_64-3.2.8.tgz
下载地址为官网:https://www.mongodb.org/dl/linux/x86_64
总共需要配置15个实例:
我给你看一个实例的大致路径的配置文件,其他的类似:请看如下图



官方文档:https://docs.mongodb.com/manual/sharding/

(1)configdb配置如下:

config server1配置
port=21000
bind_ip=192.168.175.29
configsvr=true
dbpath=/mongodb/config/21000/db
logpath=/mongodb/config/21000/log/config.log

config server2配置
port=21001
bind_ip=192.168.175.30
configsvr=true
dbpath=/mongodb/config/21001/db
logpath=/mongodb/config/21001/log/config.log

config server3配置
port=21002
bind_ip=192.168.175.35
configsvr=true
dbpath=/mongodb/config/21002/db
logpath=/mongodb/config/21002/log/config.log

(2)mongos配置如下:

mongos1配置
port=27000
bind_ip=192.168.175.29
configdb=192.168.175.29:21000,192.168.175.30:21001,192.168.175.35:21002
logpath=/mongodb/mongos/27000/log/mongos.log

mongos2配置
port=27001
bind_ip=192.168.175.30
configdb=192.168.175.29:21000,192.168.175.30:21001,192.168.175.35:21002
logpath=/mongodb/mongos/27001/log/mongos.log

mongos3配置
port=27002
bind_ip=192.168.175.35
configdb=192.168.175.29:21000,192.168.175.30:21001,192.168.175.35:21002
logpath=/mongodb/mongos/27002/log/mongos.log

(3)shard配置如下
1、shard1配置如下 其中(repliset名称为 replciation1)
配置文件1
port=22001
bind_ip=192.168.175.29
dbpath=/mongodb/replciation1/22001/db
replSet=replciation1
logpath=/mongodb/replciation1/22001/log/shard.log

配置文件2
port=22001
bind_ip=192.168.175.30
dbpath=/mongodb/replciation1/22001/db
replSet=replciation1
logpath=/mongodb/replciation1/22001/log/shard.log

配置文件3
port=22001
bind_ip=192.168.175.35
dbpath=/mongodb/replciation1/22001/db
replSet=replciation1
logpath=/mongodb/replciation1/22001/log/shard.log

2、shard2配置如下 其中(repliset名称为 replciation1)
配置文件1
port=22002
bind_ip=192.168.175.29
dbpath=/mongodb/replciation2/22002/db
replSet=replciation2
logpath=/mongodb/replciation2/22002/log/shard.log

配置文件2
port=22002
bind_ip=192.168.175.30
dbpath=/mongodb/replciation2/22002/db
replSet=replciation2
logpath=/mongodb/replciation2/22002/log/shard.log

配置文件3
port=22002
bind_ip=192.168.175.35
dbpath=/mongodb/replciation2/22002/db
replSet=replciation2
logpath=/mongodb/replciation2/22002/log/shard.log

3、shard3配置如下 其中(repliset名称为 replciation3)
配置文件1
port=22003
bind_ip=192.168.175.29
dbpath=/mongodb/replciation3/22003/db
replSet=replciation3
logpath=/mongodb/replciation3/22003/log/shard.log

配置文件2
port=22003
bind_ip=192.168.175.30
dbpath=/mongodb/replciation3/22003/db
replSet=replciation3
logpath=/mongodb/replciation3/22003/log/shard.log

配置文件3
port=22003
bind_ip=192.168.175.35
dbpath=/mongodb/replciation3/22003/db
replSet=replciation3
logpath=/mongodb/replciation3/22003/log/shard.log

(4)启动全部实例

1、分别开启configdb实例
192.168.175.29上面
/mongodb/config/21000/bin/mongod --config=/mongodb/config/21000/1.config &

192.168.175.30上面
/mongodb/config/21001/bin/mongod --config=/mongodb/config/21001/1.config &

192.168.175.35上面
/mongodb/config/21002/bin/mongod --config=/mongodb/config/21002/1.config &

2、分别开启mongos实例

192.168.175.29上面
/mongodb/mongos/27000/bin/mongos --config=/mongodb/mongos/27000/1.config &

192.168.175.30上面
/mongodb/mongos/27001/bin/mongos --config=/mongodb/mongos/27001/1.config &

192.168.175.35上面
/mongodb/mongos/27002/bin/mongos --config=/mongodb/mongos/27002/1.config &

2、分别开启shard实例

启动shard1的3个实例
192.168.175.29
/mongodb/replciation1/22001/bin/mongod --config=/mongodb/replciation1/22001/1.config &

192.168.175.30
/mongodb/replciation1/22001/bin/mongod --config=/mongodb/replciation1/22001/1.config &

192.168.175.35
/mongodb/replciation1/22001/bin/mongod --config=/mongodb/replciation1/22001/1.config &

启动shard2的3个实例
192.168.175.29
/mongodb/replciation2/22002/bin/mongod --config=/mongodb/replciation2/22002/1.config &

192.168.175.30
/mongodb/replciation2/22002/bin/mongod --config=/mongodb/replciation2/22002/1.config &

192.168.175.35
/mongodb/replciation2/22002/bin/mongod --config=/mongodb/replciation2/22002/1.config &

启动shard2的3个实例
192.168.175.29
/mongodb/replciation3/22003/bin/mongod --config=/mongodb/replciation3/22003/1.config &

192.168.175.30
/mongodb/replciation3/22003/bin/mongod --config=/mongodb/replciation3/22003/1.config &

192.168.175.35
/mongodb/replciation3/22003/bin/mongod --config=/mongodb/replciation3/22003/1.config &

(5)分别设置 replication1、replication2、replication3 set集合
连接robomongo1.0 RC1操作如下命令

登录192.168.175.29:22001
replication1 set:
rs.initiate()
rs.config()
rs.add("192.168.175.29:22001")
rs.add("192.168.175.30:22001")
rs.addArb("192.168.175.35:22001")
rs.status()

登录192.168.175.29:22002
replication2 set:
rs.initiate()
rs.config()
rs.add("192.168.175.29:22002")
rs.add("192.168.175.30:22002")
rs.addArb("192.168.175.35:22002")
rs.status()

登录192.168.175.29:22003
replication3 set:
rs.initiate()
rs.config()
rs.add("192.168.175.29:22003")
rs.add("192.168.175.30:22003")
rs.addArb("192.168.175.35:22003")
rs.status()

其中一个replication信息如下:
/* 1 */
{
"set" : "replciation3",
"date" : ISODate("2017-04-08T07:05:38.625Z"),
"myState" : 1,
"term" : NumberLong(1),
"heartbeatIntervalMillis" : NumberLong(2000),
"members" : [
{
"_id" : 0,
"name" : "192.168.175.29:22003",
"health" : 1.0,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1353629,
"optime" : {
"ts" : Timestamp(6406524092324773, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-04-08T07:05:28.000Z"),
"electionTime" : Timestamp(6406522928388636, 2),
"electionDate" : ISODate("2017-04-08T07:00:57.000Z"),
"configVersion" : 3,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.175.30:22003",
"health" : 1.0,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 20,
"optime" : {
"ts" : Timestamp(6406524092324773, 1),
"t" : NumberLong(1)
},
"optimeDate" : ISODate("2017-04-08T07:05:28.000Z"),
"lastHeartbeat" : ISODate("2017-04-08T07:05:38.184Z"),
"lastHeartbeatRecv" : ISODate("2017-04-08T07:05:37.184Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "192.168.175.29:22003",
"configVersion" : 3
},
{
"_id" : 2,
"name" : "192.168.175.35:22003",
"health" : 1.0,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 10,
"lastHeartbeat" : ISODate("2017-04-08T07:05:38.184Z"),
"lastHeartbeatRecv" : ISODate("2017-04-08T07:05:38.296Z"),
"pingMs" : NumberLong(0),
"configVersion" : 3
}
],
"ok" : 1.0
}

(6)添加分片
登录任意一台mongos服务器,将mongod进程添加到sharding中
sh.addShard("replciation1/192.168.175.29:22001,192.168.175.30:22001,192.168.175.35:22001");
sh.addShard("replciation2/192.168.175.29:22002,192.168.175.30:22002,192.168.175.35:22002");
sh.addShard("replciation3/192.168.175.29:22003,192.168.175.30:22003,192.168.175.35:22003");

查看分片情况:
db.getCollection('shards').find({})
db.getCollection('shards').find({})
/* 1 */
{
"_id" : "replciation1",
"host" : "replciation1/192.168.175.29:22001,192.168.175.30:22001"
}

/* 2 */
{
"_id" : "replciation2",
"host" : "replciation2/192.168.175.29:22002,192.168.175.30:22002"
}

/* 3 */
{
"_id" : "replciation3",
"host" : "replciation3/192.168.175.29:22003,192.168.175.30:22003"
}


或者查看信息:
sh.status()

--- Sharding Status ---
sharding version: {
"_id" : 1,
"minCompatibleVersion" : 5,
"currentVersion" : 6,
"clusterId" : ObjectId("58e88672948c5228f8ecf12d")
}
shards:
{ "_id" : "replciation1", "host" : "replciation1/192.168.175.29:22001,192.168.175.30:22001" }
{ "_id" : "replciation2", "host" : "replciation2/192.168.175.29:22002,192.168.175.30:22002" }
{ "_id" : "replciation3", "host" : "replciation3/192.168.175.29:22003,192.168.175.30:22003" }
active mongoses:
"3.2.8" : 3
balancer:
Currently enabled: yes
Currently running: no
Failed balancer rounds in last 5 attempts: 0
Migration Results for the last 24 hours:
No recent migrations
databases:

(7)测试
对一个数据库继续分片

sh.enableSharding("test");
sh.shardCollection("test.Log", { id: 1});

for(var i = 1; i <= 1000; i++){
db.Log.save({id:i,"message":"a"+i});
}

db.Log.stats();
统计信息如下:截取部分数据
"shards" : {
"replciation1" : {
"ns" : "test.Log",
"count" : 1,
"size" : 50,
"avgObjSize" : 50,
"storageSize" : 24576,

"replciation2" : {
"ns" : "test.Log",
"count" : 981,
"size" : 50933,
"replciation3" : {
"ns" : "test.Log",
"count" : 18,
"size" : 910,
"avgObjSize" : 50,
"storageSize" : 24576,

可以看到分片的数据不均,这是由于mongodb的分片策略决定的
下面按照hash进行分片。

sh.enableSharding("ctrip");
sh.shardCollection("ctrip.person", {id:"hashed"});

for(var i = 1; i <= 1000; i++){
db.person.save({id:i,"name":"jack"+i});
}

db.person.stats();
统计信息如下:
"shards" : {
"replciation1" : {
"ns" : "ctrip.person",
"count" : 310,
"size" : 16095,
"avgObjSize" : 51,
"storageSize" : 20480,
"replciation2" : {
"ns" : "ctrip.person",
"count" : 346,
"size" : 17949,
"avgObjSize" : 51,
"storageSize" : 20480,
"replciation3" : {
"ns" : "ctrip.person",
"count" : 344,
"size" : 17849,
"avgObjSize" : 51,
"storageSize" : 20480,
如下图:



结论:hashed分片后数据均匀了!!!
到这里已经安装完成了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  集群 高可用 mongodb