Replica Sets + Sharding配置
开放的端口如下:
主机主机 IP 服务及端口服务及端口
主机主机 服务及端口服务及端口
Server A 192.168.3.231 mongod shard1_1:27017
mongod shard2_1:27018
mongod config1:20000
mongs1:30000
Server B 192.168.3.232 mongod shard1_2:27017
mongod shard2_2:27018
mongod config2:20000
mongs2:30000
Server C 192.168.3.233 mongod shard1_3:27017
mongod shard2_3:27018
mongod config3:20000
mongs3:30000
23.1 创建数据目录创建数据目录
创建数据目录创建数据目录
在在Server A 上上:
在在 上上
[root@localhost bin]# mkdir -p /data/shard1_1
[root@localhost bin]# mkdir -p /data/shard2_1
[root@localhost bin]# mkdir -p /data/config
在在Server B 上上:
在在 上上
[root@localhost bin]# mkdir -p /data/shard1_2
[root@localhost bin]# mkdir -p /data/shard2_2
[root@localhost bin]# mkdir -p /data/config
在在Server C 上上:
在在 上上
[root@localhost bin]# mkdir -p /data/shard1_3
[root@localhost bin]# mkdir -p /data/shard2_3
[root@localhost bin]# mkdir -p /data/config
23.2 配置配置 Replica Sets
配置配置
23.2.1 配置配置shard1 所用到的所用到的 Replica Sets
配置配置 所用到的所用到的
在在Server A 上上:
在在 上上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017
--dbpath /data/shard1_1 --logpath /data/shard1_1/shard1_1.log --logappend --fork
[root@localhost bin]# all output going to: /data/shard1_1/shard1_1.log
forked proce : 18923
在在Server B 上上:
在在 上上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017
--dbpath /data/shard1_2 --logpath /data/shard1_2/shard1_2.log --logappend --fork
forked proce : 18859
[root@localhost bin]# all output going to: /data/shard1_2/shard1_2.log
[root@localhost bin]#
在在Server C 上上:
在在 上上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017
--dbpath /data/shard1_3 --logpath /data/shard1_3/shard1_3.log --logappend --fork
all output going to: /data/shard1_3/shard1_3.log
forked proce : 18768
[root@localhost bin]#
用 mongo 连接其中一台机器的27017 端口的 mongod,初始化Replica Set “shard1”,执行:
[root@localhost bin]# ./mongo --port 27017
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:27017/test
> config = {_id: 'shard1', members: [
... {_id: 0, host: '192.168.3.231:27017'},
... {_id: 1, host: '192.168.3.232:27017'},
... {_id: 2, host: '192.168.3.233:27017'}]
... }
……
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
23.2.2 配置配置shard2 所用到的所用到的 Replica Sets
配置配置 所用到的所用到的
在在Server A 上上:
在在 上上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018
--dbpath /data/shard2_1 --logpath /data/shard2_1/shard2_1.log --logappend --fork
all output going to: /data/shard2_1/shard2_1.log
[root@localhost bin]# forked proce : 18993
[root@localhost bin]#
在在Server B 上上:
在在 上上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018
--dbpath /data/shard2_2 --logpath /data/shard2_2/shard2_2.log --logappend --fork
all output going to: /data/shard2_2/shard2_2.log
forked proce : 18923
[root@localhost bin]#
在在Server C 上上:
在在 上上
[root@localhost bin]# /Apps/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018
--dbpath /data/shard2_3 --logpath /data/shard2_3/shard2_3.log --logappend --fork
[root@localhost bin]# all output going to: /data/shard2_3/shard2_3.log
forked proce : 18824
[root@localhost bin]#
用 mongo 连接其中一台机器的27018 端口的 mongod,初始化Replica Sets “shard2”,执行:
[root@localhost bin]# ./mongo --port 27018
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:27018/test
> config = {_id: 'shard2', members: [
... {_id: 0, host: '192.168.3.231:27018'},
... {_id: 1, host: '192.168.3.232:27018'},
... {_id: 2, host: '192.168.3.233:27018'}]
... }
……
> rs.initiate(config)
{
"info" : "Config now saved locally. Should come online in about a minute.",
"ok" : 1
}
23.3 配置配置3 台台Config Server
配置配置 台台
在在Server A、、B、、C 上执行上执行:
在在 、、 、、 上上执行执行
/Apps/mongo/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath
/data/config/config.log --logappend --fork
23.4 配置配置3 台台Route Process
配置配置 台台
在在Server A、、B、、C 上执行上执行:
在在 、、 、、 上上执行执行
/Apps/mongo/bin/mongos --configdb
192.168.3.231:20000,192.168.3.232:20000,192.168.3.233:20000 --port 30000 --chunkSize 1
--logpath /data/mongos.log --logappend --fork
23.5 配置配置Shard Cluster
配置配置
连接到其中一台机器的端口30000 的mongo 进程,并切换到admin 数据库做以下配置
[root@localhost bin]# ./mongo --port 30000
MongoDB shell version: 1.8.1
connecting to: 127.0.0.1:30000/test
> use admin
switched to db admin
>db.runCommand({addshard:"shard1/192.168.3.231:27017,192.168.3.232:27017,192.168.3.233:
27017"});
{ "shardAdded" : "shard1", "ok" : 1 }
>db.runCommand({addshard:"shard2/192.168.3.231:27018,192.168.3.232:27018,192.168.3.233:
27018"});
{ "shardAdded" : "shard2", "ok" : 1 }
>
激活数据库及集合的分片
db.runCommand({ enablesharding:"test" })
db.runCommand({ shardcollection: "test.users", key: { _id:1 }})
23.6 验证验证Sharding 正常工作正常工作
验证验证 正常工作正常工作
连接到其中一台机器的端口30000 的mongo 进程,并切换到test 数据库,以便添加测试数
据
use test
for(var i=1;i<=200000;i++) db.users.insert({id:i,addr_1:"Beijing",addr_2:"Shanghai"});
db.users.stats()
{
"sharded" : true,
"ns" : "test.users",
"count" : 200000,
"size" : 25600384,
"avgObjSize" : 128,
"storageSize" : 44509696,
"nindexes" : 2,
"nchunks" : 15,
"shards" : {
"shard0000" : {
……
},
"shard0001" : {
……
}
},
"ok" : 1
}
可以看到Sharding 搭建成功了,跟我们期望的结果一致,至此我们就将Replica Set 与Sharding
结合的架构也学习完毕了。