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

mongodb分片

2016-03-02 16:53 645 查看
新上的售后项目,领导要求使用mongodb分片,先进行下测试。一台服务器上,3个路由节点+3个配置节点+4个副本集(每个副本集包括1主+1从+1仲裁),共18个节点。

1、解压

tar zxf mongodb-linux-x86_64-rhel62-3.2.1.tgz
cd mongodb-linux-x86_64-rhel62-3.2.1
mkdir /usr/local/mongodb
cp -r * /usr/local/mongodb/
cd /usr/local/mongodb
2、先启动配置节点

./bin/mongod --configsvr -dbpath ./27021/ --port 27021 --logpath ./log/27021.log --fork
./bin/mongod --configsvr -dbpath ./27022/ --port 27022 --logpath ./log/27022.log --fork
./bin/mongod --configsvr -dbpath ./27023/ --port 27023 --logpath ./log/27023.log --fork
3、启动路由节点

./bin/mongos  --configdb 10.128.11.90:27021,10.128.11.90:27022,10.128.11.90:27023 --port 27018 --logpath ./log/27018.log --fork
./bin/mongos  --configdb 10.128.11.90:27021,10.128.11.90:27022,10.128.11.90:27023 --port 27019 --logpath ./log/27019.log --fork
./bin/mongos  --configdb 10.128.11.90:27021,10.128.11.90:27022,10.128.11.90:27023 --port 27020 --logpath ./log/27020.log --fork
4、启动数据节点

./bin/mongod --shardsvr --replSet shard1 --port 27024 --dbpath ./27024  --logpath ./log/27024.log --fork
./bin/mongod --shardsvr --replSet shard1 --port 27025 --dbpath ./27025  --logpath ./log/27025.log --fork
./bin/mongod --shardsvr --replSet shard1 --port 27026 --dbpath ./27026  --logpath ./log/27026.log --fork

./bin/mongod --shardsvr --replSet shard2 --port 27027 --dbpath ./27027  --logpath ./log/27027.log --fork
./bin/mongod --shardsvr --replSet shard2 --port 27028 --dbpath ./27028  --logpath ./log/27028.log --fork
./bin/mongod --shardsvr --replSet shard2 --port 27029 --dbpath ./27029  --logpath ./log/27029.log --fork

./bin/mongod --shardsvr --replSet shard3 --port 27030 --dbpath ./27030  --logpath ./log/27030.log --fork
./bin/mongod --shardsvr --replSet shard3 --port 27031 --dbpath ./27031  --logpath ./log/27031.log --fork
./bin/mongod --shardsvr --replSet shard3 --port 27032 --dbpath ./27032  --logpath ./log/27032.log --fork

./bin/mongod --shardsvr --replSet shard4 --port 27033 --dbpath ./27033  --logpath ./log/27033.log --fork
./bin/mongod --shardsvr --replSet shard4 --port 27034 --dbpath ./27034  --logpath ./log/27034.log --fork
./bin/mongod --shardsvr --replSet shard4 --port 27035 --dbpath ./27035  --logpath ./log/27035.log --fork
5、配置副本集

./bin/mongo  10.128.11.90:27024/admin
config = { _id:"shard1", members:[
{_id:0,host:"10.128.11.90:27024"},
{_id:1,host:"10.128.11.90:27025"},
{_id:2,host:"10.128.11.90:27026",arbiterOnly:true}
]
}
rs.initiate(config);

./bin/mongo  10.128.11.90:27027/admin
config = { _id:"shard2", members:[
{_id:0,host:"10.128.11.90:27027"},
{_id:1,host:"10.128.11.90:27028"},
{_id:2,host:"10.128.11.90:27029",arbiterOnly:true}
]
}
rs.initiate(config);

./bin/mongo  10.128.11.90:27030/admin
config = { _id:"shard3", members:[
{_id:0,host:"10.128.11.90:27030"},
{_id:1,host:"10.128.11.90:27031"},
{_id:2,host:"10.128.11.90:27032",arbiterOnly:true}
]
}
rs.initiate(config);

./bin/mongo  10.128.11.90:27033/admin
config = { _id:"shard4", members:[
{_id:0,host:"10.128.11.90:27033"},
{_id:1,host:"10.128.11.90:27034"},
{_id:2,host:"10.128.11.90:27035",arbiterOnly:true}
]
}
rs.initiate(config);
6、配置分片

./bin/mongo  10.128.11.90:27018/admin
db.runCommand( { addshard : "shard1/10.128.11.90:27024,10.128.11.90:27025,10.128.11.90:27026"});
db.runCommand( { addshard : "shard2/10.128.11.90:27027,10.128.11.90:27028,10.128.11.90:27029"});
db.runCommand( { addshard : "shard3/10.128.11.90:27030,10.128.11.90:27031,10.128.11.90:27032"});
db.runCommand( { addshard : "shard4/10.128.11.90:27033,10.128.11.90:27034,10.128.11.90:27035"});

查看配置的分片
db.runCommand( { listshards : 1 } );
7、测试

以后使用mongodb,只需连接任意路由节点,即使用如下任意一个进行即可
./bin/mongo 10.128.11.90:27018
./bin/mongo 10.128.11.90:27019
./bin/mongo 10.128.11.90:27020

admin下,对xishuai库下的blog集合,以_id为片键开启分片功能
use admin
db.runCommand( { enablesharding :"xishuai"});
db.runCommand( { shardcollection : "xishuai.blog",key : {_id: 1} } );

for (var i = 1; i <= 100; i++)db.blog.insert({id:i,"name":"test"});
db.blog.stats();

8、查看分片情况

use config
查看开启分片的库
db.databases.find()
{ "_id" : "xishuai", "primary" : "shard2", "partitioned" : true }
查看所有分片集合的具体分片情况
db.chunks.find()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: