shilang 发表于 2018-10-26 06:20:56

mongodb 集群搭建

  Sharding+Replica Sets
  主机IP服务及端口
  Server A192.168.31.231                  mongod shard1_1:27017
  mongod shard2_1:27018
  mongod config1:20000
  mongs1:30000
  Server B192.168.31.232                  mongod shard1_2:27017
  mongod shard2_2:27018
  mongod config2:20000
  mongs2:30000
  Server C192.168.31.233                  mongod shard1_3:27017
  mongod shard2_3:27018
  mongod config3:20000
  mongs3:30000
  关闭3台服务器的防火墙
  > rs.initiate(config)
  1.1 创建数据目录
  Server A
  mkdir /data/shard1_1
  mkdir /data/shard2_1
  mkdir /data/config
  Server B
  mkdir /data/shard1_2
  mkdir /data/shard2_2
  mkdir /data/config
  Server C
  mkdir /data/shard1_3
  mkdir /data/shard2_3
  mkdir /data/config
  1.2 配置shard1的Replica Sets
  Server A
  # /usr/local/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_1 --logpath /data/shard1_1/shard1_1.log --logappend --fork
  Server B
  # /usr/local/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_2 --logpath /data/shard1_2/shard1_2.log --logappend --fork
  Server C
  # /usr/local/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_3 --logpath /data/shard1_3/shard1_3.log --logappend --fork
  用 mongo 连接其中一台机器的 27017 端口的 mongod,初始化 Replica Sets“shard1” ,执行:
  # ./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.31.231:27017'},
  ... {_id: 1, host: '192.168.31.232:27017'},
  ... {_id: 2, host: '192.168.31.233:27017',arbiterOnly:true}]
  ... }
  {
  "_id" : "shard1",
  "members" : [
  {
  "_id" : 0,
  "host" : "192.168.31.231:27017"
  },
  {
  "_id" : 1,
  "host" : "192.168.31.232:27017"
  },
  {
  "_id" : 2,
  "host" : "192.168.31.233:27017",
  "arbiterOnly":true
  }
  ]
  }
  > rs.initiate(config)
  {
  "info" : "Config now saved locally. Should come online in about a minute.",
  "ok" : 1
  }
  1.3配置 shard2所用到的 Replica Sets
  Server A
  # /usr/local/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_1 --logpath /data/shard2_1/shard2_1.log --logappend --fork
  Server B
  # /usr/local/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_2 --logpath /data/shard2_2/shard2_2.log --logappend --fork
  Server C
  # /usr/local/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_3 --logpath /data/shard2_3/shard2_3.log --logappend --fork
  用 mongo 连接其中一台机器的 27018 端口的 mongod,初始化 Replica Sets “shard2” ,执行:
  # ./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.31.231:27018'},
  ... {_id: 1, host: '192.168.31.232:27018'},
  ... {_id: 2, host: '192.168.31.233:27018',arbiterOnly:true}]
  ... }
  {
  "_id" : "shard2",
  "members" : [
  {
  "_id" : 0,
  "host" : "192.168.31.231:27018"
  },
  {
  "_id" : 1,
  "host" : "192.168.31.232:27018"
  },
  {
  "_id" : 2,
  "host" : "192.168.31.233:27018",
  "arbiterOnly":true
  }
  ]
  }
  > rs.initiate(config)
  { "ok" : 1 }
  1.4 配置 3台 台 Config Server
  在 Server A 、B 、C上 执行:
  /usr/local/mongo/bin/mongod--configsvr--dbpath/data/config--port20000--logpath /data/config/config.log --logappend --fork
  23.4配置 3台 台 Route Process
  在 在 Server A 、B 、C上 执行:
  /usr/local/mongo/bin/mongos--configdb 192.168.31.231:20000,192.168.31.232:20000,192.168.31.233:20000 --port 30000 --chunkSize 1 --logpath /data/mongos.log --logappend --fork
  # netstat -tpln|grep mongo
  tcp      0      0 0.0.0.0:30000               0.0.0.0:*                   LISTEN      1573/mongos
  tcp      0      0 0.0.0.0:20000               0.0.0.0:*                   LISTEN      1375/mongod
  tcp      0      0 0.0.0.0:27017               0.0.0.0:*                   LISTEN      1061/mongod
  tcp      0      0 0.0.0.0:27018               0.0.0.0:*                   LISTEN      1211/mongod
  # ps aux|grep mongo
  root      10612.04.1 3198136 41920 ?       Sl   21:02   0:35 /usr/local/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_1 --logpath /data/shard1_1/shard1_1.log --logappend --fork
  root      12112.57.6 3202220 77716 ?       Sl   21:09   0:33 /usr/local/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_1 --logpath /data/shard2_1/shard2_1.log --logappend --fork
  root      13751.33.7 487476 38484 ?      Sl   21:17   0:12 /usr/local/mongo/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath /data/config/config.log --logappend --fork
  root      15730.80.7 2401527320 ?      Sl   21:28   0:02 /usr/local/mongo/bin/mongos --configdb 192.168.31.231:20000,192.168.31.232:20000,192.168.31.233:20000 --port 30000 --chunkSize 1 --logpath /data/mongos.log --logappend --fork
  root      17940.00.0 103244   848 pts/1    S+   21:31   0:00 grep mongo
  1.5配置 Shard Cluster
  连接到其中一台机器的端口 30000 的 mongos 进程,并切换到 admin 数据库做以下配置
  # ./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.31.231:27017,192.168.31.232:27017,192.168.31.233:27017"});
  { "shardAdded" : "shard1", "ok" : 1 }
  >db.runCommand({addshard:"shard2/192.168.31.231:27018,192.168.31.232:27018,192.168.31.233:27018"});
  { "shardAdded" : "shard2", "ok" : 1 }
  >
  进入mongos进程config库可以看到目前分片的情况:
  mongos> use config
  switched to db config
  mongos> db.shards.find()
  { "_id" : "shard1", "host" : "shard1/192.168.31.231:27017,192.168.31.232:27017,192.168.31.233:27017" }
  { "_id" : "shard2", "host" : "shard2/192.168.31.231:27018,192.168.31.232:27018,192.168.31.233:27018" }
  mongos>
  1.6激活数据库及集合的分片
  db.runCommand({ enablesharding:"test" })
  db.runCommand({ shardcollection: "test.users", key: { _id:1 }})
  1.7验证 Sharding正常工作
  连接到其中一台机器的端口 30000 的 mongos 进程,并切换到 test 数据库,以便添加测试数
  据
  use test
  for(var i=1;i
  ./mongo --port 27017
  shard2:PRIMARY> use test
  switched to db test
  shard2:PRIMARY> show collections;
  system.indexes
  users
  shard2:PRIMARY> db.users.find().count()
  11303--和db.users.stats()查看的数据条数一样 20000=11303+8697
  shard2:PRIMARY>
  ./mongo --port 27018
  shard2:PRIMARY> use test
  switched to db test
  shard2:PRIMARY> show collections;
  system.indexes
  users
  shard1:PRIMARY> db.users.find().count()
  8697   ----和db.users.stats()查看的数据条数一样
  shard1:PRIMARY>
  从SECONDARY读取数据:
  shard2:SECONDARY> show collections;
  2015-06-24T21:46:11.634+0800 E QUERY    Error: listCollections failed: { "note" : "from execCommand", "ok" : 0, "errmsg" : "not master" }
  查询时报错,说明SECONDARY节点不能执行查询操作。
  执行db.getMongo().setSlaveOk(),从库就可以进行读操作了,可以控制从哪些分片上面可以读。
  shard1:
  shard2:SECONDARY> db.getMongo().setSlaveOk()
  shard2:SECONDARY> show collections;
  system.indexes
  users
  shard2:
  shard1:SECONDARY> db.getMongo().setSlaveOk()
  shard1:SECONDARY> show collections;
  system.indexes
  users
  shard1:SECONDARY> db.users.find().count();
  8697
  shard1:SECONDARY>
  模拟故障:
  # ps aux|grep mongo
  root      10611.98.1 3200192 82956 ?       Sl   21:02   0:56 /usr/local/mongo/bin/mongod --shardsvr --replSet shard1 --port 27017 --dbpath /data/shard1_1 --logpath /data/shard1_1/shard1_1.log --logappend --fork
  root      12112.17.8 3205304 79928 ?       Sl   21:09   0:52 /usr/local/mongo/bin/mongod --shardsvr --replSet shard2 --port 27018 --dbpath /data/shard2_1 --logpath /data/shard2_1/shard2_1.log --logappend --fork
  root      13751.84.1 489528 42180 ?      Sl   21:17   0:37 /usr/local/mongo/bin/mongod --configsvr --dbpath /data/config --port 20000 --logpath /data/config/config.log --logappend --fork
  root      15730.90.7 2401527340 ?      Sl   21:28   0:13 /usr/local/mongo/bin/mongos --configdb 192.168.31.231:20000,192.168.31.232:20000,192.168.31.233:20000 --port 30000 --chunkSize 1 --logpath /data/mongos.log --logappend --fork
  root      19010.01.8 721952 19132 pts/0    Sl+21:38   0:00 ./mongo --port 27018
  root      21300.00.0 103244   848 pts/1    S+   21:51   0:00 grep mongo
  # kill -2 1061--这里不能使用kill -9 ,否则会导致丢失数据
  通过rs.status()可以查看复制集的状态:
  我们可以看到192.168.31.231:27017处于宕机状态,192.168.31.233:27017接管变成了PRIMARY
  shard1:PRIMARY>rs.status()
  {
  "set" : "shard1",
  "date" : ISODate("2015-06-24T14:03:10.425Z"),
  "myState" : 1,
  "members" : [
  {
  "_id" : 0,
  "name" : "192.168.31.231:27017",
  "health" : 0,
  "state" : 8,
  "stateStr" : "(not reachable/healthy)",
  "uptime" : 0,
  "optime" : Timestamp(0, 0),
  "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
  "lastHeartbeat" : ISODate("2015-06-24T14:03:09.083Z"),
  "lastHeartbeatRecv" : ISODate("2015-06-24T13:51:38.834Z"),
  "pingMs" : 0,
  "lastHeartbeatMessage" : "Failed attempt to connect to 192.168.31.231:27017; couldn't connect to server 192.168.31.231:27017 (192.168.31.231), connection attempt failed",
  "configVersion" : -1
  },
  {
  "_id" : 1,
  "name" : "192.168.31.232:27017",
  "health" : 1,
  "state" : 2,
  "stateStr" : "SECONDARY",
  "uptime" : 3242,
  "optime" : Timestamp(1435152718, 13),
  "optimeDate" : ISODate("2015-06-24T13:31:58Z"),
  "lastHeartbeat" : ISODate("2015-06-24T14:03:09.814Z"),
  "lastHeartbeatRecv" : ISODate("2015-06-24T14:03:09.813Z"),
  "pingMs" : 1,
  "configVersion" : 1
  },
  {
  "_id" : 2,
  "name" : "192.168.31.233:27017",
  "health" : 1,
  "state" : 1,
  "stateStr" : "PRIMARY",
  "uptime" : 3603,
  "optime" : Timestamp(1435152718, 13),
  "optimeDate" : ISODate("2015-06-24T13:31:58Z"),
  "electionTime" : Timestamp(1435153900, 1),
  "electionDate" : ISODate("2015-06-24T13:51:40Z"),
  "configVersion" : 1,
  "self" : true
  }
  ],
  "ok" : 1,
  "$gleStats" : {
  "lastOpTime" : Timestamp(0, 0),
  "electionId" : ObjectId("558ab5ec28f234f7b5abda48")
  }
  }
  查看分片集合信息:
  mongos> db.collections.find()
  { "_id" : "test.users", "lastmod" : ISODate("2015-06-24T13:30:19.670Z"), "dropped" : false, "key" : { "_id" : 1 }, "unique" : false, "lastmodEpoch" : ObjectId("558ab0eb6a881136d7047be4") }

页: [1]
查看完整版本: mongodb 集群搭建