qwe3223678qwe 发表于 2018-10-24 11:13:42

MongoDB分片群集的部署(用心描述,详细易懂)!!

# mongo  mongos> use school
  switched to db school
  mongos> for(var i=1;i show dbs
  config0.031GB
  school0.078GB
  mongos> use school
  switched to db school
  mongos> show collections
  system.indexes
  users
  mongos> db.users.find().limit(10)//查看头十行内容
  { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4ae"), "id" : 1, "name" : "jack1" }
  { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4af"), "id" : 2, "name" : "jack2" }
  { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b0"), "id" : 3, "name" : "jack3" }
  { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b1"), "id" : 4, "name" : "jack4" }
  { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b2"), "id" : 5, "name" : "jack5" }
  { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b3"), "id" : 6, "name" : "jack6" }
  { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b4"), "id" : 7, "name" : "jack7" }
  { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b5"), "id" : 8, "name" : "jack8" }
  { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b6"), "id" : 9, "name" : "jack9" }
  { "_id" : ObjectId("5b4f6a24ef33d588c0dbc4b7"), "id" : 10, "name" : "jack10" }
  mongos> sh.status()             //查看数据库分片信息
  --- Sharding Status ---
  sharding version: {
  "_id" : 1,
  "minCompatibleVersion" : 5,
  "currentVersion" : 6,
  "clusterId" : ObjectId("5b4f68599dcf397cc4e7c598")
  }
  shards:
  {"_id" : "shard0000","host" : "192.168.200.142:47017" }
  {"_id" : "shard0001","host" : "192.168.200.142:47018" }
  active mongoses:
  "3.2.1" : 1
  balancer:
  Currently enabled:yes
  Currently running:no
  Failed balancer rounds in last 5 attempts:0
  Migration Results for the last 24 hours:
  No recent migrations
  databases:
  {"_id" : "school","primary" : "shard0000","partitioned" : false }
  mongos> sh.enableSharding("school") //启用数据库分片
  { "ok" : 1 }
  mongos> sh.status()
  --- Sharding Status ---
  sharding version: {
  "_id" : 1,
  "minCompatibleVersion" : 5,
  "currentVersion" : 6,
  "clusterId" : ObjectId("5b4f68599dcf397cc4e7c598")
  }
  shards:
  {"_id" : "shard0000","host" : "192.168.200.142:47017" }
  {"_id" : "shard0001","host" : "192.168.200.142:47018" }
  active mongoses:
  "3.2.1" : 1
  balancer:
  Currently enabled:yes
  Currently running:no
  Failed balancer rounds in last 5 attempts:0
  Migration Results for the last 24 hours:
  No recent migrations
  databases:
  {"_id" : "school","primary" : "shard0000","partitioned" : true }
  mongos> db.users.createIndex({"id":1})//对users表创建索引
  {
  "raw" : {
  "192.168.200.142:47017" : {
  "createdCollectionAutomatically" : false,
  "numIndexesBefore" : 1,
  "numIndexesAfter" : 2,
  "ok" : 1
  }
  },
  "ok" : 1
  }
  mongos> sh.shardCollection("school.users",{"id":1})//表分片
  { "collectionsharded" : "school.users", "ok" : 1 }
  mongos> sh.status()
  --- Sharding Status ---
  sharding version: {
  "_id" : 1,
  "minCompatibleVersion" : 5,
  "currentVersion" : 6,
  "clusterId" : ObjectId("5b4f68599dcf397cc4e7c598")
  }
  shards:
  {"_id" : "shard0000","host" : "192.168.200.142:47017" }
  {"_id" : "shard0001","host" : "192.168.200.142:47018" }
  active mongoses:
  "3.2.1" : 1
  balancer:
  Currently enabled:yes
  Currently running:no
  Failed balancer rounds in last 5 attempts:0
  Migration Results for the last 24 hours:
  No recent migrations
  databases:
  {"_id" : "school","primary" : "shard0000","partitioned" : true }
  school.users
  shard key: { "id" : 1 }
  unique: false
  balancing: true
  chunks:
  shard0000   3
  { "id" : { "$minKey" : 1 } } -->> { "id" : 4682 } on : shard0000 Timestamp(1, 0)
  { "id" : 4682 } -->> { "id" : 9364 } on : shard0000 Timestamp(1, 1)
  { "id" : 9364 } -->> { "id" : { "$maxKey" : 1 } } on : shard0000 Timestamp(1, 2)      //分片成功

页: [1]
查看完整版本: MongoDB分片群集的部署(用心描述,详细易懂)!!