加host
vi /etc/hosts
192.168.130.93 mongo1
192.168.130.94 mongo2
192.168.130.95 mongo3
创建目录
mongo1
mkdir -p /data/shard1_1 /data/shard2_1 /data/shard3_1 /data/config
mongo2
mkdir -p /data/shard1_2 /data/shard2_2 /data/shard3_2 /data/config
mongo3
mkdir -p /data/shard1_3 /data/shard2_3 /data/shard3_3 /data/config
mongo1配置副本集,启动mongos路由.
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard1 --port 27017 --journal --dbpath /data/shard1_1/ --logpath /data/shard1_1/shard1_1.log --logappend --fork
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard2 --port 27018 --journal --dbpath /data/shard2_1/ --logpath /data/shard2_1/shard2_1.log --logappend --fork
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard3 --port 27019 --journal --dbpath /data/shard3_1/ --logpath /data/shard3_1/shard3_1.log --logappend --fork
/usr/local/mongodb/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath /data/config/config.log --logappend --fork
/usr/local/mongodb/bin/mongos -configdb mongo1:20000,mongo2:20000,mongo3:20000 --port 30000 --chunkSize 1 --logpath /data/mongos.log --logappend --fork
mongo2配置副本集,启动mongos路由.
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard1 --port 27017 --journal --dbpath /data/shard1_2/ --logpath /data/shard1_2/shard1_2.log --logappend --fork
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard2 --port 27018 --journal --dbpath /data/shard2_2/ --logpath /data/shard2_2/shard2_2.log --logappend --fork
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard3 --port 27019 --journal --dbpath /data/shard3_2/ --logpath /data/shard3_2/shard3_2.log --logappend --fork
/usr/local/mongodb/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath /data/config/config.log --logappend --fork
/usr/local/mongodb/bin/mongos -configdb mongo1:20000,mongo2:20000,mongo3:20000 --port 30000 --chunkSize 1 --logpath /data/mongos.log --logappend --fork
mongo3配置副本集,启动mongos路由.
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard1 --port 27017 --journal --dbpath /data/shard1_3/ --logpath /data/shard1_3/shard1_3.log --logappend --fork
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard2 --port 27018 --journal --dbpath /data/shard2_3/ --logpath /data/shard2_3/shard2_3.log --logappend --fork
/usr/local/mongodb/bin/mongod --shardsvr --replSet shard3 --port 27019 --journal --dbpath /data/shard3_3/ --logpath /data/shard3_3/shard3_3.log --logappend --fork
/usr/local/mongodb/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath /data/config/config.log --logappend --fork
/usr/local/mongodb/bin/mongos -configdb mongo1:20000,mongo2:20000,mongo3:20000 --port 30000 --chunkSize 1 --logpath /data/mongos.log --logappend --fork
--随便进入一台 27017 开启会议
config={_id:'shard1',members:[ {_id:0,host:'mongo1:27017',priority:3}, {_id:1,host:'mongo2:27017',priority:2},{_id:2,host:'mongo3:27017',priority:1}]}
rs.initiate(config).
--随便进入一台 27018 开启会议
config={_id:'shard2',members:[ {_id:0,host:'mongo1:27018',priority:1}, {_id:1,host:'mongo2:27018',priority:2},{_id:2,host:'mongo3:27018',priority:3}]}
rs.initiate(config)
--随便进入一台 27019 开启会议
config={_id:'shard3’,members:[ {_id:0,host:'mongo1:27019’,priority:2}, {_id:1,host:'mongo2:27019’,priority:3},{_id:2,host:'mongo3:27019’,priority:1}]}
rs.initiate(config)
添加分片
mongo --port 30000 随便进入一台
use admin
db.runCommand({addshard:"shard1/mongo1:27017,mongo2:27017,mongo3:27017"})
db.runCommand({addshard:"shard2/mongo1:27018,mongo2:27018,mongo3:27018"})
db.runCommand({addshard:"shard3/mongo1:27019,mongo2:27019,mongo3:27019”})
test数据库开启Sharding
db.runCommand({enablesharding:"test"})
users 集合开启分片(要对一个集合分片,首先要对这个集合的数据库启用分片)
db.runCommand({shardcollection:"test.users",key:{_id:1}})