Get the App
SLTechnology News&Howtos  ›  Database  › 

Mongodb shard fragmentation technology

Shulou Source: shulou.com Published: 2022-06-01 06:30:31 10月04日 Update

Start route process

Configure route process and start it as follows:

[root@localhost] # / usr/local/mongo/bin/mongos-port 40000-configdb localhost:30000-fork-logpath / data/shard/log/route.log-chunkSize 1

Forked process: 2911

[root@localhost ~] # all output going to: / data/shard/log/route.log

Configure sharding

[root@localhost] # / usr/local/mongo/bin/mongo-- port 40000

MongoDB shell version: 2.0.4

Connecting to: 127.0.0.1:40000/test

Mongos >

This place must be switched to the admin database

Mongos > use admin

Switched to db admin

Mongos > db

Admin

Mongos > db.runCommand ({addshard: "localhost:20000"})

{"shardAdded": "shard0000", "ok": 1}

Mongos > db.runCommand ({addshard: "localhost:20001"})

{"shardAdded": "shard0001", "ok": 1}

Mongos > db.runCommand ({enablesharding: "test"})

{"ok": 1}

Mongos > db.runCommand ({shardcollection: "test.users", key: {_ id:1}})

{"collectionsharded": "test.users", "ok": 1}

Momongos > for (var I = 1bot I db.users.find ()

{"_ id": ObjectId ("53d4eefd67d21d1d69f5bcd0"), "age": 1}

{"_ id": ObjectId ("53d4f024b1e3fc47712a0576"), "age": 1, "name": "fangwei", "addr": "hangzhou", "country": "China"}

{"_ id": ObjectId ("53d4f024b1e3fc47712a0577"), "age": 2, "name": "fangwei", "addr": "hangzhou", "country": "China"} ngos >

-verify our shards.

Mongos > db.users.stats ()

{

"sharded": true

"flags": 1

"ns": "test.users"

"count": 500001

"numExtents": 17

"size": 46000036

"storageSize": 81166336

"totalIndexSize": 18845680

"indexSizes": {

"_ id_": 18845680

}

"avgObjSize": 91.999888000224

"nindexes": 1

"nchunks": 37

"shards": {

"shard0000": {

"ns": "test.users"

"count": 197093

"size": 18132500

"avgObjSize": 91.99971587017296

"storageSize": 33333248

"numExtents": 8

"nindexes": 1

"lastExtentSize": 12083200

"paddingFactor": 1

"flags": 1

"totalIndexSize": 8969072

"indexSizes": {

"_ id_": 8969072

}

"ok": 1

}

"shard0001": {

"ns": "test.users"

"count": 302908

"size": 27867536

"avgObjSize": 92

"storageSize": 47833088

"numExtents": 9

"nindexes": 1

"lastExtentSize": 14499840

"paddingFactor": 1

"flags": 1

"totalIndexSize": 9876608

"indexSizes": {

"_ id_": 9876608

}

"ok": 1

}

}

"ok": 1

}

Mongos >

View the distribution of physical files on disk

Mongos > exit

Bye

[root@localhost ~] # ll / data/shard/s0/test

Total 213004

-rw-. 1 root root 67108864 Jul 27 20:28 test.0

-rw-. 1 root root 134217728 Jul 27 20:09 test.1

-rw-. 1 root root 16777216 Jul 27 20:28 test.ns

Drwxr-xr-x. 2 root root 4096 Jul 27 20:09 _ tmp

[root@localhost ~] #

[root@localhost ~] # ll / data/shard/s1/test

Total 475148

-rw-. 1 root root 67108864 Jul 27 20:38 test.0

-rw-. 1 root root 134217728 Jul 27 20:27 test.1

-rw-. 1 root root 268435456 Jul 27 20:27 test.2

-rw-. 1 root root 16777216 Jul 27 20:38 test.ns

Drwxr-xr-x. 2 root root 4096 Jul 27 20:27 _ tmp

[root@localhost ~] #

-list all shard server

Mongos > use admin

Switched to db admin

Mongos > db.runCommand ({listshards:1})

{

"shards": [

{

"_ id": "shard0000"

"host": "localhost:20000"

}

{

"_ id": "shard0001"

"host": "localhost:20001"

}

]

"ok": 1

}

-View sharding information

Mongos > printShardingSizes ()

-Sharding Status

Sharding version: {"_ id": 1, "version": 3}

Shards:

{"_ id": "shard0000", "host": "localhost:20000"}

{"_ id": "shard0001", "host": "localhost:20001"}

Databases:

{"_ id": "admin", "partitioned": false, "primary": "config"}

{"_ id": "test", "partitioned": true, "primary": "shard0000"}

Test.users chunks:

{"_ id": {$minKey: 1}}-> > {"_ id": ObjectId ("53d4eefd67d21d1d69f5bcd0")} on: shard0000 {"estimate": false, "size": 0, "numObjects": 0}

{"_ id": ObjectId ("53d4eefd67d21d1d69f5bcd0")}-- > > {"_ id": ObjectId ("53d4f024b1e3fc47712a1dd6")} on: shard0000 {"estimate": false, "size": 574116, "numObjects": 6241}

-judge to be Buddha sharding

> db.runCommand ({isdbgrid:1})

{"isdbgrid": 1, "hostname": "localhost.mongo102", "ok": 1}

>

-sharding existing tables

> db

Test

> db.users_2.stats ()

{

"sharded": false,-you can see that there is no sub-table

"primary": "shard0000"

"errmsg": "ns not found"

"ok": 0

}

It is divided into tables.

> db

Admin

> db.runCommand ({shardcollection: "test.users_2", key: {_ ud:1}})

{"collectionsharded": "test.users_2", "ok": 1}

> use test

Switched to db test

> db.users_2.stats ()

{

"sharded": true

"flags": 1

"ns": "test.users_2"

"count": 0

"numExtents": 1

"size": 0

"storageSize": 8192

"totalIndexSize": 16352

"indexSizes": {

"_ id_": 8176

"_ ud_1": 8176

}

"avgObjSize": 0

"nindexes": 2

"nchunks": 1

"shards": {

"shard0000": {

"ns": "test.users_2"

"count": 0

"size": 0

"storageSize": 8192

"numExtents": 1

"nindexes": 2

"lastExtentSize": 8192

"paddingFactor": 1

"flags": 1

"totalIndexSize": 16352

"indexSizes": {

"_ id_": 8176

"_ ud_1": 8176

}

"ok": 1

}

}

"ok": 1

}

>

-add shard server shards

[root@localhost ~] # mkdir / data/shard/s2

[root@localhost] # / usr/local/mongo/bin/mongod-shardsvr-- port 20002-- dbpath / data/shard/s2-- fork-- logpath / data/shard/log/s2.log-- directoryperdb

[root@localhost ~] # forked process: 3480

All output going to: / data/shard/log/s2.log

[root@localhost] # / usr/local/mongo/bin/mongo-- port 40000

MongoDB shell version: 2.0.4

Connecting to: 127.0.0.1:40000/test

Mongos > db

Test

Mongos > use admin

Switched to db admin

Mongos > db.runCommand ({addshard: "localhost:20002"})

{"shardAdded": "shard0002", "ok": 1}

Mongos >

Tags: Table configuration code information place situation data database file physics disk switch verification technology Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information Xiaomi Redmi OPPO Reno Shulou Tech Info