MongoDB数据库分片操作

集群概况

机器1 机器2 机器3
172.19.147.247 172.19.147.248 172.19.147.249
mongos1:27000 mongos2:27000 mongos3:27000
config1:27000 config1:27000 config1:27000
shard1 master:27001 shard1 secondary:27001 shard1 arbiter:27001
shard2 arbiter:27002 shard2 master:27002 shard2 secondary:27002
shard3 secondary:27003 shard3 arbiter:27003 shard3 master:27003

一、进入MongoDB目录,连接

[root@cluster-1 /]# cd /data/mongodb/bin/
[root@cluster-1 bin]# ./mongo 172.19.147.247:27000
MongoDB shell version v4.2.7
connecting to: mongodb://172.19.147.247:27000/test?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("d41801c0-37b1-4802-b2e6-c4a53345cff4") }
MongoDB server version: 4.2.7
Server has startup warnings: 
2021-08-10T17:31:37.241+0800 I  CONTROL  [main] 
2021-08-10T17:31:37.241+0800 I  CONTROL  [main] ** WARNING: Access control is not enabled for the database.
2021-08-10T17:31:37.241+0800 I  CONTROL  [main] **          Read and write access to data and configuration is unrestricted.
2021-08-10T17:31:37.241+0800 I  CONTROL  [main] ** WARNING: You are running this process as the root user, which is not recommended.
2021-08-10T17:31:37.241+0800 I  CONTROL  [main] 
mongos> 

二、新建数据库,新建表

mongos> use test
switched to db test
mongos> db.createCollection("table")
{
	"ok" : 1,
	"operationTime" : Timestamp(1628818042, 8),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1628818042, 8),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

三、查看数据库当前情况

primary:数据库主分片在shard1上面
partitioned:false表示分片未开启

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("611239f7d5eb662c357d1f8a")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/172.19.147.247:27001,172.19.147.248:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/172.19.147.248:27002,172.19.147.249:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/172.19.147.247:27003,172.19.147.249:27003",  "state" : 1 }
  active mongoses:
        "4.2.7" : 3
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                6 : Success
  databases:
        {  "_id" : "test",  "primary" : "shard1",  "partitioned" : false,  "version" : {  "uuid" : UUID("4903b5ec-3f05-401b-8ee3-7449e14e8b67"),  "lastMod" : 1 } }
        
        

四、开启数据库的分片

mongos> sh.enableSharding("test")
{
	"ok" : 1,
	"operationTime" : Timestamp(1628818551, 3),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1628818551, 3),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

五、新建表的索引

命令db.table.ensureIndex中
{"_id":“hashed”}:哈希(hash)分片,对单列使用hash索引作为分片键
{"_id":1}:范围(range)分片,可以使用多个字段作为分片键,并将数据划分为由分片键确定的连续范围

mongos> db.table.ensureIndex({"_id":1})
{
	"raw" : {
		"shard1/172.19.147.247:27001,172.19.147.248:27001" : {
			"numIndexesBefore" : 1,
			"numIndexesAfter" : 1,
			"note" : "all indexes already exist",
			"ok" : 1
		}
	},
	"ok" : 1,
	"operationTime" : Timestamp(1628818639, 1),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1628818639, 1),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

六、开启表分片

mongos> sh.shardCollection( "test.table", { "_id" : 1 } )
{
	"collectionsharded" : "test.table",
	"collectionUUID" : UUID("dc4df1ae-045f-4c94-82b0-557474f1942d"),
	"ok" : 1,
	"operationTime" : Timestamp(1628819535, 14),
	"$clusterTime" : {
		"clusterTime" : Timestamp(1628819535, 14),
		"signature" : {
			"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
			"keyId" : NumberLong(0)
		}
	}
}

七、插入数据

mongos> for (var i = 0; i < 100000; i++) db.table.insert({_id: Math.random()});

八、再次查看状态

mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
  	"_id" : 1,
  	"minCompatibleVersion" : 5,
  	"currentVersion" : 6,
  	"clusterId" : ObjectId("611239f7d5eb662c357d1f8a")
  }
  shards:
        {  "_id" : "shard1",  "host" : "shard1/172.19.147.247:27001,172.19.147.248:27001",  "state" : 1 }
        {  "_id" : "shard2",  "host" : "shard2/172.19.147.248:27002,172.19.147.249:27002",  "state" : 1 }
        {  "_id" : "shard3",  "host" : "shard3/172.19.147.247:27003,172.19.147.249:27003",  "state" : 1 }
  active mongoses:
        "4.2.7" : 3
  autosplit:
        Currently enabled: yes
  balancer:
        Currently enabled:  yes
        Currently running:  no
        Failed balancer rounds in last 5 attempts:  0
        Migration Results for the last 24 hours: 
                8 : Success
  databases:
        {  "_id" : "test",  "primary" : "shard1",  "partitioned" : true,  "version" : {  "uuid" : UUID("4903b5ec-3f05-401b-8ee3-7449e14e8b67"),  "lastMod" : 1 } }
                test.table
                        shard key: { "_id" : 1 }
                        unique: false
                        balancing: true
                        chunks:
                                shard1	2
                                shard2	1
                                shard3	1
                        { "_id" : { "$minKey" : 1 } } -->> { "_id" : 0.29220988526755465 } on : shard3 Timestamp(2, 0) 
                        { "_id" : 0.29220988526755465 } -->> { "_id" : 0.5833127748590623 } on : shard2 Timestamp(3, 0) 
                        { "_id" : 0.5833127748590623 } -->> { "_id" : 0.8756424820024329 } on : shard1 Timestamp(3, 1) 
                        { "_id" : 0.8756424820024329 } -->> { "_id" : { "$maxKey" : 1 } } on : shard1 Timestamp(1, 3) 

九、其他命令

查看分片数据分布

mongos> db.table.getShardDistribution()

Shard shard3 at shard3/172.19.147.247:27003,172.19.147.249:27003
 data : 511KiB docs : 29127 chunks : 1
 estimated data per chunk : 511KiB
 estimated docs per chunk : 29127

Shard shard1 at shard1/172.19.147.247:27001,172.19.147.248:27001
 data : 1.71MiB docs : 100000 chunks : 2
 estimated data per chunk : 878KiB
 estimated docs per chunk : 50000

Shard shard2 at shard2/172.19.147.248:27002,172.19.147.249:27002
 data : 512KiB docs : 29128 chunks : 1
 estimated data per chunk : 512KiB
 estimated docs per chunk : 29128

Totals
 data : 2.71MiB docs : 158255 chunks : 4
 Shard shard3 contains 18.4% data, 18.4% docs in cluster, avg obj size on shard : 18B
 Shard shard1 contains 63.18% data, 63.18% docs in cluster, avg obj size on shard : 18B
 Shard shard2 contains 18.4% data, 18.4% docs in cluster, avg obj size on shard : 18B

查看表的状态

mongos> db.trade.stats()
内容较多不再展示
mongos> db.table.stats().sharded
true

查看MongoDB整体分片情况

mongos> use config
switched to db config
mongos> db.shards.find()
{ "_id" : "shard1", "host" : "shard1/172.19.147.247:27001,172.19.147.248:27001", "state" : 1 }
{ "_id" : "shard2", "host" : "shard2/172.19.147.248:27002,172.19.147.249:27002", "state" : 1 }
{ "_id" : "shard3", "host" : "shard3/172.19.147.247:27003,172.19.147.249:27003", "state" : 1 }
mongos> 

你可能感兴趣的:(MongoDB,数据库,分片)