最近在做一个关于车险的项目,由于数据量较大,实验室的Boss决定采用HBase+ES/MongoDB这两种方案,并做性能对比,本人负责MongoDB方案。为了满足海量数据的存储要求,需要搭建一个分布式MongoDB集群。在搭建整过集群的过程中,可谓是各种坑,多次无奈得想放弃,然而在最后一次居然成功了!下面就是MongoDB集群搭建过程的详细步骤:
一、准备6个节点,每个节点的系统都为Ubantu 12.04,内存16G,硬盘500G,节点IP分别为
59.77.236.113 (mongo1),59.77.236.16(mongo2),59.77.236.19(mongo3),218.193.126.107(mongo4),218.193.126.118(mongo5),218.193.126.29(mongo6)
二、mongodb安装包下载
到https://www.mongodb.org/dl/linux/x86_64下载mongodb-linux-x86_64-v3.2-latest.gz
三、建数据存储目录
(1)在59.77.236.113 ,59.77.236.16,59.77.236.19三个节点上建立mongos日志存储目录
sudo mkdir -p /data/data1/mongodb/mongos/log
sudo chown -R mongo:mongo /data
(2)在59.77.236.113 ,59.77.236.16,59.77.236.19三个节点上建立config server 数据文件和日志文件存放目录
sudo mkdir -p /data/data1/mongodb/configServer/db
sudo chown -R mongo:mongo /data
sudo mkdir -p /data/data1/mongodb/configServer/log
sudo chown -R mongo:mongo /data
(3)在59.77.236.16,59.77.236.19,218.193.126.107三个节点上建立shard1 数据文件和日志文件存放目录
mkdir -p /data/data1/mongodb/shard1/db
mkdir -p /data/data1/mongodb/shard1/log
(4)在59.77.236.19,218.193.126.107,218.193.126.118三个节点上建立shard2 数据文件和日志文件存放目录
mkdir -p /data/data1/mongodb/shard2/db
mkdir -p /data/data1/mongodb/shard2/log
(5)在218.193.126.107,218.193.126.118,218.193.126.29三个节点上建立shard3 数据文件和日志文件存放目录
mkdir -p /data/data1/mongodb/shard3/db
mkdir -p /data/data1/mongodb/shard3/log
(6)在218.193.126.118,218.193.126.29,59.77.236.19三个节点上建立shard4数据文件和日志文件存放目录
mkdir -p /data/data1/mongodb/shard4/db
mkdir -p /data/data1/mongodb/shard4/log
四、在每一台服务器分别启动配置服务器(集群分3个config服务器[113/16/19],3个mongos服务器[113/16/19],4个分片,每个分片:主+副+仲裁)(3.2 config要采用replset 部署 –directoryperdb)
(1)config服务器副本集
/home/mongodb-3.2/bin/mongod –configsvr –replSet cfgRepset –port 26100 –fork –storageEngine wiredTiger –wiredTigerCacheSizeGB 4 — directoryperdb –dbpath /data/data1/mongodb/configServer/db –logpath /data/data1/mongodb/configServer/log/mongodb.log
(2)分片服务器
/home/mongodb-3.2/bin/mongod –shardsvr –replSet shard1 –port 26001 –dbpath /data/data1/mongodb/shard1/db –logpath /data/data1/mongodb/shard1/log/mongodb.log –fork –storageEngine wiredTiger –wiredTigerCacheSizeGB 4 –directoryperdb
/home/mongodb-3.2/bin/mongod –shardsvr –replSet shard2 –port 26002 –dbpath /data/data1/mongodb/shard2/db — logpath/data/data1/mongodb/shard2/log/mongodb.log –fork –storageEngine wiredTiger –wiredTigerCacheSizeGB 4 –directoryperdb
/home/mongodb-3.2/bin/mongod –shardsvr –replSet shard3 –port 26003 –dbpath /data/data1/mongodb/shard3/db – – logpath/data/data1/mongodb/shard3/log/mongodb.log –fork –storageEngine wiredTiger –wiredTigerCacheSizeGB 4 –directoryperdb
/home/mongodb-3.2/bin/mongod –shardsvr –replSet shard4 –port 26004 –dbpath /data/data1/mongodb/shard4/db –l logpath/data/data1/mongodb/shard4/log/mongodb.log –fork –storageEngine wiredTiger –wiredTigerCacheSizeGB 4 –directoryperdb
注意:–storageEngine wiredTiger –wiredTigerCacheSizeGB 4 :此处存储大小设置应根据节点的内存大小和副本集的分布情况具体配置。
(3)mongos服务器
/home/mongodb-3.2/bin/mongos –configdb mongodb1:26100,mongodb2:26100,mongodb3:26100 –port 26000 — logpath /data/data1/mongodb/mongos/log/mongos.log –fork
配置文件方式启动(就是把上面步骤(1),(2),(3)中的配置参数写到指定的文件中,然后启动对应的文件):
/home/mongodb-3.2/bin/mongod -f /data/data1/mongodb/configServer/configServer.conf
/home/mongodb-3.2/bin/mongos -f /data/data1/mongodb/mongos/mongos.conf
/home/mongodb-3.2/bin/mongod -f /data/data1/mongodb/shard1/mongodb.conf
/home/mongodb-3.2/bin/mongod -f /data/data1/mongodb/shard2/mongodb.conf
/home/mongodb-3.2/bin/mongod -f /data/data1/mongodb/shard3/mongodb.conf
/home/mongodb-3.2/bin/mongod -f /data/data1/mongodb/shard4/mongodb.conf
四、为每个分片配置主、副本、仲裁
(1)./mongo admin –port 26100 –eval “rs.status()”
(2)为config服务器配置副本
rs.initiate({ _id:”cfgRepset”, members:[
{_id:0,host:”hadoop238:26100″},
{_id:1,host:”hadoop239:26100″},
{_id:2,host:”hadoop240:26100″}]
});
rs.status();
(3)设置第一个分片副本集 // 59.77.236.16:26001,59.77.236.19:26001,218.193.126.107:26001
/home/mongodb-3.2/bin/mongo 59.77.236.16:26001
use admin
rs.initiate(
{ _id:”shard1″, members:[
{_id:0,host:”59.77.236.16:26001″},
{_id:1,host:”59.77.236.19:26001″},
{_id:2,host:”218.193.126.107:26001″,arbiterOnly:true}
]
}
);
rs.status();
(4)设置第二个分片副本集//59.77.236.19:26002,218.193.126.107:26002,218.193.126.118:26002
/home/mongodb-3.2/bin/mongo 59.77.236.19:26002
rs.initiate({ _id:”shard2″, members:[
{_id:0,host:”59.77.236.19:26002″},
{_id:1,host:”218.193.126.107:26002″},
{_id:2,host:”218.193.126.118:26002″,arbiterOnly:true}
]
});
rs.status();
(5)设置第三个分片副本集//218.193.126.107:26003,218.193.126.118:26003,218.193.126.29:26003
/home/mongodb-3.2/bin/mongo 218.193.126.107:26003
rs.initiate({ _id:”shard3″, members:[
{_id:0,host:”218.193.126.107:26003″},
{_id:1,host:”218.193.126.118:26003″},
{_id:2,host:”218.193.126.29:26003″,arbiterOnly:true}
]
});
(6)设置第四个分片副本集//218.193.126.118:26004,218.193.126.29:26004,59.77.236.19:26004
/home/mongodb-3.2/bin/mongo 218.193.126.118:26004
rs.initiate({ _id:”shard4″, members:[
{_id:0,host:”218.193.126.118:26004″},
{_id:1,host:”218.193.126.29:26004″},
{_id:2,host:”59.77.236.19:26004″,arbiterOnly:true}
]
});
(7)连接mongos //59.77.236.113:26100 ,59.77.236.16:26100,59.77.236.19:26100,在59.77.236.113上启动
/home/mongodb-3.2/bin/mongo 59.77.236.113:26000
use admin
(8)串联路由服务器与分配副本集1,2,3,4
db.runCommand( { addshard : “shard1/59.77.236.16:26001,59.77.236.19:26001,218.193.126.107:26001”});
db.runCommand( { addshard : “shard2/59.77.236.19:26002,218.193.126.107:26002,218.193.126.118:26002”});
db.runCommand( { addshard : “shard3/218.193.126.107:26003,218.193.126.118:26003,218.193.126.29:26003”});
db.runCommand( { addshard : “shard4/218.193.126.118:26004,218.193.126.29:26004,59.77.236.19:26004”});
db.runCommand( { listshards : 1 } );
./mongo admin –port 26000 –eval “db.runCommand( { listshards : 1 } );”
五、测试
(1)激活数据库分片,指定testdb分片生效,即表示testdb中的不同集可以放到不同的shard中。叫sharded db
db.runCommand( { enablesharding :”testdb”}); 或 sh.enableSharding(“wis”)
(2)激活数据库里Collecton分片,指定分片的集合和片键,即表示集合内数据可以放不同shard,不然只能放一个shard中。叫 sharded collection
db.runCommand( { shardcollection : “testdb.table1”,key : {id: 1} } ) 或 sh.shardCollection(‘wis.test’,{id:1}
(3)连接mongos服务器
/home/mongodb-3.2/bin/mongo 59.77.236.113:26000
(4)使用testdb
use testdb;
(5)插入测试数据
for (var i = 1; i <= 100000; i++) db.table1.save({id:i,”test1″:”testval1″});
(6)查看分片情况如下,部分无关信息省掉了
db.table1.stats();