博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mongodb分片搭建
阅读量:2433 次
发布时间:2019-05-10

本文共 8926 字,大约阅读时间需要 29 分钟。

由于是简单搭建,采用一台虚机配置多个vip的方式。
每个shard是一个复制集,每个复制集的第一个节点是arbiter
节点概览
route0  1.1.1.100:4000 
route1  1.1.1.101:4001 
conf0   1.1.1.110:4010 
conf1   1.1.1.111:4011 
conf2   1.1.1.112:4012
rs0-0   1.1.1.120:4020 
rs0-1   1.1.1.121:4021 
rs0-2   1.1.1.122:4022 
rs1-0   1.1.1.130:4030 
rs1-1   1.1.1.131:4031 
rs1-2   1.1.1.132:4032
rs2-0   1.1.1.140:4040 
rs2-1   1.1.1.141:4041 
rs2-2   1.1.1.142:4042 
rs3-0   1.1.1.150:4050 
rs3-1   1.1.1.151:4051 
rs3-2   1.1.1.152:4052 
vip脚本:
[root@db10 mongo]# cat vip.sh
ifconfig | grep eth0: | awk '{print $1}' | xargs -l -i ifconfig {} down
ifconfig eth0:1  1.1.1.100 netmask 255.255.255.0 up
ifconfig eth0:2  1.1.1.101 netmask 255.255.255.0 up
ifconfig eth0:3  1.1.1.110 netmask 255.255.255.0 up
ifconfig eth0:4  1.1.1.111 netmask 255.255.255.0 up
ifconfig eth0:5  1.1.1.112 netmask 255.255.255.0 up
ifconfig eth0:6  1.1.1.120 netmask 255.255.255.0 up
ifconfig eth0:7  1.1.1.121 netmask 255.255.255.0 up
ifconfig eth0:8  1.1.1.122 netmask 255.255.255.0 up
ifconfig eth0:9  1.1.1.130 netmask 255.255.255.0 up
ifconfig eth0:10 1.1.1.131 netmask 255.255.255.0 up
ifconfig eth0:11 1.1.1.132 netmask 255.255.255.0 up
ifconfig eth0:12 1.1.1.140 netmask 255.255.255.0 up
ifconfig eth0:13 1.1.1.141 netmask 255.255.255.0 up
ifconfig eth0:14 1.1.1.142 netmask 255.255.255.0 up
ifconfig eth0:15 1.1.1.150 netmask 255.255.255.0 up
ifconfig eth0:16 1.1.1.151 netmask 255.255.255.0 up
ifconfig eth0:17 1.1.1.152 netmask 255.255.255.0 up
ifconfig | grep -E "inet addr" | grep -v -E "127|2\.2\.2"
执行脚本
[root@db10 mongo]# ./vip.sh
          inet addr:1.1.1.10  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.100  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.101  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.110  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.111  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.112  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.120  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.121  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.122  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.130  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.131  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.132  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.140  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.141  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.142  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.150  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.151  Bcast:1.1.1.255  Mask:255.255.255.0
          inet addr:1.1.1.152  Bcast:1.1.1.255  Mask:255.255.255.0
检查连通性
[root@db10 mongo]# cat /etc/hosts | grep "1.1.1" | awk {'print $2'} | xargs -l ping -c 1 | grep from
64 bytes from db10 (1.1.1.10): icmp_seq=1 ttl=64 time=0.090 ms
64 bytes from route0 (1.1.1.100): icmp_seq=1 ttl=64 time=0.076 ms
64 bytes from route1 (1.1.1.101): icmp_seq=1 ttl=64 time=0.040 ms
64 bytes from conf0 (1.1.1.110): icmp_seq=1 ttl=64 time=0.020 ms
64 bytes from conf1 (1.1.1.111): icmp_seq=1 ttl=64 time=0.015 ms
64 bytes from conf2 (1.1.1.112): icmp_seq=1 ttl=64 time=0.014 ms
64 bytes from rs0-0 (1.1.1.120): icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from rs0-1 (1.1.1.121): icmp_seq=1 ttl=64 time=0.014 ms
64 bytes from rs0-2 (1.1.1.122): icmp_seq=1 ttl=64 time=0.014 ms
64 bytes from rs1-0 (1.1.1.130): icmp_seq=1 ttl=64 time=0.034 ms
64 bytes from rs1-1 (1.1.1.131): icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from rs1-2 (1.1.1.132): icmp_seq=1 ttl=64 time=0.018 ms
64 bytes from rs2-0 (1.1.1.140): icmp_seq=1 ttl=64 time=0.015 ms
64 bytes from rs2-1 (1.1.1.141): icmp_seq=1 ttl=64 time=0.012 ms
64 bytes from rs1-2 (1.1.1.132): icmp_seq=1 ttl=64 time=0.000 ms
64 bytes from rs3-0 (1.1.1.150): icmp_seq=1 ttl=64 time=0.017 ms
64 bytes from rs3-1 (1.1.1.151): icmp_seq=1 ttl=64 time=0.013 ms
64 bytes from rs3-2 (1.1.1.152): icmp_seq=1 ttl=64 time=0.020 ms
创建数据文件文件夹
[root@db10 mongo]# mkdir conf0 conf1 conf2 rs0_0 rs0_1 rs0_2 rs1_0 rs1_1 rs1_2 rs2_0 rs2_1 rs2_2 rs3_0 rs3_1 rs3_2
config节点参数文件
[root@db10 mongo]# cat conf0.conf
port = 4010
logpath = /opt/mongo/conf0.log
logappend = true
dbpath = /opt/mongo/conf0/
smallfiles = true
启动config节点
nohup mongod --configsvr -f /opt/mongo/conf0.conf &
nohup mongod --configsvr -f /opt/mongo/conf1.conf &
nohup mongod --configsvr -f /opt/mongo/conf2.conf &
路由节点参数文件
[root@db10 mongo]# cat route0.conf
port = 4000
logpath = /opt/mongo/route0.log
logappend = true
configdb = conf0:4010,conf1:4011,conf2:4012
启动路由节点
nohup mongos -f route0.conf &
nohup mongos -f route1.conf &
shard复制集参数文件
(有4个复制集,每个复制集的第一个节点是arbiter,后面的是存储数据的节点)
[root@db10 mongo]# cat rs0-0.conf
smallfiles = true
logappend = true
replSet = rs0
port = 4020
dbpath = /opt/mongo/rs0-0/
logpath = /opt/mongo/rs0-0.log
初始化各复制集
(如果是用一台机器+vip+hosts配机器名的方式做测试,初始化复制集是一定要用下面这种手工制定members[n].host的方式,否则mongo会把当前节点的host设置为机器名,而不是我们制定的rs0-n)
> var config = {_id: 'rs0', members: [
...     {_id: 0, host: 'rs0-0:4020', arbiterOnly: true},
...     {_id: 1, host: 'rs0-1:4021'},
...     {_id: 2, host: 'rs0-2:4022'}
...     ]};
> rs.initiate(config);
{ "ok" : 1 }
rs0:OTHER> rs.status();
{
        "set" : "rs0",
        "date" : ISODate("2015-03-30T06:09:28.166Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "rs0-0:4020",
                        "health" : 1,
                        "state" : 7,
                        "stateStr" : "ARBITER",
                        "uptime" : 7,
                        "lastHeartbeat" : ISODate("2015-03-30T06:09:26.825Z"),
                        "lastHeartbeatRecv" : ISODate("2015-03-30T06:09:26.834Z"),
                        "pingMs" : 1,
                        "configVersion" : 1
                },
                {
                        "_id" : 1,
                        "name" : "rs0-1:4021",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 112,
                        "optime" : Timestamp(1427695760, 1),
                        "optimeDate" : ISODate("2015-03-30T06:09:20Z"),
                        "electionTime" : Timestamp(1427695762, 1),
                        "electionDate" : ISODate("2015-03-30T06:09:22Z"),
                        "configVersion" : 1,
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "rs0-2:4022",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 7,
                        "optime" : Timestamp(1427695760, 1),
                        "optimeDate" : ISODate("2015-03-30T06:09:20Z"),
                        "lastHeartbeat" : ISODate("2015-03-30T06:09:26.825Z"),
                        "lastHeartbeatRecv" : ISODate("2015-03-30T06:09:26.834Z"),
                        "pingMs" : 1,
                        "configVersion" : 1
                }
        ],
        "ok" : 1
}
添加shard
mongos> sh.addShard("rs0/rs0-0:4020,rs0-1:4021,rs0-2:4022");
{ "shardAdded" : "rs0", "ok" : 1 }
mongos> sh.addShard("rs1/rs1-0:4030,rs1-1:4031,rs1-2:4032");
{ "shardAdded" : "rs1", "ok" : 1 }
mongos> sh.addShard("rs2/rs2-0:4040,rs2-1:4041,rs2-2:4042");
{ "shardAdded" : "rs2", "ok" : 1 }
mongos> sh.addShard("rs3/rs3-0:4050,rs3-1:4051,rs3-2:4052");
{ "shardAdded" : "rs3", "ok" : 1 }
isdbgrid表示当前连接的是一个shard cluster
mongos> rs.isMaster()
{
        "ismaster" : true,
         
"msg" : "isdbgrid",
        "maxBsonObjectSize" : 16777216,
        "maxMessageSizeBytes" : 48000000,
        "maxWriteBatchSize" : 1000,
        "localTime" : ISODate("2015-03-30T06:33:04.209Z"),
        "maxWireVersion" : 3,
        "minWireVersion" : 0,
        "ok" : 1
}
创建测试数据
(
为了方便在少量数据下观察,最好把chunksize设成1k
use config
db.settings.save( { _id:"chunksize", value: 1 } )
)
use  
test
sh.
enableSharding("test");
db.createCollection("
doc0");
db.createCollection("
doc1");
db.createCollection("
doc2");
sh.shardCollection("test.doc0", {"int1": 1, "int2": 1});
sh.shardCollection("test.doc1", {"datee": 1});
sh.shardCollection("test.doc2", {"_id": "hashed"});
for(var i = 0; i < 100; i++) {
  for(var j = 0; j < 100; j++) {
    db.
doc0.insert({int1:i, int2:j});
  }
}
for(var i = 0; i < 100; i++) {
  for(var j = 0; j < 100; j++) {
    db.
doc1.insert({datee:ISODate("2015-0"+(i%9+1)+"-0"+(i%9+1)), int1:i, int2:j});
    db.
doc1.insert({datee:ISODate("2015-0"+(i%9+1)+"-"+(i%19+10)), int1:i, int2:j});
    db.
doc1.insert({datee:ISODate("2015-1"+(i%3)+"-0"+(i%9+1)), int1:i, int2:j});
    db.
doc1.insert({datee:ISODate("2015-1"+(i%3)+"-"+(i%19+10)), int1:i, int2:j});
  }
}
for(var i = 0; i < 100; i++) {
  for(var j = 0; j < 100; j++) {
    db.
doc2.insert({int1:i*j});
  }
}
mongos> db.doc0.find().count()
10000
mongos> db.doc1.find().count()
40000
mongos> db.doc2.find().count()
10000
一键启动
[root@db10 mongo]# cat startall
nohup mongod --configsvr -f /opt/mongo/conf0.conf &
nohup mongod --configsvr -f /opt/mongo/conf1.conf &
nohup mongod --configsvr -f /opt/mongo/conf2.conf &
nohup mongod -f /opt/mongo/rs0-0.conf &
nohup mongod -f /opt/mongo/rs0-1.conf &
nohup mongod -f /opt/mongo/rs0-2.conf &
nohup mongod -f /opt/mongo/rs1-0.conf &
nohup mongod -f /opt/mongo/rs1-1.conf &
nohup mongod -f /opt/mongo/rs1-2.conf &
nohup mongod -f /opt/mongo/rs2-0.conf &
nohup mongod -f /opt/mongo/rs2-1.conf &
nohup mongod -f /opt/mongo/rs2-2.conf &
nohup mongod -f /opt/mongo/rs3-0.conf &
nohup mongod -f /opt/mongo/rs3-1.conf &
nohup mongod -f /opt/mongo/rs3-2.conf &
sleep 4
nohup mongos -f route0.conf &
nohup mongos -f route1.conf &
sleep 2
ps -ef| grep -E mongo[sd]
一键关闭
[root@db10 mongo]# cat killall
find . -name mongod.lock -exec cat {} \; | xargs kill
ps -ef| grep mongos | grep -v grep | awk '{print $2}' |xargs kill
sleep 4
ps -ef| grep -E mongo[sd]
检查进程
[root@db10 mongo]# cat check
ps -ef| grep -E mongo[sd]

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/26239116/viewspace-1485434/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/26239116/viewspace-1485434/

你可能感兴趣的文章
关于计算机病毒的免疫性研究 (转)
查看>>
网吧操作系统制作与优化2007最终版(转)
查看>>
用PHP连mysql和oracle数据库性能比较(转)
查看>>
J2ME中文教程之J2ME技术概述(转)
查看>>
SQLServer2005:SQLCLR代码之CLR安全(转)
查看>>
如何禁止改变启动菜单(转)
查看>>
初识 Smart Device Extensions(转)
查看>>
QD入门.symbian手机通用(转)
查看>>
Win2K无盘终端网组建全攻略(6)(转)
查看>>
提高拆分数据库在网上运行、编辑的速度(转)
查看>>
w3 upload组件实例(转)
查看>>
BXP无盘网络方案及设备选用(转)
查看>>
Helloworld项目分析杂记(一)(转)
查看>>
在ASP中判断SQL语句是否执行成功(转)
查看>>
GSM900/1800双频无线网络参数及其调整(转)
查看>>
Oracle新手最常碰到的6个错误及解决方案(转)
查看>>
Delphi 文本编辑器的设计(四)(转)
查看>>
羊群效应:搜索引擎暗规则之四(转)
查看>>
新的MySQL安装的安全性(转)
查看>>
网络目录和搜索引擎有什么区别?(转)
查看>>