系统环境:Centos 6.5
官网手册:http://docs.mongodb.org/manual/
参阅资料:《Mongodb权威指南》《Mongodb实战》《深入学习Mongodb》
由上方的官方拓扑可以看出,Sharding集群成员由Shard、ConfigServer、Router组成
Shard:可以是一个实例,官方推荐生产环境使用副本集Replica Set
Config Server:为三台主机,当有1台或者2台Server当机,对于用户而言,集群还是可读写的,不过集群不会再进行Chunk分裂和迁移,直到正常恢复为止。
Router:Mongos进程,数量不进行限制,建议针对一个应用服务器只针对一个mongos进程。
下面我会进行一个模拟环境的组建,注意,实际生产环境中,每个实例都是单独的主机,每个config Server也会是单独的主机,不要陷入逻辑的思维中。
主机规划:
主机A(Shard1,由三个实例组成的Replica Sets): 10.1.2.11:27101 10.1.2.11:27102 10.1.2.11:27103 主机B(Shard2,由三个实例组成的Replica Sets): 10.1.2.12:27201 10.1.2.12:27202 10.1.2.12:27203 主机C:(三个config Server实例,一个mongos实例) 10.1.2.10:27301 10.1.2.10:27302 10.1.2.10:27303 10.1.2.10:27400(mongos)
A、B主机的操作基本一致,这里我写一遍在A上的操作。
1.
下载并解压Mongodb二进制源码包!
最新Mongodb稳定安装包下载:http://www.mongodb.org/downloads
tar xzf mongodb-linux-x86_64-2.6.4.tgz mv mongodb-linux-x86_64-2.6.4/usr/local/mongodb
2.为每个实例创建数据目录:
mkdir -p /data/shard1_1 mkdir -p /data/shard1_2 mkdir -p /data/shard1_3
3.创建Mongodb日志及PID文件目录
mkdir /var/log/mongodb mkdir /var/run/mongodb
4.创建Mongodb配置文件目录,及配置合理的配置文件内容。(生产环境我建议通过配置文件来控制启动参数!这步,由于我实验每个主机开了多个实例,就不用配置文件了,大家如果是生产环境,可以参考下!)
cd /usr/local/mongodb/ mkdir etc vim etc/mongod.conf #Filename:mongodb.conf #Now use options: fork = true port = 27101 quiet = true replSet = sharding1 dbpath = /data/mongodb pidfilepath = /var/run/mongodb/mongod.pid logpath = /var/log/mongodb/mongod.log logappend = true journal = true
5.配置全局环境变量:
echo"PATH=$PATH:/usr/local/mongodb/bin" >> /etc/profile
执行. /etc/profile 可立即使当前用户生效。这样的目的是为了方便执行Mongodb相关命令!
6.启动实例:
mongod --shardsvr --replSet sharding1--port 27101 --dbpath /data/shard1_1/ --pidfilepath /var/run/mongodb/sharding1_1.pid--logpath /var/log/mongodb/sharding1_1.log --logappend �Cfork mongod --shardsvr --replSet sharding1--port 27102 --dbpath /data/shard1_2/ --pidfilepath /var/run/mongodb/sharding1_2.pid--logpath /var/log/mongodb/sharding1_2.log --logappend �Cfork mongod --shardsvr --replSet sharding1--port 27103 --dbpath /data/shard1_3/ --pidfilepath /var/run/mongodb/sharding1_3.pid--logpath /var/log/mongodb/sharding1_3.log --logappend �Cfork
7.初始化Shard1的 Replica Set(副本集):
# mongo --port 27101 MongoDB shell version: 2.6.4 connecting to: 127.0.0.1:27101/test > use admin switched to db admin >db.runCommand({"replSetInitiate" :{ ... "_id":"sharding1", ... "members":[ ...{"_id":1,"host":"10.1.2.11:27101"}, ...{"_id":2,"host":"10.1.2.11:27102"}, ...{"_id":3,"host":"10.1.2.11:27103"}, ... ]}}) #执行后返回: { "info": "Config now saved locally. Shouldcome online in about a minute.", "ok": 1 }
8.验证Replica Sets状态:
sharding1:PRIMARY> rs.status() { "set": "sharding1", "date": ISODate("2014-08-28T08:29:45Z"), "myState": 1, "members": [ { "_id": 1, "name": "10.1.2.11:27101", "health": 1, "state": 1, "stateStr": "PRIMARY", "uptime": 577, "optime": Timestamp(1409214151, 1), "optimeDate": ISODate("2014-08-28T08:22:31Z"), "electionTime": Timestamp(1409214162, 1), "electionDate": ISODate("2014-08-28T08:22:42Z"), "self": true }, { "_id": 2, "name": "10.1.2.11:27102", "health": 1, "state": 2, "stateStr": "SECONDARY", "uptime": 431, "optime": Timestamp(1409214151, 1), "optimeDate": ISODate("2014-08-28T08:22:31Z"), "lastHeartbeat": ISODate("2014-08-28T08:29:45Z"), "lastHeartbeatRecv": ISODate("2014-08-28T08:29:43Z"), "pingMs": 0, "syncingTo": "10.1.2.11:27101" }, { "_id": 3, "name": "10.1.2.11:27103", "health": 1, "state": 2, "stateStr": "SECONDARY", "uptime": 431, "optime": Timestamp(1409214151, 1), "optimeDate": ISODate("2014-08-28T08:22:31Z"), "lastHeartbeat": ISODate("2014-08-28T08:29:45Z"), "lastHeartbeatRecv": ISODate("2014-08-28T08:29:45Z"), "pingMs": 12, "syncingTo": "10.1.2.11:27101" } ], "ok": 1 }
B主机操作与A主机基本一致,配置完B主机后,再继续下面的操作!
主机C上的配置:
1.解压源码包
tar xzf mongodb-linux-x86_64-2.6.4.tgz mv mongodb-linux-x86_64-2.6.4/usr/local/mongodb
2.配置config及日志目录:
mkdir -p /data/config_1 mkdir -p /data/config_2 mkdir -p /data/config_3 mkdir -p /var/log/mongodb
3.配置全局环境变量:
echo"PATH=$PATH:/usr/local/mongodb/bin" >> /etc/profile
4.启动config Server实例:
mongod --configsvr --dbpath /data/config_1/--port 27301 --logpath /var/log/mongodb/dbconfig_1.log --logappend �Cfork mongod --configsvr --dbpath /data/config_2/--port 27302 --logpath /var/log/mongodb/dbconfig_2.log --logappend �Cfork mongod --configsvr --dbpath /data/config_3/--port 27303 --logpath /var/log/mongodb/dbconfig_3.log --logappend �Cfork
5.启动mongos实例:
mongos --configdb10.1.2.10:27301,10.1.2.10:27302,10.1.2.10:27303 --port 27400 --logpath/var/log/mongodb/mongos.log --logappend �Cfork
注:生产环境mongos一样建议用配置文件来控制,例如:
#mongos.conf #Options: port =27400 fork =true logappend= true logpath= /var/log/mongodb/mongos.log pidfilepath= /var/run/mongos.pid configdb= 10.1.2.10:27301,10.1.2.10:27302,10.1.2.10:27303
6.登录mongos进程,配置Shard Cluster:
# mongo --port 27400 admin MongoDB shell version: 2.6.4 connecting to: 127.0.0.1:27400/admin mongos> mongos>db.runCommand({addshard:"sharding1/10.1.2.11:27101,10.1.2.11:27102,10.1.2.11:27103"}); { "shardAdded" :"sharding1", "ok" : 1 } mongos>db.runCommand({addshard:"sharding2/10.1.2.12:27201,10.1.2.12:27202,10.1.2.12:27203"}); { "shardAdded" :"sharding2", "ok" : 1 }
7.验证Sharding集群状态:
mongos> sh.status() --- Sharding Status --- sharding version: { "_id": 1, "version": 4, "minCompatibleVersion": 4, "currentVersion": 5, "clusterId": ObjectId("53fee749becfed3ca6de7051") } shards: { "_id" : "sharding1", "host" :"sharding1/10.1.2.11:27101,10.1.2.11:27102,10.1.2.11:27103" } { "_id" : "sharding2", "host" :"sharding2/10.1.2.12:27201,10.1.2.12:27202,10.1.2.12:27203" } databases: { "_id" : "admin", "partitioned" : false, "primary" : "config" } { "_id" : "test", "partitioned" : false, "primary" : "sharding2" } mongos>
至此,全部的环境建制完毕!
附:下面提一点关于分片的操作:
激活数据库分片: db.runCommand({enablesharding:"dbname"}) //这里要在admin数据库下操作 或 sh.enableSharding(“dbname”) //这个可以全局操作 Collection启用分片(这里要选取一个shard key,详细介绍请参照官网): db.runCommand({shardcollection:"dbname.collectionname",key{_id:1}}) //同上! 或 sh.shardCollection( "records.active", { a:"hashed" } )