2 19
Mongodb分片部署

参考:

选用服务器:

`192.168.139.139 (主)`

`192.168.139.138`

`192.168.139.134`

在每台服务器上建立MongoDB的软连接

每台机器:

sudo ln -s /data/services/mongodb-linux-x86_64-3.0.7/bin/mongos /usr/bin/mongos

sudo ln -s -f /data/services/mongodb-linux-x86_64-3.0.7/bin/mongos /usr/bin/mongos

sudo ln -s -f /data/services/mongodb-linux-x86_64-3.0.7/bin/mongod /usr/bin/mongod

sudo ln -s -f /data/services/mongodb-linux-x86_64-3.0.7/bin/mongo /usr/bin/mongo

rm -rf conf/ mongos/ shard1 shard2 shard3

mkdir -p conf/data conf/log mongos/log shard{1..3}/data shard{1..3}/log

建立配置服器: start_configsvr.sh 并启动

139: mongod --configsvr --dbpath /home/steve-1/mongo_shards/conf/data --port 27100 --logpath /home/steve-1/mongo_shards/conf/confdb.log --fork --directoryperdb

138: mongod --configsvr --dbpath /home/apps/mongo_shards/conf/data --port 27100 --logpath /home/apps/mongo_shards/conf/confdb.log --fork --directoryperdb

134: mongod --configsvr --dbpath /home/steve-3/mongo_shards/conf/data --port 27100 --logpath /home/steve-3/mongo_shards/conf/confdb.log --fork --directoryperdb

建立分片服务器 start_mongos.sh 并启动

139:
    mongos --configdb 192.168.139.139:27100,192.168.139.138:27100,192.168.139.134:27100 --port 27000 --logpath /home/steve-1/mongo_shards/mongos/mongos.log --fork

138:
    mongos --configdb 192.168.139.139:27100,192.168.139.138:27100,192.168.139.134:27100 --port 27000 --logpath /home/apps/mongo_shards/mongos/mongos.log --fork

134:
    mongos --configdb 192.168.139.139:27100,192.168.139.138:27100,192.168.139.134:27100 --port 27000 --logpath /home/steve-3/mongo_shards/mongos/mongos.log --fork

建立副本集并设置分片 start_repl.sh

139:
    mongod --shardsvr --replSet shard1 --port 27001 --dbpath /home/steve-1/mongo_shards/shard1/data --logpath /home/steve-1/mongo_shards/shard1/log/shard1.log --directoryperdb --fork

    mongod --shardsvr --replSet shard2 --port 27002 --dbpath /home/steve-1/mongo_shards/shard2/data --logpath /home/steve-1/mongo_shards/shard2/log/shard2.log --directoryperdb --fork

    mongod --shardsvr --replSet shard3 --port 27003 --dbpath /home/steve-1/mongo_shards/shard3/data --logpath /home/steve-1/mongo_shards/shard3/log/shard3.log --directoryperdb --fork


138:
    mongod --shardsvr --replSet shard1 --port 27001 --dbpath /home/apps/mongo_shards/shard1/data --logpath /home/apps/mongo_shards/shard1/log/shard1.log --directoryperdb --fork

    mongod --shardsvr --replSet shard2 --port 27002 --dbpath /home/steve-3/mongo_shards/shard2/data --logpath /home/apps/mongo_shards/shard2/log/shard2.log --directoryperdb --fork

    mongod --shardsvr --replSet shard3 --port 27003 --dbpath /home/steve-3/mongo_shards/shard3/data --logpath /home/apps/mongo_shards/shard3/log/shard3.log --directoryperdb --fork


134:
    mongod --shardsvr --replSet shard1 --port 27001 --dbpath /home/steve-3/mongo_shards/shard1/data --logpath /home/steve-3/mongo_shards/shard1/log/shard1.log --directoryperdb --fork

    mongod --shardsvr --replSet shard2 --port 27002 --dbpath /home/steve-3/mongo_shards/shard2/data --logpath /home/steve-3/mongo_shards/shard2/log/shard2.log --directoryperdb --fork

    mongod --shardsvr --replSet shard3 --port 27003 --dbpath /home/steve-3/mongo_shards/shard3/data --logpath /home/steve-3/mongo_shards/shard3/log/shard3.log --directoryperdb --fork

初始化分片服务器

139:

mongo  192.168.139.139:27001

use admin

rs.initiate({_id: 'shard1',members: [{_id: 84, host: '192.168.139.139:27001'},{_id: 89, host: '192.168.139.138:27001'},{_id: 90, host: '192.168.139.134:27001',arbiterOnly: true}]});

mongo  192.168.139.139:27000  —> 却换到mongos

use admin

db.runCommand({addShard: 'shard1/192.168.139.139:27001,192.168.139.138:27001,192.168.139.134:27001'});

138:

mongo  192.168.139.138:27002

use admin

rs.initiate({_id: 'shard2',members: [{_id: 84, host: '192.168.139.139:27002',arbiterOnly: true},{_id: 89, host: '192.168.139.138:27002'},{_id: 90, host: '192.168.139.134:27002'}]});

mongo  192.168.139.138:27000  —> 却换到mongos

use admin

db.runCommand({addShard: 'shard2/192.168.139.139:27002,192.168.139.138:27002,192.168.139.134:27002'});

134:

mongo  192.168.139.134:27003

use admin

rs.initiate({_id: 'shard3',members: [{_id: 84, host: '192.168.139.139:27003'},{_id: 89, host: '192.168.139.138:27003',arbiterOnly: true},{_id: 90, host: '192.168.139.134:27003'}]});

mongo  192.168.139.134:27000  —> 却换到mongos

use admin

db.runCommand({addShard: 'shard3/192.168.139.139:27003,192.168.139.138:27003,192.168.139.134:27003'});

建立测试分片数据库

顺便登陆其中一个服务器:

下面测试分片:

mongo 192.168.139.138:27000

use admin

db.runCommand( { enablesharding :“testdb”});

db.runCommand( { shardcollection : “testdb.table1”,key : {id: 1} } )

mongo 192.168.139.138:27000

use testdb

for (var i = 1; i <= 1000; i++) {db.table1.save({id:i,“test1”:“testval1”});}

//查看分片信息

db.table1.stats();