方案二:先建立分片集群,再同步数据,最后配置分片集群。
导出数据;建立分片集群;配置config server;配置mongos server;导入数据;配置分片集群。
1. 导出数据
/usr/local/mongo/bin/mongodump --host 172.16.8.134:29017 -d 201 -o /data/data/mongo/tmp/ --directoryperdb
并且将该数据同步到131的/data/data/mongodb/tmp目录中
2. 建立分片集群
2.1 建立shard1分片集群
2.1.1 启动shard1的replica set
server1:
/usr/local/mongo/bin/mongod --shardsvr --replSet shard1 --port 29017 --dbpath=/data/data/mongodb/shard11 --logpath=/data/log/shard11.log --logappend --fork
server2:
/usr/local/mongo/bin/mongod --shardsvr --replSet shard1 --port 29017 --dbpath=/data/data/mongodb/shard12 --logpath=/data/log/shard12.log --logappend --fork
server3:
/usr/local/mongo/bin/mongod --shardsvr --replSet shard1 --port 29017 --dbpath=/data/data/mongodb/shard13 --logpath=/data/log/shard13.log --logappend --fork
2.1.2 初始化replica set
连接任意一个mongos实例,如server1
cd /usr/local/mongo/bin
./mongo --port 29017
config={_id : 'shard1',members : [{_id : 0, host : '172.16.8.131:29017'},{_id : 1, host : '172.16.8.132:29017'},{_id : 2, host : '172.16.8.133:29017'}]}
rs.initiate(config)
rs.status()
2.2 建立shard2分片集群
2.2.1 启动shard2的replica set
server1:
/usr/local/mongo/bin/mongod --shardsvr --replSet shard2 --port 29018 --dbpath=/data/data/mongodb/shard21 --logpath=/data/log/shard21.log --logappend --fork
server2:
/usr/local/mongo/bin/mongod --shardsvr --replSet shard2 --port 29018 --dbpath=/data/data/mongodb/shard22 --logpath=/data/log/shard22.log --logappend --fork
server3:
/usr/local/mongo/bin/mongod --shardsvr --replSet shard2 --port 29018 --dbpath=/data/data/mongodb/shard23 --logpath=/data/log/shard23.log --logappend --fork
2.2.2 初始化replica set
连接任意一个mongos实例,如server1
cd /usr/local/mongo/bin
./mongo --port 29018
config={_id : 'shard2',members : [{_id : 0, host : '172.16.8.131:29018'},{_id : 1, host : '172.16.8.132:29018'},{_id : 2, host : '172.16.8.133:29018'}]}
rs.initiate(config)
rs.status()
2.3 建立shard3分片集群
2.3.1 启动shard3的replica set
server1:
/usr/local/mongo/bin/mongod --shardsvr --replSet shard3 --port 29019 --dbpath=/data/data/mongodb/shard31 --logpath=/data/log/shard31.log --logappend --fork
server2:
/usr/local/mongo/bin/mongod --shardsvr --replSet shard3 --port 29019 --dbpath=/data/data/mongodb/shard32 --logpath=/data/log/shard32.log --logappend --fork
server3:
/usr/local/mongo/bin/mongod --shardsvr --replSet shard3 --port 29019 --dbpath=/data/data/mongodb/shard33 --logpath=/data/log/shard33.log --logappend --fork
2.3.2 初始化replica set
连接任意一个mongos实例,如server1
cd /usr/local/mongo/bin
./mongo --port 29019
config={_id : 'shard3',members : [{_id : 0, host : '172.16.8.131:29019'},{_id : 1, host : '172.16.8.132:29019'},{_id : 2, host : '172.16.8.133:29019'}]}
rs.initiate(config)
rs.status()
3. 配置config server
server1:
/usr/local/mongo/bin/mongod --configsvr --dbpath=/data/data/mongodb/config --port 20000 --logpath=/data/log/config.log --logappend --fork
server2:
/usr/local/mongo/bin/mongod --configsvr --dbpath=/data/data/mongodb/config --port 20000 --logpath=/data/log/config.log --logappend --fork
server3:
/usr/local/mongo/bin/mongod --configsvr --dbpath=/data/data/mongodb/config --port 20000 --logpath=/data/log/config.log --logappend --fork
4. 配置mongos server
server1:
/usr/local/mongo/bin/mongos --configdb 172.16.8.131:20000,172.16.8.132:20000,172.16.8.133:20000 -port 40000 -chunkSize 5 --logpath=/data/log/mongos.log --logappend --fork
server2:
/usr/local/mongo/bin/mongos --configdb 172.16.8.131:20000,172.16.8.132:20000,172.16.8.133:20000 -port 40000 -chunkSize 5 --logpath=/data/log/mongos.log --logappend --fork
server3:
/usr/local/mongo/bin/mongos --configdb 172.16.8.131:20000,172.16.8.132:20000,172.16.8.133:20000 -port 40000 -chunkSize 5 --logpath=/data/log/mongos.log --logappend --fork
5. 导入数据
需要导入到PRIMARY节点
./mongorestore --host 172.16.8.131:29017 -d 201 --directoryperdb /data/data/mongodb/tmp/201/
6. 配置分片集群
6.1 连接任意一个mongos实例,如server1,并切换到admin
cd /usr/local/mongo/bin
./mongo 172.16.8.131:40000/admin
db
6.2 加入shards
db.runCommand({ addshard : "shard1/172.16.8.131:29017,172.16.8.132:29017,172.16.8.133:29017", name:"shard1", maxsize:20480});
db.runCommand({ addshard : "shard2/172.16.8.131:29018,172.16.8.132:29018,172.16.8.133:29018", name:"shard2", maxsize:20480});
db.runCommand({ addshard : "shard3/172.16.8.131:29019,172.16.8.132:29019,172.16.8.133:29019", name:"shard3", maxsize:20480});
6.3 Listing shards
db.runCommand({listshards : 1})
6.4 激活数据库分片
db.runCommand({enablesharding : "201"})
6.5 建立collection的片键索引
use 201
db.customer_user_mst.ensureIndex({"name":1})
db.user_attr_mst.ensureIndex({"name":1})
6.5 配置collection分片
use admin
db.runCommand({"shardcollection":"201.customer_user_mst", key:{"name":1}})
db.runCommand({"shardcollection":"201.user_attr_mst", key:{"name":1}})