官方链接:http://docs.mongoing.com/manual-zh/tutorial/convert-replica-set-to-replicated-shard-cluster.html
数据复制集rs0:
mongodb0.example.net:26001
mongodb1.example.net:26002
mongodb2.example.net:26003
数据复制集rs1:
mongodb3.example.net:26004
mongodb4.example.net:26005
mongodb5.example.net:26006
配置服务器复制集configReplSet:
mongodb6.example.net:26007
mongodb7.example.net:26008
mongodb8.example.net:26009
mongos实例:
mongodb9.example.net:26010
步骤:
1、更新hosts文件 (测试时,xxx.xxx.xxx.xxx 可以为同一IP)
路径:C:\Windows\System32\drivers\etc
在hosts文件里添加:xxx.xxx.xxx.xxx mongodb0.example.net
xxx.xxx.xxx.xxx mongodb1.example.net
xxx.xxx.xxx.xxx mongodb2.example.net
xxx.xxx.xxx.xxx mongodb3.example.net
xxx.xxx.xxx.xxx mongodb4.example.net
xxx.xxx.xxx.xxx mongodb5.example.net
xxx.xxx.xxx.xxx mongodb6.example.net
xxx.xxx.xxx.xxx mongodb7.example.net
xxx.xxx.xxx.xxx mongodb8.example.net
xxx.xxx.xxx.xxx mongodb9.example.net
2、添加文件夹
路径:D:\MongoDB\data
添加10个文件夹db26001,db26002,db26003 ...db26010
3、准备config文件
路径:D:\MongoDB\Server\3.4\bin
文件名:mongodb26001.conf (属于复制集rs0)
port=26001
logpath=D:\MongoDB\data\db26001\mongod26001.log
logappend=true
pidfilepath=D:\MongoDB\data\db26001\26001.pid
dbpath=D:\MongoDB\data\db26001
bind_ip=mongodb0.example.net,127.0.0.1
maxConns=500
replSet=rs0
shardsvr=true
#auth=true
#keyFile=D:\MongoDB\Server\3.2\bin\aaakeyfile1.dat
#fork=true
#cpu=true
#noauth=true
//mongodb26002.conf,mongodb26003.conf修改红色字体配置
文件名:mongodb26004.conf (属于复制集rs1)
//mongodb26005.conf,mongodb26006.conf修改红色字体配置
文件名:mongodb26007.conf (属于配置服务器复制集configReplSet)
port=26007
logpath=D:\MongoDB\data\db26007\mongod26007.log
logappend=true
pidfilepath=D:\MongoDB\data\db26007\26007.pid
dbpath=D:\MongoDB\data\db26007
bind_ip=mongodb6.example.net,127.0.0.1
maxConns=500
replSet=configReplSet
configsvr=true
#auth=true
#keyFile=D:\MongoDB\Server\3.2\bin\aaakeyfile1.dat
#fork=true
#cpu=true
#noauth=true
//mongodb26008.conf,mongodb26009.conf修改红色字体配置
文件名:mongodb26010.conf (属于mongos实例)
4、配置复制集rs0,rs1
详细步骤:http://blog.csdn.net/albert0707/article/details/53992932
5、进入复制集rs0主节点批量加入数据
mongo mongodb0.example.net:26001
use test
var bulk = db.test_collection.initializeUnorderedBulkOp();
people = ["Marc", "Bill", "George", "Eliot", "Matt", "Trey", "Tracy", "Greg", "Steve", "Kristina", "Katie", "Jeff"];
for(var i=0; i<1000000; i++){
user_id = i;
name = people[Math.floor(Math.random()*people.length)];
number = Math.floor(Math.random()*10001);
bulk.insert( { "user_id":user_id, "name":name, "number":number });
}
bulk.execute();
6、配置Config Server 复制集configReplSet
mongod -f D:\MongoDB\Server\3.4\bin\mongodb26007.conf
rs.initiate({
_id: "configReplSet",
configsvr: true,
members: [
{ _id: 0, host: "mongodb6.example.net:26007" },
{ _id: 1, host: "mongodb7.example.net:26008" },
{ _id: 2, host: "mongodb8.example.net:26009" }
]
})
rs.conf()
7、启动mongos实例
mongos -f D:\MongoDB\Server\3.4\bin\mongodb26010.conf
进入mongos实例
mongo mongodb9.example.net:26010
添加分片,添加索引,集合名称:test_collection , 片键为number
sh.addShard( "rs0/mongodb0.example.net:26001,mongodb1.example.net:26002,mongodb2.example.net:26003" )
sh.addShard( "rs1/mongodb3.example.net:26004,mongodb4.example.net:26005,mongodb5.example.net:26006" )
sh.enableSharding( "test" )
use test
db.test_collection.createIndex( { number : 1 } )
sh.shardCollection( "test.test_collection", { "number" : 1 } )
8、检验结果
use test db.stats() db.printShardingStatus()Run these commands for a second time to demonstrate that chunks are migrating from rs0 to rs1.