Welcome 微信登录

首页 / 数据库 / MySQL / MongoDB 分片(Cluster)

基本环境:由于资源紧张,只有3台虚拟机的关系,只做两个replicaSet,每台机器配置分别如下:10.10.1.55这台机器安装 Primary1,configServer1, Arbiter110.10.1.56 安装 Primary2,configServer2, Arbiter210.10.1.57 安装 Secondary1,Secondary2,configServer3,mongos1.55机器的配置文件如下:Primary1的conf文件:
1234567891011dbpath=/data/mongodb/rs0_0logpath=/data/mongodb/log/rs0_0.loglogappend=trueport=40000bind_ip=192.168.11.55,10.10.1.55oplogSize=10000fork=truejournal = true#noprealloc = truereplSet=rs0directoryperdb=true
Arbiter1的配置文件:
1234567891011dbpath=/data/mongodb/rs0_arbiterlogpath=/data/mongodb/log/rs0_arbiter.loglogappend=trueport=40002bind_ip=192.168.11.55,10.10.1.55oplogSize=10000fork=truejournal = true#noprealloc = truereplSet=rs0directoryperdb=true
ConfigServer1的配置文件:
12345678910dbpath=/data/mongodb/rs0_conflogpath=/data/mongodb/log/rs0_conf.loglogappend=trueport=40006bind_ip=192.168.11.55,10.10.1.55fork=truejournal = true#noprealloc = trueconfigsvr=truedirectoryperdb=true
分别通过mongod --config  filename 来启动不同的mongo 进程,成功启动后可以通过netstat 查看在1.55 机器上分别分配了Primary1端口:40000,Arbiter1端口:40002,configureServer1端口:400061.56机器的配置:Primary2的配置文件:
1234567891011dbpath=/data/mongodb/rs1_primarylogpath=/data/mongodb/log/rs1_p.loglogappend=truebind_ip=192.168.11.56,10.10.1.56directoryperdb=true  port=40003oplogSize=10000fork=truejournal = truenoprealloc = truereplSet=rs1
Arbiter2配置文件:
1234567891011dbpath=/data/mongodb/rs1_arbiterlogpath=/data/mongodb/log/rs1_a.loglogappend=truebind_ip=192.168.11.56,10.10.1.56directoryperdb=true  port=40005oplogSize=10000fork=truejournal = truenoprealloc = truereplSet=rs1
ConfigureServer2配??文件:
1234567891011dbpath=/data/mongodb/rs1_conflogpath=/data/mongodb/log/rs1_conf.loglogappend=truebind_ip=192.168.11.56,10.10.1.56directoryperdb=true  port=40007oplogSize=10000fork=truejournal = truenoprealloc = trueconfigsvr=true
分别通过mongod --config  filename 来启动不同的mongo 进程,成功启动后可以通过netstat 查看在1.55 机器上分别分配了Primary2端口:40003,Arbiter2端口:40005,configureServer2端口:40007 1.56机器的配置:rs0_Secondary1配置:
1234567891011dbpath=/data/mongodb/rs0_secondary1logpath=/data/mongodb/log/rs0_secondary1.loglogappend=trueport=40001bind_ip=192.168.11.57,10.10.1.57oplogSize=10000fork=truejournal = true#noprealloc = truereplSet=rs0directoryperdb=true
rs1_Secondary1配置:
1234567891011dbpath=/data/mongodb/rs1_secondary1logpath=/data/mongodb/log/rs1_secondary1.loglogappend=truebind_ip=192.168.11.57,10.10.1.57directoryperdb=true  port=40004oplogSize=10000fork=truejournal = truenoprealloc = truereplSet=rs1
configureServer3配置:
12345678910dbpath=/data/mongodb/confSvr3logpath=/data/mongodb/log/conf3.loglogappend=truebind_ip=192.168.11.57,10.10.1.57directoryperdb=true  port=40008oplogSize=10000fork=truejournal = trueconfigsvr=true
mongos 配置:(启动mongos路由器要注意多台服务器时间必须要同步,否则出现错误)
1234logpath=/data/mongodb/log/mongos.logport = 40009configdb=10.10.1.55:40006,10.10.1.56:40007,10.10.1.57:40008fork = true
分别通过mongod --config  filename 来启动不同的mongo 进程,成功启动后可以通过netstat 查看在1.55 机器上分别分配了rs0_secondary1端口:40001,rs1_secondary1端口:40004,configureServer3端口:40008,mongos路由端口:40009现在用mongo shell登录primary1 配置replicaSet0,步骤如下:cfg={ "_id" : "rs0", "members" : [ { "_id" : 0, "host" : "10.10.1.55:40000" }, { "_id" : 1, "host" : "10.10.1.57:40001" } ] }rs.initiate(cfg)
rs.status()rs.addArb("10.10.1.55:40002")

现在用mongo shell登录primary2 配置replicaSet1,步骤如下:cfg={ "_id" : "rs1", "members" : [ { "_id" : 0, "host" : "10.10.1.56:40003" }, { "_id" : 1, "host" : "10.10.1.57:40004" } ] }rs.initiate(cfg)
rs.status()rs.addArb("10.10.1.56:40005")使用mongo shell登录mongos路由添加分片信息:
12345678910111213141516171819mongo --host 10.10.1.57 --port 40009mongos> sh.addShard("rs0/10.10.1.55:40000,10.10.1.57:40001""shardAdded" "rs0""ok" : 1 }mongos> sh.addShard("rs1/10.10.1.56:40003,10.10.1.57:40004""shardAdded" "rs1""ok" : 1 }mongos> sh.status()--- Sharding Status ---   sharding version: {    "_id" : 1,    "version" : 4,    "minCompatibleVersion" : 4,    "currentVersion" : 5,    "clusterId" : ObjectId("561c7bdd4315b18f9862adb4"}  shards:    "_id" "rs0""host" "rs0/10.10.1.55:40000,10.10.1.57:40001" }    "_id" "rs1""host" "rs1/10.10.1.56:40003,10.10.1.57:40004" }  databases:    "_id" "admin""partitioned" false"primary" "config" }
现在创建一个新的数据库来测试一下分片:mongos> use people
switched to db people
mongos> for(var i=1;i<10;i++) db.customers.insert({name:"jordan"+i,country:"American"})
WriteResult({ "nInserted" : 1 })
mongos> for(var i=1;i<10;i++) db.customers.insert({name:"gaga"+i,country:"American"})
WriteResult({ "nInserted" : 1 })
mongos> for(var i=1;i<10;i++) db.customers.insert({name:"ham"+i,country:"UK"})
WriteResult({ "nInserted" : 1 })
mongos> for(var i=1;i<10;i++) db.customers.insert({name:"brown"+i,country:"UK"})
WriteResult({ "nInserted" : 1 })
mongos> for(var i=1;i<10;i++) db.customers.insert({name:"ramda"+i,country:"Malaysia"})
WriteResult({ "nInserted" : 1 })开始建立分片:
1234567891011121314151617181920212223242526272829303132333435mongos> db.customers.ensureIndex({country:1,_id:1}){    "raw" : {        "rs0/10.10.1.55:40000,10.10.1.57:40001" : {            "createdCollectionAutomatically" false,            "numIndexesBefore" : 1,            "numIndexesAfter" : 2,            "ok" : 1        }    },    "ok" : 1}mongos> sh.shardCollection("people.customers",{country:1,_id:1})"collectionsharded" "people.customers""ok" : 1 }mongos> sh.status()--- Sharding Status ---   sharding version: {    "_id" : 1,    "version" : 4,    "minCompatibleVersion" : 4,    "currentVersion" : 5,    "clusterId" : ObjectId("561c7bdd4315b18f9862adb4"}  shards:    "_id" "rs0""host" "rs0/10.10.1.55:40000,10.10.1.57:40001" }    "_id" "rs1""host" "rs1/10.10.1.56:40003,10.10.1.57:40004" }  databases:    "_id" "admin""partitioned" false"primary" "config" }    "_id" "test""partitioned" false"primary" "rs0" }    "_id" "people""partitioned" true"primary" "rs0" }        people.customers            shard key: { "country" : 1, "_id" : 1 }            chunks:                rs0    1            "country" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "country" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : rs0 Timestamp(1, 0)
现在由于数据量不多只有一个分片在rs0上,可以通过增加数据量来提高分片:
12for(var i=10;i<10000;i++) db.customers.insert({name:"ham"+i,country:"UK"})for(var i=10;i<10000;i++) db.customers.insert({name:"ramda"+i,country:"Malaysia"})
mongos> sh.status()
--- Sharding Status ---
  sharding version: {
  "_id" : 1,
  "version" : 4,
  "minCompatibleVersion" : 4,
  "currentVersion" : 5,
  "clusterId" : ObjectId("561c7bdd4315b18f9862adb4")
}
  shards:
  {  "_id" : "rs0",  "host" : "rs0/10.10.1.55:40000,10.10.1.57:40001" }
  {  "_id" : "rs1",  "host" : "rs1/10.10.1.56:40003,10.10.1.57:40004" }
  databases:
  {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
  {  "_id" : "test",  "partitioned" : false,  "primary" : "rs0" }
  {  "_id" : "people",  "partitioned" : true,  "primary" : "rs0" }
      people.customers
          shard key: { "country" : 1, "_id" : 1 }
          chunks:
              rs1  2
              rs0  1
          { "country" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "country" : "American", "_id" : ObjectId("561c7da73af7c7865defefb1") } on : rs1 Timestamp(2, 0)
          { "country" : "American", "_id" : ObjectId("561c7da73af7c7865defefb1") } -->> { "country" : "UK", "_id" : ObjectId("561c7db63af7c7865defefd4") } on : rs0 Timestamp(3, 1)
          { "country" : "UK", "_id" : ObjectId("561c7db63af7c7865defefd4") } -->> { "country" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : rs1 Timestamp(3, 0)

现在rs0上有一个分片,rs1上有两个分片更多MongoDB相关内容可以看看以下的有用链接: MongoDB 3.0 正式版发布下载  http://www.linuxidc.com/Linux/2015-03/114414.htmCentOS编译安装MongoDB http://www.linuxidc.com/Linux/2012-02/53834.htmCentOS 编译安装 MongoDB与mongoDB的php扩展 http://www.linuxidc.com/Linux/2012-02/53833.htmCentOS 6 使用 yum 安装MongoDB及服务器端配置 http://www.linuxidc.com/Linux/2012-08/68196.htmUbuntu 13.04下安装MongoDB2.4.3 http://www.linuxidc.com/Linux/2013-05/84227.htmMongoDB入门必读(概念与实战并重) http://www.linuxidc.com/Linux/2013-07/87105.htmUbunu 14.04下MongoDB的安装指南 http://www.linuxidc.com/Linux/2014-08/105364.htm《MongoDB 权威指南》(MongoDB: The Definitive Guide)英文文字版[PDF] http://www.linuxidc.com/Linux/2012-07/66735.htmNagios监控MongoDB分片集群服务实战 http://www.linuxidc.com/Linux/2014-10/107826.htm基于CentOS 6.5操作系统搭建MongoDB服务 http://www.linuxidc.com/Linux/2014-11/108900.htmMongoDB 的详细介绍:请点这里
MongoDB 的下载地址:请点这里本文永久更新链接地址