广告
返回顶部
首页 > 资讯 > 数据库 >MongoDB 3 分片集群安装配置
  • 960
分享到

MongoDB 3 分片集群安装配置

2024-04-02 19:04:59 960人浏览 薄情痞子
摘要

操作系统:Centos 6   x86_64mongoDB版本:3.4.3集群主机拓扑:主机monGo shardsvr & ReplSetNamemongo configsvr 

操作系统Centos 6   x86_64

mongoDB版本:3.4.3

集群主机拓扑:

主机monGo shardsvr & ReplSetName
mongo configsvr & ReplSetNamemongos
test1.lanshard-a    shard-b


test2.lanshard-a    shard-b

test3.lanshard-a    shard-b

test4.lan

cfgshard
test5.lan

cfgshard
test6.lan

cfgshard
test7.lan


yes

test1-3  分别在一台主机上启动两个不同副本集名称的mongod实例。

test4-6  三台主机作为 config server 单独运行。

test7     主机作为 mongos 路由主机。

MongoDB 3 分片集群安装配置

  1. 安装 MongoDB 

    1. 配置 repo 源

      [mongodb-org-3.4]
      name=MongoDB Repository
      #baseurl=https://repo.mongodb.org/yum/redhat//mongodb-org/3.4/x86_64/
      baseurl=Https://mirrors.aliyun.com/mongodb/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
      gpGCheck=0
      enabled=1
      gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc

      选择国内 阿里云 镜像资源。

    2. # yum install mongodb-org -y
    3. 配置 /etc/mongod.conf

      # mongod.conf
      
      # for documentation of all options, see:
      #   http://docs.mongodb.org/manual/reference/configuration-options/
      
      # where to write logging data.
      systemLog:
        destination: file
        logAppend: true
        path: /var/log/mongodb/mongod.log
      
      # Where and how to store data.
      storage:
        dbPath: /var/lib/mongo
        journal:
          enabled: true
      #  engine:
      #  mmapv1:
      #  wiredTiger:
      
      # how the process runs
      proceSSManagement:
        fork: true  # fork and run in background
        pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
      
      # network interfaces
      net:
        port: 27017
        bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.
      
      
      #security:
      
      #operationProfiling:
      
      replication:
          replSetName: shard-a        
      
      sharding:
          clusterRole: shardsvr
      
      ## Enterprise-Only Options
      
      #auditLog:
      
      #snmp:

      replication 处配置 副本集 名,sharding 开启 shardsvr 模式。

  2.  启动 mongod 服务

    [root@test1 ~]# service mongod start
    Starting mongod:                                           [  OK  ]
    
    [root@test2 ~]# service mongod start
    Starting mongod:                                           [  OK  ]
    
    [root@test3 ~]# service mongod start
    Starting mongod:                                           [  OK  ]
  3.  配置 shard-a 副本集

    [root@test1 ~]# mongo test1.lan:27017
    MongoDB shell version v3.4.3
    connecting to: test1.lan:27017
    MongoDB server version: 3.4.3
    Server has startup warnings: 
    2017-04-24T22:46:19.703+0800 I STORAGE  [initandlisten] 
    2017-04-24T22:46:19.703+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
    2017-04-24T22:46:19.703+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
    2017-04-24T22:46:20.321+0800 I CONTROL  [initandlisten] 
    > rs.initiate()
    {
    	"info2" : "no configuration specified. Using a default configuration for the set",
    	"me" : "test1.lan:27017",
    	"ok" : 1
    }
    shard-a:SECONDARY> 
    shard-a:PRIMARY> config  = rs.config()        # 保存配置对象
    {
    	"_id" : "shard-a",
    	"version" : 1,
    	"protocolVersion" : NumberLong(1),
    	"members" : [
    		{
    			"_id" : 0,
    			"host" : "test1.lan:27017",
    			"arbiterOnly" : false,
    			"buildIndexes" : true,
    			"hidden" : false,
    			"priority" : 1,
    			"tags" : {
    				
    			},
    			"slaveDelay" : NumberLong(0),
    			"votes" : 1
    		}
    	],
    	"settings" : {
    		"chainingAllowed" : true,
    		"heartbeatIntervalMillis" : 2000,
    		"heartbeatTimeoutSecs" : 10,
    		"electionTimeoutMillis" : 10000,
    		"catchUpTimeoutMillis" : 2000,
    		"getLastErrORModes" : {
    			
    		},
    		"getLastErrorDefaults" : {
    			"w" : 1,
    			"wtimeout" : 0
    		},
    		"replicaSetId" : ObjectId("58fe111823612a418eb7f3fc")
    	}
    }
    shard-a:PRIMARY> config.members[0].priority = 2        # 这里增加自身主机的优先级为 2,防止后面 PRIMARY 重新选举到其余主机
    2
    shard-a:PRIMARY> rs.reconfig(config)                    # 重新应用该配置
    { "ok" : 1 }
    shard-a:PRIMARY> rs.add("test2.lan:27017")            # 添加副本集主机
    { "ok" : 1 }
    shard-a:PRIMARY> rs.add("test3.lan")                # 添加副本集主机(默认端口为 27017)
    { "ok" : 1 }
    shard-a:PRIMARY> rs.config()
    {
    	"_id" : "shard-a",
    	"version" : 4,
    	"protocolVersion" : NumberLong(1),
    	"members" : [
    		{
    			"_id" : 0,
    			"host" : "test1.lan:27017",
    			"arbiterOnly" : false,
    			"buildIndexes" : true,
    			"hidden" : false,
    			"priority" : 2,
    			"tags" : {
    				
    			},
    			"slaveDelay" : NumberLong(0),
    			"votes" : 1
    		},
    		{
    			"_id" : 1,
    			"host" : "test2.lan:27017",
    			"arbiterOnly" : false,
    			"buildIndexes" : true,
    			"hidden" : false,
    			"priority" : 1,
    			"tags" : {
    				
    			},
    			"slaveDelay" : NumberLong(0),
    			"votes" : 1
    		},
    		{
    			"_id" : 2,
    			"host" : "test3.lan:27017",
    			"arbiterOnly" : false,
    			"buildIndexes" : true,
    			"hidden" : false,
    			"priority" : 1,
    			"tags" : {
    				
    			},
    			"slaveDelay" : NumberLong(0),
    			"votes" : 1
    		}
    	],
    	"settings" : {
    		"chainingAllowed" : true,
    		"heartbeatIntervalMillis" : 2000,
    		"heartbeatTimeoutSecs" : 10,
    		"electionTimeoutMillis" : 10000,
    		"catchUpTimeoutMillis" : 2000,
    		"getLastErrorModes" : {
    			
    		},
    		"getLastErrorDefaults" : {
    			"w" : 1,
    			"wtimeout" : 0
    		},
    		"replicaSetId" : ObjectId("58fe111823612a418eb7f3fc")
    	}
    }

    这样,副本集 shard-a 就配置完成

  4. 接下来我们启动并配置副本集 shard-b

    [root@test1 ~]# mkdir /var/lib/mongo2
    [root@test1 ~]# mongod --shardsvr --replSet shard-b --dbpath /var/lib/mongo2/ --port 37017 --logpath /var/log/mongodb/mongod2.log --fork --journal
    about to fork child process, waiting until server is ready for connections.
    forked process: 14323
    child process started successfully, parent exiting
    
    [root@test2 ~]# mkdir /var/lib/mongo2
    [root@test2 ~]# mongod --shardsvr --replSet shard-b --dbpath /var/lib/mongo2/ --port 37017 --logpath /var/log/mongodb/mongod2.log --fork --journal
    about to fork child process, waiting until server is ready for connections.
    forked process: 5623
    child process started successfully, parent exiting
    
    [root@test3 ~]# mkdir /var/lib/mongo2
    [root@test3 ~]# mongod --shardsvr --replSet shard-b --dbpath /var/lib/mongo2/ --port 37017 --logpath /var/log/mongodb/mongod2.log --fork --journal
    about to fork child process, waiting until server is ready for connections.
    forked process: 4303
    child process started successfully, parent exiting
  5. 配置 shard-b 副本集

    [root@test1 ~]# mongo test1.lan:37017
    MongoDB shell version v3.4.3
    connecting to: test1.lan:37017
    MongoDB server version: 3.4.3
    Server has startup warnings: 
    2017-04-24T22:59:43.476+0800 I STORAGE  [initandlisten] 
    2017-04-24T22:59:43.476+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
    2017-04-24T22:59:43.476+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
    2017-04-24T22:59:44.019+0800 I CONTROL  [initandlisten] 
    > rs.initiate()
    {
    	"info2" : "no configuration specified. Using a default configuration for the set",
    	"me" : "test1.lan:37017",
    	"ok" : 1
    }
    shard-b:SECONDARY> 
    shard-b:PRIMARY> config = rs.config()
    {
    	"_id" : "shard-b",
    	"version" : 1,
    	"protocolVersion" : NumberLong(1),
    	"members" : [
    		{
    			"_id" : 0,
    			"host" : "test1.lan:37017",
    			"arbiterOnly" : false,
    			"buildIndexes" : true,
    			"hidden" : false,
    			"priority" : 1,
    			"tags" : {
    				
    			},
    			"slaveDelay" : NumberLong(0),
    			"votes" : 1
    		}
    	],
    	"settings" : {
    		"chainingAllowed" : true,
    		"heartbeatIntervalMillis" : 2000,
    		"heartbeatTimeoutSecs" : 10,
    		"electionTimeoutMillis" : 10000,
    		"catchUpTimeoutMillis" : 2000,
    		"getLastErrorModes" : {
    			
    		},
    		"getLastErrorDefaults" : {
    			"w" : 1,
    			"wtimeout" : 0
    		},
    		"replicaSetId" : ObjectId("58fe1465f7a2e985d87b8bf8")
    	}
    }
    shard-b:PRIMARY> config.members[0].priority = 2
    2
    shard-b:PRIMARY> rs.reconfig(config)
    { "ok" : 1 }
    shard-b:PRIMARY> rs.add("test2.lan:37017")
    { "ok" : 1 }
    shard-b:PRIMARY> rs.add("test3.lan:37017")
    { "ok" : 1 }
    shard-b:PRIMARY> rs.isMaster()
    {
    	"hosts" : [
    		"test1.lan:37017",
    		"test2.lan:37017",
    		"test3.lan:37017"
    	],
    	"setName" : "shard-b",
    	"setVersion" : 4,
    	"ismaster" : true,
    	"secondary" : false,
    	"primary" : "test1.lan:37017",
    	"me" : "test1.lan:37017",
    	"electionId" : ObjectId("7fffffff0000000000000001"),
    	"lastWrite" : {
    		"opTime" : {
    			"ts" : Timestamp(1493046429, 1),
    			"t" : NumberLong(1)
    		},
    		"lastWriteDate" : ISODate("2017-04-24T15:07:09Z")
    	},
    	"maxBsonObjectSize" : 16777216,
    	"maxMessageSizeBytes" : 48000000,
    	"maxWriteBatchSize" : 1000,
    	"localTime" : ISODate("2017-04-24T15:07:24.475Z"),
    	"maxWireVersion" : 5,
    	"minWireVersion" : 0,
    	"readOnly" : false,
    	"ok" : 1
    }

    这样 shard-a   shard-b   两个副本集已经配置完成

  6. 开始配置 config server,MongoDB 从3.2版本之后开始规定 config server 也必须要开启副本集功能。
    config server 的配置文件如下:config server 一般情况下是监听在 27019 端口

    # mongod.conf
    
    # for documentation of all options, see:
    #   http://docs.mongodb.org/manual/reference/configuration-options/
    
    # where to write logging data.
    systemLog:
      destination: file
      logAppend: true
      path: /var/log/mongodb/mongod.log
    
    # Where and how to store data.
    storage:
      dbPath: /var/lib/mongo
      journal:
        enabled: true
    #  engine:
    #  mmapv1:
    #  wiredTiger:
    
    # how the process runs
    processManagement:
      fork: true  # fork and run in background
      pidFilePath: /var/run/mongodb/mongod.pid  # location of pidfile
    
    # network interfaces
    net:
      port: 27019
      bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.
    
    
    #security:
    
    #operationProfiling:
    
    replication:
      replSetName: cfgReplSet
    
    sharding:
      clusterRole: configsvr
    
    ## Enterprise-Only Options
    
    #auditLog:
    
    #snmp:
  7. 启动三台config server 的mongod 服务

    [root@test4 ~]# service mongod start
    Starting mongod:                                           [  OK  ]
    
    [root@test5 ~]# service mongod start
    Starting mongod:                                           [  OK  ]
    
    [root@test6 ~]# service mongod start
    Starting mongod:                                           [  OK  ]

    同样,config server 的副本集配置如上文所示,这里的代码就省略了。

  8. 配置启动 mongos 路由主机

    [root@test7 ~]# mongos --configdb cfgReplSet/test4.lan,test5.lan,test6.lan:27019 --logpath /var/log/mongodb/mongos.log --fork --port 30000
    about to fork child process, waiting until server is ready for connections.
    forked process: 3338
    child process started successfully, parent exiting

    MongoDB 版本 >3.2 启动mongos 的时候,需要跟上 config server 的副本集名称 (这里是 cfgReplSet

  9. 连接 mongos 测试

    [root@test7 ~]# mongo test7.lan:30000
    MongoDB shell version v3.4.4
    connecting to: test7.lan:30000
    MongoDB server version: 3.4.4
    Server has startup warnings: 
    2017-04-24T23:30:47.285+0800 I CONTROL  [main] 
    2017-04-24T23:30:47.285+0800 I CONTROL  [main] ** WARNING: Access control is not enabled for the database.
    2017-04-24T23:30:47.285+0800 I CONTROL  [main] **          Read and write access to data and configuration is unrestricted.
    2017-04-24T23:30:47.285+0800 I CONTROL  [main] ** WARNING: You are running this process as the root user, which is not recommended.
    2017-04-24T23:30:47.285+0800 I CONTROL  [main] 
    mongos> show dbs
    admin   0.000GB
    config  0.000GB
    mongos> use config
    switched to db config
    mongos> show collections
    chunks
    lockpings
    locks
    migrations
    mongos
    shards
    tags
    version
    mongos> db.shards.find()
    # 这里没有返回文档,也说明分片集群中并没有添加可用分片集群。
  10. 配置分片集群:shard

    mongos> sh.addShard("shard-a/test1.lan,test2.lan,test3.lan")
    { "shardAdded" : "shard-a", "ok" : 1 }
    
    mongos> db.shards.find()
    { "_id" : "shard-a", "host" : "shard-a/test1.lan:27017,test2.lan:27017,test3.lan:27017", "state" : 1 }
    
    mongos> sh.addShard("shard-b/test1.lan:37017,test2.lan:37017,test3.lan:37017")
    { "shardAdded" : "shard-b", "ok" : 1 }
    
    mongos> db.shards.find()
    { "_id" : "shard-a", "host" : "shard-a/test1.lan:27017,test2.lan:27017,test3.lan:27017", "state" : 1 }
    { "_id" : "shard-b", "host" : "shard-b/test1.lan:37017,test2.lan:37017,test3.lan:37017", "state" : 1 }
    # 检查分片集群的分片副本集数量,方法一
    
    mongos> db.getSiblingDB('config').shards.find()
    { "_id" : "shard-a", "host" : "shard-a/test1.lan:27017,test2.lan:27017,test3.lan:27017", "state" : 1 }
    { "_id" : "shard-b", "host" : "shard-b/test1.lan:37017,test2.lan:37017,test3.lan:37017", "state" : 1 }
    # 检查分片集群的分片副本集数量,方法二
    
    mongos> use admin
    switched to db admin
    mongos> db.runCommand({listshards: 1})
    {
    	"shards" : [
    		{
    			"_id" : "shard-a",
    			"host" : "shard-a/test1.lan:27017,test2.lan:27017,test3.lan:27017",
    			"state" : 1
    		},
    		{
    			"_id" : "shard-b",
    			"host" : "shard-b/test1.lan:37017,test2.lan:37017,test3.lan:37017",
    			"state" : 1
    		}
    	],
    	"ok" : 1
    }
    # 检查分片集群的分片副本集数量,方法三
  11. 配置分片集合:接下来的步骤就是在数据库上启动分片。分片不会自动完成,而是需要在数据库里提前为集合做好设置才行。

    mongos> sh.enableSharding("test2_db")        # 该库可以是已存在的,也可以是暂不存在的
    { "ok" : 1 }
    
    mongos> db.getSiblingDB("config").databases.find()
    { "_id" : "test2_db", "primary" : "shard-a", "partitioned" : true }
    # sharding 分片库的配置库 databases 集合已经有相应的配置记录了。
    
    mongos> sh.status()
    --- Sharding Status --- 
      sharding version: {
    	"_id" : 1,
    	"minCompatibleVersion" : 5,
    	"currentVersion" : 6,
    	"clusterId" : ObjectId("58fe17e90b3Df66581ff6b09")
    }
      shards:
    	{  "_id" : "shard-a",  "host" : "shard-a/test1.lan:27017,test2.lan:27017,test3.lan:27017",  "state" : 1 }
    	{  "_id" : "shard-b",  "host" : "shard-b/test1.lan:37017,test2.lan:37017,test3.lan:37017",  "state" : 1 }
      active mongoses:
    	"3.4.4" : 1
     autosplit:
    	Currently enabled: yes
      balancer:
    	Currently enabled:  yes
    	Currently running:  no
    		Balancer lock taken at Mon Apr 24 2017 23:21:13 GMT+0800 (CST) by ConfigServer:Balancer
    	Failed balancer rounds in last 5 attempts:  0
    	Migration Results for the last 24 hours: 
    		No recent migrations
      databases:
    	{  "_id" : "test2_db",  "primary" : "shard-a",  "partitioned" : true }
    # 分片状态中更能方便的查看当前分片的状态信息,包括分片集群成员 以及 分片数据库,分片机制等。
    
    mongos> sh.shardCollection("test2_db.users", {username: 1, _id: 1})        
    { "collectionsharded" : "test2_db.users", "ok" : 1 }
    # 此处,我们选择 username _id 作为组合分片键,组合分片键必须是一个索引
    # 如果集合为空,那么该行命令会自动在集合中创建该索引,如果集合已存在对应数据,且该组合键的索引没有事先创建好,那么这条语句就会抛出错误
    # 需要手动到集合创建该组合的索引,之后才能作为分片键
    
    mongos> db.getSiblingDB("config").collections.find().pretty()
    {
    	"_id" : "test2_db.users",
    	"lastmodEpoch" : ObjectId("58fe21de224dc86230e9a8f7"),
    	"lastmod" : ISODate("1970-02-19T17:02:47.296Z"),
    	"dropped" : false,
    	"key" : {
    		"username" : 1,
    		"_id" : 1
    	},
    	"unique" : false
    }
    # 配置完成后,config.collections 就存在了相应集合的分片键信息。


来看看分片集合在单独分片副本集中的存在形式

首先需要找到该库已经被分配到了哪个分片之上(由于该库之前并没有数据,所以创建分片键的时候,会自动插入索引数据,自动按照默认配置路由到其中一个分片键集群之中)

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("58fe17e90b3df66581ff6b09")
}
  shards:
	{  "_id" : "shard-a",  "host" : "shard-a/test1.lan:27017,test2.lan:27017,test3.lan:27017",  "state" : 1 }
	{  "_id" : "shard-b",  "host" : "shard-b/test1.lan:37017,test2.lan:37017,test3.lan:37017",  "state" : 1 }
  active mongoses:
	"3.4.4" : 1
 autosplit:
	Currently enabled: yes
  balancer:
	Currently enabled:  yes
	Currently running:  no
		Balancer lock taken at Mon Apr 24 2017 23:21:13 GMT+0800 (CST) by ConfigServer:Balancer
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours: 
		No recent migrations
  databases:
	{  "_id" : "test2_db",  "primary" : "shard-a",  "partitioned" : true }
		test2_db.users
			shard key: { "username" : 1, "_id" : 1 }
			unique: false
			balancing: true
			chunks:
				shard-a	1
			{ "username" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "username" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : shard-a Timestamp(1, 0) 
# 最后这行 databases 看到了,该库的该数据块(chunks) 被分配到了 shard-a 副本集中,那么我们接下来就可以直接到 shard-a 中查看该库中users集合的文档信息。

# 登录到 shard-a 副本集中进行查看
shard-a:PRIMARY> show dbs
admin     0.000GB
local     0.000GB
test2_db  0.000GB
shard-a:PRIMARY> use test2_db
switched to db test2_db
shard-a:PRIMARY> db.users.find()    # 该集合暂时没有文档

shard-a:PRIMARY> db.users.getIndexes()        # 查看该集合的索引配置信息
[
	{
		"v" : 2,
		"key" : {
			"_id" : 1
		},
		"name" : "_id_",
		"ns" : "test2_db.users"
	},
	{
		"v" : 2,
		"key" : {
			"username" : 1,
			"_id" : 1
		},
		"name" : "username_1__id_1",
		"ns" : "test2_db.users"
	}    # 查看到了两个索引,第一个索引 _id 为系统默认添加的索引,第二个索引就是创建分片键的时候自动创建的组合键索引
]


写入数据到分片集群

# 首先创建一个数据对象,用来填充文档大小
mongos> data = new Array(2049).join("abcd ")
mongos> data.length
10240
# data 大小为 1MB

mongos> for (var i=0; i < 100; i++){
...         db.getSiblingDB("test2_db").users.insert({
...             username: "Join" + i,
...             age: i % 13 + 20,
...             data: data }
...         )
... }
WriteResult({ "nInserted" : 1 })
# 批量插入 100 条文档,每个文档约为 1MB 大小。

# 接下来看看有了这么多文档过后,会怎么分片。
mongos> db.getSiblingDB("config").chunks.count()
3
# 插入这么多数据以后,就会发现多了几个数据块。我们可以通过检查集合中的数据库的数量来验证这个猜想

mongos> db.getSiblingDB("config").chunks.find().pretty()
{
	"_id" : "test2_db.users-username_MinKey_id_MinKey",
	"lastmod" : Timestamp(2, 1),
	"lastmodEpoch" : ObjectId("58fe21de224dc86230e9a8f7"),
	"ns" : "test2_db.users",
	"min" : {
		"username" : { "$minKey" : 1 },
		"_id" : { "$minKey" : 1 }
	},
	"max" : {
		"username" : "Join1",
		"_id" : ObjectId("58fe293756525c8a54e2a5af")
	},
	"shard" : "shard-a"
}
{
	"_id" : "test2_db.users-username_\"Join1\"_id_ObjectId('58fe293756525c8a54e2a5af')",
	"lastmod" : Timestamp(1, 2),
	"lastmodEpoch" : ObjectId("58fe21de224dc86230e9a8f7"),
	"ns" : "test2_db.users",
	"min" : {
		"username" : "Join1",
		"_id" : ObjectId("58fe293756525c8a54e2a5af")
	},
	"max" : {
		"username" : "Join2",
		"_id" : ObjectId("58fe293756525c8a54e2a5b0")
	},
	"shard" : "shard-a"
}
{
	"_id" : "test2_db.users-username_\"Join2\"_id_ObjectId('58fe293756525c8a54e2a5b0')",
	"lastmod" : Timestamp(2, 0),
	"lastmodEpoch" : ObjectId("58fe21de224dc86230e9a8f7"),
	"ns" : "test2_db.users",
	"min" : {
		"username" : "Join2",
		"_id" : ObjectId("58fe293756525c8a54e2a5b0")
	},
	"max" : {
		"username" : { "$maxKey" : 1 },
		"_id" : { "$maxKey" : 1 }
	},
	"shard" : "shard-b"
}
# 查看每个数据块的详细分片信息,发现有两个块被存储在 shard-a 副本集中,还有一个数据块被存储在 shard-b 副本集中

# 我们也可以通过 sh.status() 来更直观的看到相关信息。
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
	"_id" : 1,
	"minCompatibleVersion" : 5,
	"currentVersion" : 6,
	"clusterId" : ObjectId("58fe17e90b3df66581ff6b09")
}
  shards:
	{  "_id" : "shard-a",  "host" : "shard-a/test1.lan:27017,test2.lan:27017,test3.lan:27017",  "state" : 1 }
	{  "_id" : "shard-b",  "host" : "shard-b/test1.lan:37017,test2.lan:37017,test3.lan:37017",  "state" : 1 }
  active mongoses:
	"3.4.4" : 1
 autosplit:
	Currently enabled: yes
  balancer:
	Currently enabled:  yes
	Currently running:  no
		Balancer lock taken at Mon Apr 24 2017 23:21:13 GMT+0800 (CST) by ConfigServer:Balancer
	Failed balancer rounds in last 5 attempts:  0
	Migration Results for the last 24 hours: 
		1 : Success
  databases:
	{  "_id" : "test2_db",  "primary" : "shard-a",  "partitioned" : true }
		test2_db.users
			shard key: { "username" : 1, "_id" : 1 }
			unique: false
			balancing: true
			chunks:
				shard-a	2
				shard-b	1
			{ "username" : { "$minKey" : 1 }, "_id" : { "$minKey" : 1 } } -->> { "username" : "Join1", "_id" : ObjectId("58fe293756525c8a54e2a5af") } on : shard-a Timestamp(2, 1) 
			{ "username" : "Join1", "_id" : ObjectId("58fe293756525c8a54e2a5af") } -->> { "username" : "Join2", "_id" : ObjectId("58fe293756525c8a54e2a5b0") } on : shard-a Timestamp(1, 2) 
			{ "username" : "Join2", "_id" : ObjectId("58fe293756525c8a54e2a5b0") } -->> { "username" : { "$maxKey" : 1 }, "_id" : { "$maxKey" : 1 } } on : shard-b Timestamp(2, 0) 
# 这个方法会打印所有的数据库信息,并且包含范围信息。


表象背后,MongoDB 底层依赖 2 个机制来保持集群的平衡:分割与迁移。


分割是把一个大的数据库分割为 2 个更小的数据块的过程。它只会在数据块大小超过最大限制的时候才会发生,目前的默认设置是 64MB。分割是必须的,因为数据块太大就难以在整个集合中分布。


迁移就是在分片之间移动数据块的过程。当某些分片服务器包含的数据块数量大大超过其他分片服务器就会触发迁移过程,这个触发器叫做迁移回合(migration round)。在一个迁移回合中,数据块从某些分片服务器迁移到其他分片服务器,直到集群看起来相对平衡为止。我们可以想象一下这两个操作,迁移比分割昂贵得多。


实际上,这些操作不应该影响我们,但是明白这一点非常有用,当遇到性能问题的时候就要想到可能它们正在迁移数据。如果插入的数据分布均匀,各个分片上的数据集应该差不多以相同的速度增长,则迁移应该不会频繁发生。

您可能感兴趣的文档:

--结束END--

本文标题: MongoDB 3 分片集群安装配置

本文链接: https://www.lsjlt.com/news/35990.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • MongoDB 3 分片集群安装配置
    操作系统:CentOS 6   x86_64MongoDB版本:3.4.3集群主机拓扑:主机mongo shardsvr & ReplSetNamemongo configsvr ...
    99+
    2022-10-18
  • mongodb分片集群生产环境怎么配置
    要配置MongoDB分片集群的生产环境,您需要执行以下步骤:1. 设计分片策略:确定如何划分数据片段和选择分片键。分片键是用于将数据...
    99+
    2023-08-23
    mongodb
  • mongodb 中怎么设置分片集群
    今天就跟大家聊聊有关mongodb 中怎么设置分片集群,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。1、先enable sharding,开启数据库...
    99+
    2022-10-18
  • Jboss集群的安装配置
    这篇文章主要介绍“Jboss集群的安装配置”,在日常操作中,相信很多人在Jboss集群的安装配置问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”Jboss集群的安装配置”的疑惑...
    99+
    2022-10-18
  • Nacos 2.2.0集群安装配置
    一、文档、下载 Nacos 快速开始 https://nacos.io/zh-cn/docs/quick-start.html 集群部署说明 https://nacos.io/zh-cn/docs/v2/guide/admin/cl...
    99+
    2023-09-03
    nacos 集群 内置数据库 mysql Powered by 金山文档
  • mongo副本集集群安装配置
    新建用户和目录 useradd mongodb mkdir  -p /comm/mg10000/data mkdir  -p /comm/mg10001/data mkdir...
    99+
    2022-10-18
  • MongoDB安装与副本集配置
    副本集是一组,由N个mongo节点组成并协同工作的,提供自动的故障集群转移。...
    99+
    2022-10-18
  • yum安装MySQL及集群配置
    在CentOS7中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB。1 下载并安装MySQL官方的 Yum Repos...
    99+
    2022-10-18
  • Weblogic Server11G集群的安装与配置
    本篇内容主要讲解“Weblogic Server11G集群的安装与配置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Weblogic Server11G集群的安装与配置”吧!环境OS:windo...
    99+
    2023-06-04
  • Docker ZooKeeper3.4.10集群安装配置过程
    目录一. 服务器规划二. 集群部署1. 配置1.1 创建宿主机映射目录1.2. 创建配置文件(3台zk节点配置文件一样)1.3. 配置zookeeper主机id,每个机器id不能相同...
    99+
    2022-11-13
  • 0、MySql第零章,安装及集群配置
    MySql第零章,安装及集群配置 一、MySql安装 1、RPM安装 RPM安装,无法自定义一些安装路径和配置文件路径 ##以后再填坑 2、Generic安装 二进制 预编译 Generic,下载地址: https:...
    99+
    2014-08-11
    MySql第零章,安装及集群配置
  • 集群Cluster MySQL的安装配置和使用
    MySQL Cluster MySQL集群基本概念:“NDB” 是一种“内存中”的存储引擎,它具有可用性高和数据一致性好的特点。 MySQL Cluster 能够使用多种故障切换和负载平衡选项配置NDB存储...
    99+
    2022-10-18
  • Hadoop多节点集群怎么安装配置
    本篇内容主要讲解“Hadoop多节点集群怎么安装配置”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Hadoop多节点集群怎么安装配置”吧!1、集群部署介绍 1.1 Hadoop简介 H...
    99+
    2023-06-03
  • FreeSwitch+Opensips集群 安装配置操作指导
    1.1 rsync安装服务端与客户端都需要安装rsync安装命令:yum install rsync1.1.2 rsync服务端配置步骤1:生成并编辑配置文件rsyncd.conf#touch /etc/rsyncd.c...
    99+
    2023-06-05
  • Docker下Redis集群安装配置怎么实现
    这篇文章主要介绍“Docker下Redis集群安装配置怎么实现”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Docker下Redis集群安装配置怎么实现”文章能帮助大家解决问题。一、所有机器拉去镜像...
    99+
    2023-07-02
  • hadoop集群服务器详细安装配置教程
    这篇文章主要讲解了“hadoop集群服务器详细安装配置教程”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“hadoop集群服务器详细安装配置教程”吧! 一、安装包准备1、虚拟机分布式...
    99+
    2023-06-02
  • MariaDB Galera Cluster集群企业版编译安装与配置
    安装环境系统:CentOS 6.8 x86_64软件:MariaDB 10.1.16节点一:192.168.11.132 4C 8GB节点二:192.168.11.133 4C 8GB软件获取访问Maria...
    99+
    2022-10-18
  • Nginx+Tomcat负载均衡集群安装配置案例详解
    目录前言一、Nginx+Tomcat二、配置Nginx服务器三、部署Tomcat应用服务器总结前言 介绍Tomcat及Nginx+Tomcat负载均衡集群,Tomcat的应用场景,然...
    99+
    2022-11-12
  • 内置jetty server下solrcloud6.2.1和Zookeeper集群如何安装和配置过程
    小编给大家分享一下内置jetty server下solrcloud6.2.1和Zookeeper集群如何安装和配置过程,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧...
    99+
    2023-06-03
  • 在CentOS中安装Rancher2并配置kubernetes集群的图文教程
    准备 一台CentOS主机,安装dockerCE,用于安装Rancher2 一台CentOS主机,安装DockerCE,用于安装kubernetes集群管理主机 多台CentOS主机,安装DockerCE,用于运行kub...
    99+
    2022-06-04
    kubernetes集群 centos 安装rancher2
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作