1.规划
角色 主机 端口 数据文件路径 日志文件路径
Router localhost 9001 /home/master/data/router1 /home/master/log/router1/router1.log
localhost 9002 /home/master/data/router2 /home/master/log/router2/router2.log
Config Server localhost 10001 /home/master/data/config1 /home/master/log/config1/config1.log
localhost 10002 /home/master/data/config2 /home/master/log/config2/config2.log
localhost 10003 /home/master/data/config3 /home/master/log/config3/config3.log
复制集1(power1) localhost 20001 /home/master/data/power1_01 /home/master/log/power1_01/power1_01.log
localhost 20002 /home/master/data/power1_02 /home/master/log/power1_02/power1_02.log
localhost 20003 /home/master/data/power1_03 /home/master/log/power1_03/power1_03.log
复制集2(power2) localhost 30001 /home/master/data/power2_01 /home/master/log/power2_01/power2_01.log
localhost 30002 /home/master/data/power2_02 /home/master/log/power2_02/power2_02.log
localhost 30003 /home/master/data/power2_03 /home/master/log/power2_03/power2_03.log
2.创建相关目录
mkdir /home/oracle/data/router1
mkdir /home/oracle/data/router2
mkdir /home/oracle/data/config1
mkdir /home/oracle/data/config2
mkdir /home/oracle/data/config3
mkdir /home/oracle/data/power1_01
mkdir /home/oracle/data/power1_02
mkdir /home/oracle/data/power1_03
mkdir /home/oracle/data/power2_01
mkdir /home/oracle/data/power2_02
mkdir /home/oracle/data/power2_03
mkdir -p /home/oracle/log/router1
mkdir -p /home/oracle/log/router2
mkdir -p /home/oracle/log/config1
mkdir -p /home/oracle/log/config2
mkdir -p /home/oracle/log/config3
mkdir -p /home/oracle/log/power1_01
mkdir -p /home/oracle/log/power1_02
mkdir -p /home/oracle/log/power1_03
mkdir -p /home/oracle/log/power2_01
mkdir -p /home/oracle/log/power2_02
mkdir -p /home/oracle/log/power2_03
3. 部署config server集群
mongod --configsvr --replSet configset --port 10001 --dbpath /home/oracle/data/config1 --logpath /home/oracle/log/config1/config1.log --fork
mongod --configsvr --replSet configset --port 10002 --dbpath /home/oracle/data/config2 --logpath /home/oracle/log/config2/config2.log --fork
mongod --configsvr --replSet configset --port 10003 --dbpath /home/oracle/data/config3 --logpath /home/oracle/log/config3/config3.log --fork
--初始化集群:
rs.initiate( {
_id: "configset",
configsvr: true,
members: [
{ _id: 0, host: "master:10001" },
{ _id: 1, host: "master:10002" },
{ _id: 2, host: "master:10003" }
]
} )
4.启动mongos实例
mongos --configdb configset/master:10001,master:10002,master:10003 --port 9001 --logpath /home/oracle/log/router1/router1.log --fork
mongos --configdb configset/master:10001,master:10002,master:10003 --port 9002 --logpath /home/oracle/log/router2/router2.log --fork
5.创建mongodb复制集
---启动mongodb
mongod --replSet "power1" --dbpath /home/oracle/data/power1_01 --port 20001 --logpath /home/oracle/log/power1_01/power1_01.log --fork
mongod --replSet "power1" --dbpath /home/oracle/data/power1_02 --port 20002 --logpath /home/oracle/log/power1_02/power1_02.log --fork
mongod --replSet "power1" --dbpath /home/oracle/data/power1_03 --port 20003 --logpath /home/oracle/log/power1_03/power1_03.log --fork
mongod --replSet "power2" --dbpath /home/oracle/data/power2_01 --port 30001 --logpath /home/oracle/log/power2_01/power2_01.log --fork
mongod --replSet "power2" --dbpath /home/oracle/data/power2_02 --port 30002 --logpath /home/oracle/log/power2_02/power2_02.log --fork
mongod --replSet "power2" --dbpath /home/oracle/data/power2_03 --port 30003 --logpath /home/oracle/log/power2_03/power2_03.log --fork
mongo master:20001/admin
rs.initiate()
rs.conf()
rs.add("master:20002")
rs.add("master:20003")
rs.status()
mongo master:30001/admin
rs.initiate()
rs.conf()
rs.add("master:30002")
rs.add("master:30003")
6.将复制集加入到分片集群中
mongo master:9001/admin
sh.addShard("power1/master:20001")
sh.addShard("power2/master:30001")
7.开启数据库test分片功能
mongo master:9001/adminn
sh.enableSharding("test")
8.开启collection的分片功能
1)决定使用什么作为片键。片键的选择会影响分片的性能。
2)如果一个collection已经包含数据,则必须在片键的字段上使用ensureIndex()命令建立一个索引。如果collection是空的,则MongoDB将在sh.shardCollection()阶段建立一个索引。
3)通过在mongo shell中执行sh.shardCollection()方法开启一个collection的分片功能。这个方法的使用语法如下:
db.shardCollection(“<database>.<collection>”, shard-key-pattern)
sh.shardCollection("records.people", { "zipcode": 1, "name": 1 } )
sh.shardCollection("people.addresses", { "state": 1, "_id": 1 } )
sh.shardCollection("assets.chairs", { "type": 1, "_id": 1 } )
sh.shardCollection("test.alerts", { "_id": "hashed" } )
角色 主机 端口 数据文件路径 日志文件路径
Router localhost 9001 /home/master/data/router1 /home/master/log/router1/router1.log
localhost 9002 /home/master/data/router2 /home/master/log/router2/router2.log
Config Server localhost 10001 /home/master/data/config1 /home/master/log/config1/config1.log
localhost 10002 /home/master/data/config2 /home/master/log/config2/config2.log
localhost 10003 /home/master/data/config3 /home/master/log/config3/config3.log
复制集1(power1) localhost 20001 /home/master/data/power1_01 /home/master/log/power1_01/power1_01.log
localhost 20002 /home/master/data/power1_02 /home/master/log/power1_02/power1_02.log
localhost 20003 /home/master/data/power1_03 /home/master/log/power1_03/power1_03.log
复制集2(power2) localhost 30001 /home/master/data/power2_01 /home/master/log/power2_01/power2_01.log
localhost 30002 /home/master/data/power2_02 /home/master/log/power2_02/power2_02.log
localhost 30003 /home/master/data/power2_03 /home/master/log/power2_03/power2_03.log
2.创建相关目录
mkdir /home/oracle/data/router1
mkdir /home/oracle/data/router2
mkdir /home/oracle/data/config1
mkdir /home/oracle/data/config2
mkdir /home/oracle/data/config3
mkdir /home/oracle/data/power1_01
mkdir /home/oracle/data/power1_02
mkdir /home/oracle/data/power1_03
mkdir /home/oracle/data/power2_01
mkdir /home/oracle/data/power2_02
mkdir /home/oracle/data/power2_03
mkdir -p /home/oracle/log/router1
mkdir -p /home/oracle/log/router2
mkdir -p /home/oracle/log/config1
mkdir -p /home/oracle/log/config2
mkdir -p /home/oracle/log/config3
mkdir -p /home/oracle/log/power1_01
mkdir -p /home/oracle/log/power1_02
mkdir -p /home/oracle/log/power1_03
mkdir -p /home/oracle/log/power2_01
mkdir -p /home/oracle/log/power2_02
mkdir -p /home/oracle/log/power2_03
3. 部署config server集群
mongod --configsvr --replSet configset --port 10001 --dbpath /home/oracle/data/config1 --logpath /home/oracle/log/config1/config1.log --fork
mongod --configsvr --replSet configset --port 10002 --dbpath /home/oracle/data/config2 --logpath /home/oracle/log/config2/config2.log --fork
mongod --configsvr --replSet configset --port 10003 --dbpath /home/oracle/data/config3 --logpath /home/oracle/log/config3/config3.log --fork
--初始化集群:
rs.initiate( {
_id: "configset",
configsvr: true,
members: [
{ _id: 0, host: "master:10001" },
{ _id: 1, host: "master:10002" },
{ _id: 2, host: "master:10003" }
]
} )
4.启动mongos实例
mongos --configdb configset/master:10001,master:10002,master:10003 --port 9001 --logpath /home/oracle/log/router1/router1.log --fork
mongos --configdb configset/master:10001,master:10002,master:10003 --port 9002 --logpath /home/oracle/log/router2/router2.log --fork
5.创建mongodb复制集
---启动mongodb
mongod --replSet "power1" --dbpath /home/oracle/data/power1_01 --port 20001 --logpath /home/oracle/log/power1_01/power1_01.log --fork
mongod --replSet "power1" --dbpath /home/oracle/data/power1_02 --port 20002 --logpath /home/oracle/log/power1_02/power1_02.log --fork
mongod --replSet "power1" --dbpath /home/oracle/data/power1_03 --port 20003 --logpath /home/oracle/log/power1_03/power1_03.log --fork
mongod --replSet "power2" --dbpath /home/oracle/data/power2_01 --port 30001 --logpath /home/oracle/log/power2_01/power2_01.log --fork
mongod --replSet "power2" --dbpath /home/oracle/data/power2_02 --port 30002 --logpath /home/oracle/log/power2_02/power2_02.log --fork
mongod --replSet "power2" --dbpath /home/oracle/data/power2_03 --port 30003 --logpath /home/oracle/log/power2_03/power2_03.log --fork
mongo master:20001/admin
rs.initiate()
rs.conf()
rs.add("master:20002")
rs.add("master:20003")
rs.status()
mongo master:30001/admin
rs.initiate()
rs.conf()
rs.add("master:30002")
rs.add("master:30003")
6.将复制集加入到分片集群中
mongo master:9001/admin
sh.addShard("power1/master:20001")
sh.addShard("power2/master:30001")
7.开启数据库test分片功能
mongo master:9001/adminn
sh.enableSharding("test")
8.开启collection的分片功能
1)决定使用什么作为片键。片键的选择会影响分片的性能。
2)如果一个collection已经包含数据,则必须在片键的字段上使用ensureIndex()命令建立一个索引。如果collection是空的,则MongoDB将在sh.shardCollection()阶段建立一个索引。
3)通过在mongo shell中执行sh.shardCollection()方法开启一个collection的分片功能。这个方法的使用语法如下:
db.shardCollection(“<database>.<collection>”, shard-key-pattern)
sh.shardCollection("records.people", { "zipcode": 1, "name": 1 } )
sh.shardCollection("people.addresses", { "state": 1, "_id": 1 } )
sh.shardCollection("assets.chairs", { "type": 1, "_id": 1 } )
sh.shardCollection("test.alerts", { "_id": "hashed" } )
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10972173/viewspace-1992985/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10972173/viewspace-1992985/