Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure sharding fragmentation in MongoDB 3.4.How to configure sharding fragmentation

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces how to configure sharding slicing in MongoDB 3.4.The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

1. Create a copy set of configuration services

Create a data directory, config directory for configuration services, and shard directory for sharding data nodes

[root@MySQL01 ~] # su-mongo

[mongo@MySQL01 ~] $cd / data/db

[mongo@MySQL01 db] $mkdir config01

[mongo@MySQL01 db] $mkdir config02

[mongo@MySQL01 db] $mkdir config03

[mongo@MySQL01 db] $mkdir shard01

[mongo@MySQL01 db] $mkdir shard02

[mongo@MySQL01 db] $mkdir shard03

Start each node of the configuration service replica set

[mongo@MySQL01] $mongod-- configsvr-- replSet fire-- dbpath / data/db/config01-- port 10001

[mongo@MySQL01] $mongod-- configsvr-- replSet fire-- dbpath / data/db/config02-- port 10002

[mongo@MySQL01] $mongod-- configsvr-- replSet fire-- dbpath / data/db/config03-- port 10003

Connect to the configuration server and perform initialization

[mongo@MySQL01 ~] $mongo localhost:10001

MongoDB shell version v3.4.10

Connecting to: localhost:10001

MongoDB server version: 3.4.10

> rs.initiate (

... {

... _ id: "fire"

... Configsvr: true

... Members: [

... {_ id: 0, host: "MySQL01:10001"}

... {_ id: 1, host: "MySQL01:10002"}

... {_ id: 2, host: "MySQL01:10003"}

...]

...}

.)

{"ok": 1}

Fire:SECONDARY > exit

Bye

two。 Create a fragmented copy set

Start each node of the replica set

[mongo@MySQL01] $mongod-- shardsvr-- replSet flame-- port 20001-- dbpath / data/db/shard01

[mongo@MySQL01] $mongod-- shardsvr-- replSet flame-- port 20002-- dbpath / data/db/shard02

[mongo@MySQL01] $mongod-- shardsvr-- replSet flame-- port 20003-- dbpath / data/db/shard03

A node connected to a fragmented replica set to perform the replica set initialization operation

[mongo@MySQL01 ~] $mongo localhost:20001

MongoDB shell version v3.4.10

Connecting to: localhost:20001

MongoDB server version: 3.4.10

> rs.initiate (

... {

... _ id: "flame"

... Members: [

... {_ id: 0, host: "MySQL01:20001"}

... {_ id: 1, host: "MySQL01:20002"}

... {_ id: 2, host: "MySQL01:20003"}

...]

...}

.)

{"ok": 1}

Flame:OTHER > exit

Bye

3. Connect to the sharding cluster through mongos

[mongo@MySQL01] $mongos-- configdb fire/MySQL01:10001,MySQL01:10002,MySQL01:10003-- port 30000

4. Add shards to the cluster

Connect to mongos and add the replica set created above to the cluster

[mongo@MySQL01 ~] $mongo localhost:30000

MongoDB shell version v3.4.10

Connecting to: localhost:30000

MongoDB server version: 3.4.10

Mongos > sh.addShard ("flame/MySQL01:40001")

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

Open the sharding of the specified database

Enable sharding function for sale database

Mongos > sh.enableSharding ("sale")

{"ok": 1}

Create a collection in the sale database

Mongos > use sale

Switched to db sale

Mongos >

Mongos > show collections

Mongos > db.dept.insert ({"id": 10})

WriteResult ({"nInserted": 1})

Mongos > db.dept.insert ({"id": 20})

WriteResult ({"nInserted": 1})

Mongos > db.dept.insert ({"id": 30})

WriteResult ({"nInserted": 1})

Perform range fragmentation for the created collection

Mongos > sh.shardCollection ("sale.dept", {id: 1})

{

"proposedKey": {

"id": 1

}

"curIndexes": [

{

"v": 2

"key": {

"_ id": 1

}

"name": "_ id_"

"ns": "sale.dept"

}

]

"ok": 0

"errmsg": "please create an index that starts with the shard key before sharding."

}

An error occurred. You need to create an index on the fragment field of the collection.

Mongos > db.dept.ensureIndex ({"id": 1})

{

"raw": {

"flame/MySQL01:20001,MySQL01:20002,MySQL01:20003": {

"createdCollectionAutomatically": false

"numIndexesBefore": 1

"numIndexesAfter": 2

"ok": 1

"$gleStats": {

"lastOpTime": {

Ts: Timestamp (1512224813, 1)

"t": NumberLong (1)

}

"electionId": ObjectId ("7fffffff0000000000000001")

}

}

}

"ok": 1

}

Perform fragmentation on a collection

Mongos > sh.shardCollection ("sale.dept", {id: 1})

{"collectionsharded": "sale.dept", "ok": 1}

About how to configure sharding fragments in MongoDB 3.4.This is it. I hope the above content can be helpful to everyone and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report