MongoDB分片是服务器集群配置
1)创建文件夹
/usr/local/server/mongodb/
/usr/local/server/mongodb/config/data/
/usr/local/server/mongodb/config/log/
/usr/local/server/mongodb/mongos/log/
/usr/local/server/mongodb/shard1/data
/usr/local/server/mongodb/shard1/log
/usr/local/server/mongodb/shard2/data
/usr/local/server/mongodb/shard2/log
/usr/local/server/mongodb/shard3/data
/usr/local/server/mongodb/shard3/log
2) 在三台机器上分别启动mongod进程
mongod --shardsvr --replSet shard1 --port 27017 --dbpath /usr/local/server/mongodb/shard1/data/ --oplogSize 100 --logpath /usr/local/server/mongodb/shard1/log/shard1.log --logappend --fork --rest
mongod --shardsvr --replSet shard2 --port 27018 --dbpath /usr/local/server/mongodb/shard2/data/ --oplogSize 100 --logpath /usr/local/server/mongodb/shard1/log/shard2.log --logappend --fork --rest
mongod --shardsvr --replSet shard3 --port 27019 --dbpath /usr/local/server/mongodb/shard3/data/ --oplogSize 100 --logpath /usr/local/server/mongodb/shard1/log/shard3.log --logappend --fork --rest
3) 初始化两组Replica Set
确认第2步没有报错之后,我们开始配置Replica Set。 通过mongo连接到shard1的一个mongod:
mongo 192.168.244.128:27017
执行如下命令:
config = {_id: 'shard1', members: [{_id: 0, host: '192.168.244.128:27017'}, {_id: 1, host: '192.168.244.129:27017'}, {_id: 2, host: '192.168.244.130:27017',arbiterOnly:true}]};
rs.initiate(config);
同样方法,配置shard2用到的replica set:
mongo 192.168.244.128:27018
config = {_id: 'shard2', members: [{_id: 0, host: '192.168.244.128:27018'}, {_id: 1, host: '192.168.244.128:27018'}, {_id: 2, host: '192.168.244.130:27018',arbiterOnly:true}]};
rs.initiate(config);
同样方法,配置shard3用到的replica set:
mongo 192.168.244.128:27019
config = {_id: 'shard3', members: [{_id: 0, host: '192.168.244.128:27019'}, {_id: 1, host: '192.168.244.128:27019'}, {_id: 2, host: '192.168.244.130:27019',arbiterOnly:true}]};
rs.initiate(config);
4) 启动Config Server
在三台机器上分别启动并配置一台Config Server。命令如下:
mongod --configsvr --dbpath /usr/local/server/mongodb/config/data/ --port 20000 --logpath /usr/local/server/mongodb/config/log/config.log --logappend --fork
5)启动Routing Server
部署并配置三台Routing Server
mongos --configdb 192.168.244.128:20000,192.168.244.129:20000,192.168.244.130:20000 --port 30000 --chunkSize 100 --logpath /usr/local/server/mongodb/mongos/log/mongos.log --logappend --fork
6)添加分片
连接到mongs服务器,并切换到admin
./bin/mongo 192.168.244.128:30000/admin
db.runCommand({addshard:"shard1/192.168.244.128:27017,192.168.244.129:27017,192.168.244.130:27017",name:"shard1",maxsize:2048, allowLocal:true });
db.runCommand({addshard:"shard2/192.168.244.128:27018,192.168.244.129:27018,192.168.244.130:27018",name:"shard2",maxsize:2048, allowLocal:true });
db.runCommand({addshard:"shard3/192.168.244.128:27019,192.168.244.129:27019,192.168.244.130:27019",name:"shard3",maxsize:2048, allowLocal:true });
db.runCommand( { listshards : 1 } );
7)查看分片服务器的配置
db.runCommand( { listshards : 1 } );
8)目前配置服务、路由服务、分片服务、副本集服务都已经串联起来了,但我们的目的是希望插入数据,数据能够自动分片,就差那么一点点,一点点。。。
连接在mongos上,准备让指定的数据库、指定的集合分片生效。
#指定testdb分片生效
db.runCommand( { enablesharding :"testdb"});
#指定数据库里需要分片的集合和片键
db.runCommand( { shardcollection : "testdb.table1",key : {id: 1} } )
我们设置testdb的 table1 表需要分片,根据 id 自动分片到 shard1 ,shard2,shard3 上面去。要这样设置是因为不是所有mongodb 的数据库和表 都需要分片!
11、测试分片配置结果。
#连接mongos服务器
/data/mongodbtest/mongodb-linux-x86_64-2.4.8/bin/mongo 127.0.0.1:20000
#使用testdb
use testdb;
#插入测试数据
for (var i = 1; i <= 100000; i++)
db.table1.save({id:i,"test1":"testval1"});
#查看分片情况如下,部分无关信息省掉了
db.table1.stats();
来源:https://www.cnblogs.com/Rabon/p/4602310.html