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

安装mongodb副本分片集群

2016-02-04 15:54 751 查看
一.mongodb安装配置

tar xzvf mongodb-linux-x86_64-rhel62-3.0.7.tgz
mkdir -p /opt/hostkeeper/mongodb
rm -rf /opt/hostkeeper/mongodb
mv mongodb-linux-x86_64-rhel62-3.0.7 /opt/hostkeeper/mongodb
mkdir -p /data/hostkeeper/mongodb/mongos/log
mkdir -p /data/hostkeeper/mongodb/config/data
mkdir -p /data/hostkeeper/mongodb/config/log
mkdir -p /data/hostkeeper/mongodb/shard1/data
mkdir -p /data/hostkeeper/mongodb/shard1/log
mkdir -p /data/hostkeeper/mongodb/shard2/data
mkdir -p /data/hostkeeper/mongodb/shard2/log
mkdir -p /data/hostkeeper/mongodb/shard3/data
mkdir -p /data/hostkeeper/mongodb/shard3/log
chown hostkeeper:acfunadmin -R /opt/hostkeeper
chown hostkeeper:acfunadmin -R /data/hostkeeper

二.分别在master、slave1和slave2服务器上启动配置服务器

numactl --interleave=all /opt/hostkeeper/mongodb/bin/mongod --configsvr --dbpath /data/hostkeeper/mongodb/config/data/ --httpinterface --rest --port 21000 --logpath /data/hostkeeper/mongodb/config/log/config.log --fork

三.分别在master、slave1和slave2服务器上启动mongos服务器

numactl --interleave=all /opt/hostkeeper/mongodb/bin/mongos  --configdb 10.0.0.45:21000,10.0.0.57:21000,10.0.0.58:21000 --port 20000 --logpath /data/hostkeeper/mongodb/mongos/log/mongos.log --fork

四.分别在master、slave1和slave2服务器上配置分片副本集

numactl --interleave=all /opt/hostkeeper/mongodb/bin/mongod --shardsvr --replSet shard1 --port 22001 --dbpath /data/hostkeeper/mongodb/shard1/data  --logpath /data/hostkeeper/mongodb/shard1/log/shard.log --fork --nojournal  --oplogSize 10
numactl --interleave=all /opt/hostkeeper/mongodb/bin/mongod --shardsvr --replSet shard2 --port 22002 --dbpath /data/hostkeeper/mongodb/shard2/data  --logpath /data/hostkeeper/mongodb/shard2/log/shard.log --fork --nojournal  --oplogSize 10
numactl --interleave=all /opt/hostkeeper/mongodb/bin/mongod --shardsvr --replSet shard3 --port 22003 --dbpath /data/hostkeeper/mongodb/shard3/data  --logpath /data/hostkeeper/mongodb/shard3/log/shard.log --fork --nojournal  --oplogSize 10

五.设置副本集,必须使用admin数据库

1.设置第1个分片副本集
/opt/hostkeeper/mongodb/bin/mongo 10.0.0.45:22001/admin
config = { _id:"shard1", members:[
{_id:0,host:"10.0.0.45:22001",priority:1},
{_id:1,host:"10.0.0.57:22001",priority:2},
{_id:2,host:"10.0.0.58:22001",arbiterOnly:true}
]
}

rs.initiate(config);
exit

2.设置第2个分片副本集

/opt/hostkeeper/mongodb/bin/mongo 10.0.0.57:22002/admin
config = { _id:"shard2", members:[
{_id:0,host:"10.0.0.45:22002",arbiterOnly:true},
{_id:1,host:"10.0.0.57:22002",priority:1},
{_id:2,host:"10.0.0.58:22002",priority:2}
]
}

rs.initiate(config);
exit

3.设置第3个分片副本集

/opt/hostkeeper/mongodb/bin/mongo 10.0.0.58:22003/admin
config = { _id:"shard3", members:[
{_id:0,host:"10.0.0.45:22003",priority:2},
{_id:1,host:"10.0.0.57:22003",arbiterOnly:true},
{_id:2,host:"10.0.0.58:22003",priority:1}
]
}

 rs.initiate(config);
exit

六.串联路由服务器与分配副本集

/opt/hostkeeper/mongodb/bin/mongo 10.0.0.45:20000/admin
#串联路由服务器与分配副本集1
db.runCommand( { addshard : "shard1/10.0.0.45:22001,10.0.0.57:22001,10.0.0.58:22001"});
#串联路由服务器与分配副本集2
db.runCommand( { addshard : "shard2/10.0.0.45:22002,10.0.0.57:22002,10.0.0.58:22002"});
#串联路由服务器与分配副本集3
db.runCommand( { addshard : "shard3/10.0.0.45:22003,10.0.0.57:22003,10.0.0.58:22003"});

七.分片测试

/opt/hostkeeper/mongodb/bin/mongo 10.0.0.45:20000/admin
#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定数据库里需要分片的集合和片键
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
#使用testdb
use  testdb;
#插入测试数据
for (var i = 1; i <= 100000; i++) db.table1.save({id:i,"test1":"testval1"});
#查看分片情况如下
db.table1.stats();

八.web监控页面

http://10.0.0.45:22000[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: