国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

MongoDB分片集群搭建

這篇具有很好參考價(jià)值的文章主要介紹了MongoDB分片集群搭建。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

MongoDB 有三種集群架構(gòu)模式,分別為主從復(fù)制(Master-Slaver)、副本集(Replica Set)和分片(Sharding)模式。

Master-Slaver 是一種主從復(fù)制的模式,目前已經(jīng)不推薦使用

ReplicaSet模式取代了Master-Slaver模式,是一種互為主從的關(guān)系。Replica Set 將數(shù)據(jù)復(fù)制多份保存,不同服務(wù)器保存同一份數(shù)據(jù),在出現(xiàn)故障時(shí)自動(dòng)切換,實(shí)現(xiàn)故障轉(zhuǎn)移。

MongoDB復(fù)制集主要用于實(shí)現(xiàn)服務(wù)的高可用性,與Redis中的哨兵模式相似。它的核心作用是數(shù)據(jù)的備份和故障轉(zhuǎn)移。

Sharding 模式適合處理大量數(shù)據(jù),它將數(shù)據(jù)分開存儲(chǔ),不同服務(wù)器保存不同的數(shù)據(jù),所有服務(wù)器數(shù)據(jù)的總和即為整個(gè)數(shù)據(jù)集。

本文檔主要內(nèi)容為分片集群的搭建和操作

一、分片集群組件介紹

構(gòu)建一個(gè)MongoDB的分片集群,需要三個(gè)重要組件,分別是分片服務(wù)器(Shard Server)、配置服務(wù)器(Config Server)、路由服務(wù)器(Router Server)。

Shard Server

每個(gè)分片服務(wù)器都是一個(gè)mongod數(shù)據(jù)庫實(shí)例,用于存儲(chǔ)實(shí)際的數(shù)據(jù)塊,整個(gè)數(shù)據(jù)庫集合分成多個(gè)存儲(chǔ)在不同的分片服務(wù)器中。在實(shí)際生產(chǎn)中,一個(gè)Shard Server可以由多臺(tái)機(jī)器組成一個(gè)副本集來承擔(dān),防止因主節(jié)點(diǎn)單點(diǎn)故障導(dǎo)致整個(gè)系統(tǒng)崩潰。

Config Server

這是獨(dú)立的一個(gè)mongod進(jìn)程,存儲(chǔ)所有數(shù)據(jù)庫元信息(路由、分片)的配置。mongos本身沒有物理存儲(chǔ)分片服務(wù)器和數(shù)據(jù)路由信息,只是緩存在內(nèi)存里,配置服務(wù)器則實(shí)際存儲(chǔ)這些數(shù)據(jù)。mongos第一次啟動(dòng)或者關(guān)掉重啟就會(huì)從config server加載配置信息,以后如果配置服務(wù)器信息變化會(huì)通知到所有的mongos更新自己的狀態(tài),這樣mongos就能繼續(xù)準(zhǔn)確路由。在生產(chǎn)環(huán)境通常設(shè)置多個(gè)config server,因?yàn)樗鎯?chǔ)了分片路由的元數(shù)據(jù),防止單點(diǎn)數(shù)據(jù)丟失!

Router Server

這是獨(dú)立的一個(gè)mongod進(jìn)程,Router Server在集群中可作為路由使用,客戶端由此

接入,讓整個(gè)集群看起來像是一個(gè)單一的數(shù)據(jù)庫,提供客戶端應(yīng)用程序和分片集群之間的接口。router Server本身不保存數(shù)據(jù),啟動(dòng)時(shí)從Config Server加載集群信息到緩存中,并將客戶端的請(qǐng)求路由給每個(gè)Shard Server,在各Shard Server返回結(jié)果后進(jìn)行聚合并返回給客戶端。

在實(shí)際生產(chǎn)環(huán)境中,副本集和分片是結(jié)合起來使用的,可滿足實(shí)際應(yīng)用場景中高可用性和高可擴(kuò)展性的需求

副本集介紹

replica set

中文翻譯副本集,其實(shí)就是shard的備份,防止shard掛掉之后數(shù)據(jù)丟失。復(fù)制提供了數(shù)據(jù)的冗余備份,并在多個(gè)服務(wù)器上存儲(chǔ)數(shù)據(jù)副本,提高了數(shù)據(jù)的可用性, 并可以保證數(shù)據(jù)的安全性。

仲裁者(Arbiter),是復(fù)制集中的一個(gè)MongoDB實(shí)例,它并不保存數(shù)據(jù)。仲裁節(jié)點(diǎn)使用最小的資源并且不要求硬件設(shè)備,不能將Arbiter部署在同一個(gè)數(shù)據(jù)集節(jié)點(diǎn)中,可以部署在其他應(yīng)用服務(wù)器或者監(jiān)視服務(wù)器中,也可部署在單獨(dú)的虛擬機(jī)中。為了確保復(fù)制集中有奇數(shù)的投票成員(包括primary),需要添加仲裁節(jié)點(diǎn)做為投票,否則primary不能運(yùn)行時(shí)不會(huì)自動(dòng)切換primary。Secondary為primary的備用節(jié)點(diǎn)、在primary出現(xiàn)故障時(shí)通過arbiter使secondary成為primary,而當(dāng)原來的primary節(jié)點(diǎn)恢復(fù)時(shí)自動(dòng)成為secondary。

二、操作系統(tǒng)參數(shù)優(yōu)化
2.1 關(guān)閉內(nèi)存大頁

echo never > /sys/kernel/mm/transparent_hugepage/enabled

echo never > /sys/kernel/mm/transparent_hugepage/defrag

并追加??件中

/etc/rc.local

[root@localhost ~]# vim /etc/rc.d/rc.local

增加下列內(nèi)容:

if test -f /sys/kernel/mm/transparent_hugepage/enabled; then

echo never > /sys/kernel/mm/transparent_hugepage/enabled

fi

if test -f /sys/kernel/mm/transparent_hugepage/defrag; then

echo never > /sys/kernel/mm/transparent_hugepage/defrag

fi

保存并退出,然后給rc.local添加可執(zhí)行權(quán)限。

[root@localhost ~]# chmod +x /etc/rc.d/rc.local

最后重啟。

2.2 禁用numa架構(gòu)

使用普通用戶啟動(dòng)命令之前添加 sudo -u mongo numactl --interleave=all

修改內(nèi)核參數(shù)

echo 0 > /proc/sys/vm/zone_reclaim_mode

echo vm.zone_reclaim_mode = 0 >> /etc/sysctl.conf

2.3 設(shè)置 vm.swappiness

臨時(shí)修改

sysctl -w vm.swappiness=0

永久修改

vim /etc/sysctl.conf

vm.swappiness = 0

2.4 修改打開?件數(shù)限制

vim /etc/security/limits.conf

mongo soft nofile 65535

mongo hard nofile 65535

mongo soft nproc 65535

mongo hard nproc 65535

mongo soft stack 10240

mongo hards stack 10240

三、實(shí)施部署
3.1 服務(wù)規(guī)劃

節(jié)點(diǎn)名稱

IP

端口

角色

Mongos1

127.0.0.1

30001

Mongos(路由)

Mongos2

127.0.0.1

30002

Mongos(路由)

Config1

127.0.0.1

20001

Config(配置服務(wù)主)

Config2

127.0.0.1

20002

Config(配置服務(wù)從)

Config3

127.0.0.1

20003

Config(配置服務(wù)從)

Shard1_1

127.0.0.1

27017

分片集群1數(shù)據(jù)節(jié)點(diǎn)1主

Shard1_2

127.0.0.1

27018

分片集群1數(shù)據(jù)節(jié)點(diǎn)2從

Shard1_3

127.0.0.1

27019

分片集群1數(shù)據(jù)節(jié)點(diǎn)3從

Shard2_1

127.0.0.1

28017

分片集群2數(shù)據(jù)節(jié)點(diǎn)1主

Shard2_2

127.0.0.1

28018

分片集群2數(shù)據(jù)節(jié)點(diǎn)2從

Shard2_3

127.0.0.1

28019

分片集群2數(shù)據(jù)節(jié)點(diǎn)3從

3.2下載并安裝

cd /u01

tar -zxvf? mongodb-linux-x86_64-rhel70-4.4.26.tgz

移動(dòng)目錄

mv mongodb-linux-x86_64-rhel70-4.4.26.tgz mongodb

創(chuàng)建目錄

mkdir? key? config1

cd /data/mongodb/config1

mkdir data log conf

cd /u01/mongodb

cp -r config1 config2

cp -r config1 config3

cp –r config1 shard1_1

cp –r config1 shard1_2

cp –r config1 shard1_3

cp –r config1 shard2_1

cp –r config1 shard2_2

cp –r config1 shard2_3

cp -r config1 mongos1

cp -r config1 mongos2

創(chuàng)建啟動(dòng)賬號(hào)

groupadd mongo

useradd -M -g mongo mongo

生成keyfile,如果部署到不同主機(jī),需要拷貝到其他主機(jī)上

openssl rand -base64 259 >? /data/mongodb/key/mongo_cluster.key

chmod 600 /data/mongodb/key/mongo_cluster.key

修改目錄權(quán)限

chown -R mongo:mongo /data/mongodb

3.3 配置config集群
3.3.1 config節(jié)點(diǎn)的配置文件

systemLog:

?destination: file

?path: /data/mongodb/config1/log/mongod.log # log path

?logAppend: true

?logRotate: reopen

?destination: file

?timeStampFormat: "iso8601-local"

storage:

?dbPath: /data/mongodb/config1/data # data directory

?journal: #是否啟用journal日志

? enabled: true

?directoryPerDB: true

?syncPeriodSecs: 60

?engine: wiredTiger #存儲(chǔ)引擎

?wiredTiger:

? engineConfig:

? cacheSizeGB: 10

? journalCompressor: "snappy"

? directoryForIndexes: false

?collectionConfig:

? blockCompressor: "snappy"

?indexConfig:

? prefixCompression: true

net:

?port: 20001 # port

? bindIpAll: true

? maxIncomingConnections: 50000

? wireObjectCheck: true

? ipv6: false

? unixDomainSocket:

?? enabled: true

?? pathPrefix: "/data/mongodb/config1/tmp"

?? filePermissions: 0700

processManagement:

?fork: true

?pidFilePath: /data/mongodb/config1/mongodb.pid

security:

?keyFile: "/u01/mongodb/key/mongo_cluster.key"

?clusterAuthMode: "keyFile"

authorization: "enabled"

?javascriptEnabled: true

operationProfiling:

?slowOpThresholdMs: 100

?mode: slowOp

replication:

?oplogSizeMB: 20480

?replSetName: "configset" #副本集名稱#

sharding:

? clusterRole: configsvr # 集群角色,這里配置的角色是配置節(jié)點(diǎn)#

其他2節(jié)點(diǎn)根據(jù)上面配置文件進(jìn)行相應(yīng)的修改

3.3.2 啟動(dòng)config

sudo -u mongo numactl --interleave=all mongod -f? /data/mongodb/config1/conf/mongodb.conf

sudo -u mongo numactl --interleave=all mongod -f? /data/mongodb/config2/conf/mongodb.conf

sudo -u mongo numactl --interleave=all mongod -f? /data/mongodb/config2/conf/mongodb.conf

3.3.3 初始化配置

use admin

cfg={_id:"configset",

members:[

{_id:0,host:"localhost:20001"},

{_id:1,host:"localhost:20002"},

{_id:2,host:"localhost:20003"}]

}

rs.initiate(cfg);

執(zhí)行結(jié)果

> cfg={_id:"configset",

... members:[

... {_id:0,host:"localhost:20001"},

... {_id:1,host:"localhost:20002"},

... {_id:2,host:"localhost:20003"}]

... }

{

? ? ? ? "_id" : "configset",

? ? ? ? "members" : [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 0,

? ? ? ? ? ? ? ? ? ? ? ? "host" : "localhost:20001"

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "host" : "localhost:20002"

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "host" : "localhost:20003"

? ? ? ? ? ? ? ? }

? ? ? ? ]

}

> rs.initiate(cfg);

{

? ? ? ? "ok" : 1,

? ? ? ? "$gleStats" : {

? ? ? ? ? ? ? ? "lastOpTime" : Timestamp(1704942959, 1),

? ? ? ? ? ? ? ? "electionId" : ObjectId("000000000000000000000000")

? ? ? ? },

? ? ? ? "lastCommittedOpTime" : Timestamp(0, 0)

}

3.3.4 查看集群狀態(tài)

rs.status()

{

? ? ? ? "set" : "configset",

? ? ? ? "date" : ISODate("2024-01-11T03:17:44.330Z"),

? ? ? ? "myState" : 1,

? ? ? ? "term" : NumberLong(1),

? ? ? ? "syncSourceHost" : "",

? ? ? ? "syncSourceId" : -1,

? ? ? ? "configsvr" : true,

? ? ? ? "heartbeatIntervalMillis" : NumberLong(2000),

? ? ? ? "majorityVoteCount" : 2,

? ? ? ? "writeMajorityCount" : 2,

? ? ? ? "votingMembersCount" : 3,

? ? ? ? "writableVotingMembersCount" : 3,

? ? ? ? "members" : [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 0,

? ? ? ? ? ? ? ? ? ? ? ? "name" : "localhost:20001",

? ? ? ? ? ? ? ? ? ? ? ? "health" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "state" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "stateStr" : "PRIMARY",

? ? ? ? ? ? ? ? ? ? ? ? "uptime" : 1395,

? ? ? ? ? ? ? ? ? ? ? ? "optime" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704943063, 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDate" : ISODate("2024-01-11T03:17:43Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastAppliedWallTime" : ISODate("2024-01-11T03:17:43.985Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastDurableWallTime" : ISODate("2024-01-11T03:17:43.985Z"),

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceHost" : "",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceId" : -1,

? ? ? ? ? ? ? ? ? ? ? ? "infoMessage" : "Could not find member to sync from",

? ? ? ? ? ? ? ? ? ? ? ? "electionTime" : Timestamp(1704942969, 1),

? ? ? ? ? ? ? ? ? ? ? ? "electionDate" : ISODate("2024-01-11T03:16:09Z"),

? ? ? ? ? ? ? ? ? ? ? ? "configVersion" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "configTerm" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "self" : true,

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatMessage" : ""

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "name" : "localhost:20002",

? ? ? ? ? ? ? ? ? ? ? ? "health" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "state" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "stateStr" : "SECONDARY",

? ? ? ? ? ? ? ? ? ? ? ? "uptime" : 104,

? ? ? ? ? ? ? ? ? ? ? ? "optime" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704943062, 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurable" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704943062, 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDate" : ISODate("2024-01-11T03:17:42Z"),

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurableDate" : ISODate("2024-01-11T03:17:42Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastAppliedWallTime" : ISODate("2024-01-11T03:17:43.985Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastDurableWallTime" : ISODate("2024-01-11T03:17:43.985Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeat" : ISODate("2024-01-11T03:17:43.872Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatRecv" : ISODate("2024-01-11T03:17:42.973Z"),

? ? ? ? ? ? ? ? ? ? ? ? "pingMs" : NumberLong(0),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceHost" : "localhost:20001",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceId" : 0,

? ? ? ? ? ? ? ? ? ? ? ? "infoMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "configVersion" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "configTerm" : 1

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "name" : "localhost:20003",

? ? ? ? ? ? ? ? ? ? ? ? "health" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "state" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "stateStr" : "SECONDARY",

? ? ? ? ? ? ? ? ? ? ? ? "uptime" : 104,

? ? ? ? ? ? ? ? ? ? ? ? "optime" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704943062, 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurable" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704943062, 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDate" : ISODate("2024-01-11T03:17:42Z"),

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurableDate" : ISODate("2024-01-11T03:17:42Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastAppliedWallTime" : ISODate("2024-01-11T03:17:43.985Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastDurableWallTime" : ISODate("2024-01-11T03:17:43.985Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeat" : ISODate("2024-01-11T03:17:43.872Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatRecv" : ISODate("2024-01-11T03:17:42.973Z"),

? ? ? ? ? ? ? ? ? ? ? ? "pingMs" : NumberLong(0),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceHost" : "localhost:20001",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceId" : 0,

? ? ? ? ? ? ? ? ? ? ? ? "infoMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "configVersion" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "configTerm" : 1

? ? ? ? ? ? ? ? }

? ? ? ? ],

? ? ? ? "ok" : 1,

? ? ? ? "$gleStats" : {

? ? ? ? ? ? ? ? "lastOpTime" : Timestamp(1704942959, 1),

? ? ? ? ? ? ? ? "electionId" : ObjectId("7fffffff0000000000000001")

? ? ? ? },

? ? ? ? "lastCommittedOpTime" : Timestamp(1704943063, 1),

? ? ? ? "$clusterTime" : {

? ? ? ? ? ? ? ? "clusterTime" : Timestamp(1704943063, 1),

? ? ? ? ? ? ? ? "signature" : {

? ? ? ? ? ? ? ? ? ? ? ? "hash" : BinData(0,"oTJEVY6xZIgckyqV7XQ8rNE6e4Y="),

? ? ? ? ? ? ? ? ? ? ? ? "keyId" : NumberLong("7322674293400141847")

? ? ? ? ? ? ? ? }

? ? ? ? },

? ? ? ? "operationTime" : Timestamp(1704943063, 1)

}

3.3.5 創(chuàng)建管理用戶

use admin

db.createUser({user:"admin", pwd: "123456",roles:[{role:"root",db:"admin"}]})

db.auth("admin", "123456");

3.4 配置shard集群
3.4.1 shard節(jié)點(diǎn)的配置文件

systemLog:

? destination: file

? path: /data/mongodb/shard1_1/log/mongod.log # log path

? logAppend: true

? logRotate: reopen

? timeStampFormat: "iso8601-local"

storage:

?dbPath: /data/mongodb/shard1_1/data # data directory

?journal: #是否啟用journal日志

? ? enabled: true

?directoryPerDB: true

?syncPeriodSecs: 60

?engine: wiredTiger #存儲(chǔ)引擎

?wiredTiger:

?? engineConfig:

? ? cacheSizeGB: 10

? ? journalCompressor: "snappy"

? ? directoryForIndexes: false

?? collectionConfig:

? ? blockCompressor: "snappy"

?? indexConfig:

? ? prefixCompression: true

net:

?port: 27017 # port

?bindIpAll: true

processManagement:

?fork: true

?pidFilePath: /data/mongodb/shard1_1/mongodb.pid

security:

?keyFile: "/data/mongodb/key/mongo_cluster.key"

?clusterAuthMode: "keyFile"

?authorization: "enabled"

?javascriptEnabled: true

operationProfiling:

?slowOpThresholdMs: 100

?mode: slowOp

replication:

?oplogSizeMB: 20480

?replSetName: "shard1"

sharding:

?clusterRole: shardsvr # 集群角色,這里配置的角色是shard節(jié)點(diǎn)#

3.4.2 啟動(dòng)config

sudo -u mongo numactl --interleave=all mongod -f? /data/mongodb/shard1_1/conf/mongodb.conf

sudo -u mongo numactl --interleave=all mongod -f? /data/mongodb/shard1_2/conf/mongodb.conf

sudo -u mongo numactl --interleave=all mongod -f? /data/mongodb/shard1_3/conf/mongodb.conf

3.4.3 初始化配置

use admin

cfg={_id:"shard1",

members:[

{_id:0,host:"localhost:27017"},

{_id:1,host:"localhost:27018"},

{_id:2,host:"localhost:27019"}]

}

rs.initiate(cfg);

執(zhí)行結(jié)果

> use admin;

switched to db admin

> cfg={_id:"shard1",

... members:[

... {_id:0,host:"localhost:27017"},

... {_id:1,host:"localhost:27018"},

... {_id:2,host:"localhost:27019"}]

... }

{

? ? ? ? "_id" : "shard1",

? ? ? ? "members" : [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 0,

? ? ? ? ? ? ? ? ? ? ? ? "host" : "localhost:27017"

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "host" : "localhost:27018"

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "host" : "localhost:27019"

? ? ? ? ? ? ? ? }

? ? ? ? ]

}

>?

> rs.initiate(cfg);

{ "ok" : 1 }

3.4.4 查看集群狀態(tài)

> rs.status()

{

? ? ? ? "set" : "shard1",

? ? ? ? "date" : ISODate("2024-01-11T06:23:06.964Z"),

? ? ? ? "myState" : 1,

? ? ? ? "term" : NumberLong(1),

? ? ? ? "syncSourceHost" : "",

? ? ? ? "syncSourceId" : -1,

? ? ? ? "heartbeatIntervalMillis" : NumberLong(2000),

? ? ? ? "majorityVoteCount" : 2,

? ? ? ? "writeMajorityCount" : 2,

? ? ? ? "votingMembersCount" : 3,

? ? ? ? "writableVotingMembersCount" : 3,

? ? ? ? "members" : [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 0,

? ? ? ? ? ? ? ? ? ? ? ? "name" : "localhost:27017",

? ? ? ? ? ? ? ? ? ? ? ? "health" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "state" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "stateStr" : "PRIMARY",

? ? ? ? ? ? ? ? ? ? ? ? "uptime" : 8442,

? ? ? ? ? ? ? ? ? ? ? ? "optime" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704954182, 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDate" : ISODate("2024-01-11T06:23:02Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastAppliedWallTime" : ISODate("2024-01-11T06:23:02.177Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastDurableWallTime" : ISODate("2024-01-11T06:23:02.177Z"),

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceHost" : "",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceId" : -1,

? ? ? ? ? ? ? ? ? ? ? ? "infoMessage" : "Could not find member to sync from",

? ? ? ? ? ? ? ? ? ? ? ? "electionTime" : Timestamp(1704954092, 1),

? ? ? ? ? ? ? ? ? ? ? ? "electionDate" : ISODate("2024-01-11T06:21:32Z"),

? ? ? ? ? ? ? ? ? ? ? ? "configVersion" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "configTerm" : -1,

? ? ? ? ? ? ? ? ? ? ? ? "self" : true,

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatMessage" : ""

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "name" : "localhost:27018",

? ? ? ? ? ? ? ? ? ? ? ? "health" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "state" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "stateStr" : "SECONDARY",

? ? ? ? ? ? ? ? ? ? ? ? "uptime" : 105,

? ? ? ? ? ? ? ? ? ? ? ? "optime" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704954182, 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurable" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704954182, 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDate" : ISODate("2024-01-11T06:23:02Z"),

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurableDate" : ISODate("2024-01-11T06:23:02Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastAppliedWallTime" : ISODate("2024-01-11T06:23:02.177Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastDurableWallTime" : ISODate("2024-01-11T06:23:02.177Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeat" : ISODate("2024-01-11T06:23:06.162Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatRecv" : ISODate("2024-01-11T06:23:05.584Z"),

? ? ? ? ? ? ? ? ? ? ? ? "pingMs" : NumberLong(0),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceHost" : "localhost:27017",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceId" : 0,

? ? ? ? ? ? ? ? ? ? ? ? "infoMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "configVersion" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "configTerm" : -1

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "name" : "localhost:27019",

? ? ? ? ? ? ? ? ? ? ? ? "health" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "state" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "stateStr" : "SECONDARY",

? ? ? ? ? ? ? ? ? ? ? ? "uptime" : 105,

? ? ? ? ? ? ? ? ? ? ? ? "optime" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704954182, 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurable" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704954182, 1),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDate" : ISODate("2024-01-11T06:23:02Z"),

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurableDate" : ISODate("2024-01-11T06:23:02Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastAppliedWallTime" : ISODate("2024-01-11T06:23:02.177Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastDurableWallTime" : ISODate("2024-01-11T06:23:02.177Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeat" : ISODate("2024-01-11T06:23:06.162Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatRecv" : ISODate("2024-01-11T06:23:05.580Z"),

? ? ? ? ? ? ? ? ? ? ? ? "pingMs" : NumberLong(0),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceHost" : "localhost:27017",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceId" : 0,

? ? ? ? ? ? ? ? ? ? ? ? "infoMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "configVersion" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "configTerm" : -1

? ? ? ? ? ? ? ? }

? ? ? ? ],

? ? ? ? "ok" : 1

}

3.4.5 創(chuàng)建管理用戶

use admin

db.createUser({user:"admin", pwd: "123456",roles:[{role:"root",db:"admin"}]})

db.auth("admin", "123456");

3.4.6 Shard2集群配置

Shard2按照上面的步驟配置一遍

啟動(dòng)并配置

sudo -u mongo numactl --interleave=all mongod -f? /data/mongodb/shard1_1/conf/mongodb.conf

sudo -u mongo numactl --interleave=all mongod -f? /data/mongodb/shard1_2/conf/mongodb.conf

sudo -u mongo numactl --interleave=all mongod -f? /data/mongodb/shard1_3/conf/mongodb.conf

use admin

cfg={_id:"shard2",

members:[

{_id:0,host:"localhost:28017"},

{_id:1,host:"localhost:28018"},

{_id:2,host:"localhost:28019"}]

}

rs.initiate(cfg);

查看集群狀態(tài)

shard2:PRIMARY> rs.status()

{

? ? ? ? "set" : "shard2",

? ? ? ? "date" : ISODate("2024-01-11T06:39:37.061Z"),

? ? ? ? "myState" : 1,

? ? ? ? "term" : NumberLong(1),

? ? ? ? "syncSourceHost" : "",

? ? ? ? "syncSourceId" : -1,

? ? ? ? "heartbeatIntervalMillis" : NumberLong(2000),

? ? ? ? "majorityVoteCount" : 2,

? ? ? ? "writeMajorityCount" : 2,

? ? ? ? "votingMembersCount" : 3,

? ? ? ? "writableVotingMembersCount" : 3,

? ? ? ? "members" : [

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 0,

? ? ? ? ? ? ? ? ? ? ? ? "name" : "localhost:28017",

? ? ? ? ? ? ? ? ? ? ? ? "health" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "state" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "stateStr" : "PRIMARY",

? ? ? ? ? ? ? ? ? ? ? ? "uptime" : 251,

? ? ? ? ? ? ? ? ? ? ? ? "optime" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704955172, 5),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDate" : ISODate("2024-01-11T06:39:32Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastAppliedWallTime" : ISODate("2024-01-11T06:39:32.976Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastDurableWallTime" : ISODate("2024-01-11T06:39:32.976Z"),

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceHost" : "",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceId" : -1,

? ? ? ? ? ? ? ? ? ? ? ? "infoMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "electionTime" : Timestamp(1704955172, 1),

? ? ? ? ? ? ? ? ? ? ? ? "electionDate" : ISODate("2024-01-11T06:39:32Z"),

? ? ? ? ? ? ? ? ? ? ? ? "configVersion" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "configTerm" : -1,

? ? ? ? ? ? ? ? ? ? ? ? "self" : true,

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatMessage" : ""

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "name" : "localhost:28018",

? ? ? ? ? ? ? ? ? ? ? ? "health" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "state" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "stateStr" : "SECONDARY",

? ? ? ? ? ? ? ? ? ? ? ? "uptime" : 14,

? ? ? ? ? ? ? ? ? ? ? ? "optime" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704955172, 5),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurable" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704955172, 5),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDate" : ISODate("2024-01-11T06:39:32Z"),

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurableDate" : ISODate("2024-01-11T06:39:32Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastAppliedWallTime" : ISODate("2024-01-11T06:39:32.976Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastDurableWallTime" : ISODate("2024-01-11T06:39:32.976Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeat" : ISODate("2024-01-11T06:39:36.962Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatRecv" : ISODate("2024-01-11T06:39:36.706Z"),

? ? ? ? ? ? ? ? ? ? ? ? "pingMs" : NumberLong(0),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceHost" : "localhost:28019",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceId" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "infoMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "configVersion" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "configTerm" : -1

? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? {

? ? ? ? ? ? ? ? ? ? ? ? "_id" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "name" : "localhost:28019",

? ? ? ? ? ? ? ? ? ? ? ? "health" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "state" : 2,

? ? ? ? ? ? ? ? ? ? ? ? "stateStr" : "SECONDARY",

? ? ? ? ? ? ? ? ? ? ? ? "uptime" : 14,

? ? ? ? ? ? ? ? ? ? ? ? "optime" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704955172, 5),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurable" : {

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "ts" : Timestamp(1704955172, 5),

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? "t" : NumberLong(1)

? ? ? ? ? ? ? ? ? ? ? ? },

? ? ? ? ? ? ? ? ? ? ? ? "optimeDate" : ISODate("2024-01-11T06:39:32Z"),

? ? ? ? ? ? ? ? ? ? ? ? "optimeDurableDate" : ISODate("2024-01-11T06:39:32Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastAppliedWallTime" : ISODate("2024-01-11T06:39:32.976Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastDurableWallTime" : ISODate("2024-01-11T06:39:32.976Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeat" : ISODate("2024-01-11T06:39:36.963Z"),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatRecv" : ISODate("2024-01-11T06:39:35.704Z"),

? ? ? ? ? ? ? ? ? ? ? ? "pingMs" : NumberLong(0),

? ? ? ? ? ? ? ? ? ? ? ? "lastHeartbeatMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceHost" : "localhost:28017",

? ? ? ? ? ? ? ? ? ? ? ? "syncSourceId" : 0,

? ? ? ? ? ? ? ? ? ? ? ? "infoMessage" : "",

? ? ? ? ? ? ? ? ? ? ? ? "configVersion" : 1,

? ? ? ? ? ? ? ? ? ? ? ? "configTerm" : -1

? ? ? ? ? ? ? ? }

? ? ? ? ],

? ? ? ? "ok" : 1

}

創(chuàng)建管理用戶

use admin

db.createUser({user:"admin", pwd: "123456",roles:[{role:"root",db:"admin"}]})

db.auth("admin", "123456");

3.5 配置mongos

mongos負(fù)責(zé)查詢與數(shù)據(jù)寫入的路由,是實(shí)例訪問的統(tǒng)一入口,是一個(gè)無狀態(tài)的節(jié)點(diǎn),每一個(gè)節(jié)點(diǎn)都可以從config-server節(jié)點(diǎn)獲取到配置信息

注意點(diǎn):

Mongos不需要存儲(chǔ)數(shù)據(jù),所有不需要配置storage相關(guān)屬性值

Mongos不是副本集的概念,所有不需要配置replication相關(guān)屬性值

Mongos需要配置configDB信息

3.5.1 mongos節(jié)點(diǎn)的配置文件

systemLog:

? destination: file

? path: /data/mongodb/mongos2/log/mongod.log # log path

? logAppend: true

? logRotate: reopen

? timeStampFormat: "iso8601-local"

#storage:

net:

?port: 30002 # port

?bindIpAll: true

processManagement:

?fork: true

?pidFilePath: /data/mongodb/mongos2/mongodb.pid

security:

?keyFile: "/data/mongodb/key/mongo_cluster.key"

?clusterAuthMode: "keyFile"

#replication:

#配置configsvr副本集和IP端口

sharding:

?configDB: configset/127.0.0.1:20001,127.0.0.1:20002,127.0.0.1:20003

3.5.2 啟動(dòng)mongos

sudo -u mongo numactl --interleave=all mongos? -f? /data/mongodb/mongos1/conf/mongodb.conf

sudo -u mongo numactl --interleave=all mongos? -f? /data/mongodb/mongos2/conf/mongodb.conf

查看路由狀態(tài)

mongos> sh.status()

--- Sharding Status ---?

? sharding version: {

? ? ? ? "_id" : 1,

? ? ? ? "minCompatibleVersion" : 5,

? ? ? ? "currentVersion" : 6,

? ? ? ? "clusterId" : ObjectId("659f5d79606c4a0b3f65bd66")

? }

? shards:

? active mongoses:

? 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:?

? ? ? ? ? ? ? ? No recent migrations

? databases:

? ? ? ? {? "_id" : "config",? "primary" : "config",? "partitioned" : true } ?

3.5.3?配置分片信息

因?yàn)閙ongos是無中心的配置,所有需要每一臺(tái)mongos都需要進(jìn)行分片配置

添加shard1和shard2分片節(jié)點(diǎn),

sh.addShard("shard1/127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019")

sh.addShard("shard2/127.0.0.1:28017,127.0.0.1:28018,127.0.0.1:28019")

mongos> ? sh.addShard("shard1/localhost:27017,localhost:27018,localhost:27019")

{

? ? ? ? "shardAdded" : "shard1",

? ? ? ? "ok" : 1,

? ? ? ? "operationTime" : Timestamp(1704958302, 6),

? ? ? ? "$clusterTime" : {

? ? ? ? ? ? ? ? "clusterTime" : Timestamp(1704958302, 6),

? ? ? ? ? ? ? ? "signature" : {

? ? ? ? ? ? ? ? ? ? ? ? "hash" : BinData(0,"EGVAx43UOwaSIDFvR/vqvbATiQs="),

? ? ? ? ? ? ? ? ? ? ? ? "keyId" : NumberLong("7322674293400141847")

? ? ? ? ? ? ? ? }

? ? ? ? }

}

mongos> ? sh.addShard("shard2/localhost:28017,localhost:28018,localhost:28019")

{

? ? ? ? "shardAdded" : "shard2",

? ? ? ? "ok" : 1,

? ? ? ? "operationTime" : Timestamp(1704958469, 4),

? ? ? ? "$clusterTime" : {

? ? ? ? ? ? ? ? "clusterTime" : Timestamp(1704958469, 4),

? ? ? ? ? ? ? ? "signature" : {

? ? ? ? ? ? ? ? ? ? ? ? "hash" : BinData(0,"PNzHtyzXRmDaVE0Y+rnDLKg/L8E="),

? ? ? ? ? ? ? ? ? ? ? ? "keyId" : NumberLong("7322674293400141847")

? ? ? ? ? ? ? ? }

? ? ? ? }

}

3.5.4?查看路由狀態(tài)

mongos> sh.status()

--- Sharding Status ---?

? sharding version: {

? ? ? ? "_id" : 1,

? ? ? ? "minCompatibleVersion" : 5,

? ? ? ? "currentVersion" : 6,

? ? ? ? "clusterId" : ObjectId("659f5d79606c4a0b3f65bd66")

? }

? shards:

? ? ? ? {? "_id" : "shard1",? "host" : "shard1/localhost:27017,localhost:27018,localhost:27019",? "state" : 1 }

? ? ? ? {? "_id" : "shard2",? "host" : "shard2/localhost:28017,localhost:28018,localhost:28019",? "state" : 1 }

? active mongoses:

? ? ? ? "4.4.26" : 2

? 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:?

? ? ? ? ? ? ? ? 405 : Success

? databases:

? ? ? ? {? "_id" : "config",? "primary" : "config",? "partitioned" : true }

? ? ? ? ? ? ? ? config.system.sessions

? ? ? ? ? ? ? ? ? ? ? ? shard key: { "_id" : 1 }

? ? ? ? ? ? ? ? ? ? ? ? unique: false

? ? ? ? ? ? ? ? ? ? ? ? balancing: true

? ? ? ? ? ? ? ? ? ? ? ? chunks:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shard1? 619

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shard2? 405

? ? ? ? ? ? ? ? ? ? ? ? too many chunks to print, use verbose if you want to force print

3.6 開啟數(shù)據(jù)庫和集合分片(指定片鍵)
3.6.1 開啟數(shù)據(jù)和集合分片

默認(rèn)添加數(shù)據(jù)是沒有使用分片存儲(chǔ)的,操作都是在路由服務(wù)中

為數(shù)據(jù)庫開啟分片功能:sh.enableSharding("test_employ")

為指定集合開啟分片功能:sh.shardCollection("test_employ.employ_datas",{"name":"hashed"})

##根據(jù)test_employ數(shù)據(jù)庫中employ_datas表的name字段開啟hash分片存儲(chǔ)

mongos> sh.enableSharding("test_employ")

{

? ? ? ? "ok" : 1,

? ? ? ? "operationTime" : Timestamp(1704960417, 9),

? ? ? ? "$clusterTime" : {

? ? ? ? ? ? ? ? "clusterTime" : Timestamp(1704960417, 9),

? ? ? ? ? ? ? ? "signature" : {

? ? ? ? ? ? ? ? ? ? ? ? "hash" : BinData(0,"79mTp8DzV5l7rNeGwerhUvXsID8="),

? ? ? ? ? ? ? ? ? ? ? ? "keyId" : NumberLong("7322674293400141847")

? ? ? ? ? ? ? ? }

? ? ? ? }

}

mongos>? sh.shardCollection("test_employ.employ_datas",{"name":"hashed"})

{

? ? ? ? "collectionsharded" : "test_employ.employ_datas",

? ? ? ? "collectionUUID" : UUID("4a5ccf70-259e-4910-b613-21fa29cc9f41"),

? ? ? ? "ok" : 1,

? ? ? ? "operationTime" : Timestamp(1704961475, 25),

? ? ? ? "$clusterTime" : {

? ? ? ? ? ? ? ? "clusterTime" : Timestamp(1704961475, 25),

? ? ? ? ? ? ? ? "signature" : {

? ? ? ? ? ? ? ? ? ? ? ? "hash" : BinData(0,"IcfmJfwNDiKEVIydLb4pnDIC7xU="),

? ? ? ? ? ? ? ? ? ? ? ? "keyId" : NumberLong("7322674293400141847")

? ? ? ? ? ? ? ? }

? ? ? ? }

}

查看分片集群狀態(tài)

mongos> ? sh.status()?

--- Sharding Status ---?

? sharding version: {

? ? ? ? "_id" : 1,

? ? ? ? "minCompatibleVersion" : 5,

? ? ? ? "currentVersion" : 6,

? ? ? ? "clusterId" : ObjectId("659f5d79606c4a0b3f65bd66")

? }

? shards:

? ? ? ? {? "_id" : "shard1",? "host" : "shard1/localhost:27017,localhost:27018,localhost:27019",? "state" : 1 }

? ? ? ? {? "_id" : "shard2",? "host" : "shard2/localhost:28017,localhost:28018,localhost:28019",? "state" : 1 }

? active mongoses:

? ? ? ? "4.4.26" : 2

? 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:?

? ? ? ? ? ? ? ? 512 : Success

? databases:

? ? ? ? {? "_id" : "config",? "primary" : "config",? "partitioned" : true }

? ? ? ? ? ? ? ? config.system.sessions

? ? ? ? ? ? ? ? ? ? ? ? shard key: { "_id" : 1 }

? ? ? ? ? ? ? ? ? ? ? ? unique: false

? ? ? ? ? ? ? ? ? ? ? ? balancing: true

? ? ? ? ? ? ? ? ? ? ? ? chunks:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shard1? 512

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shard2? 512

? ? ? ? ? ? ? ? ? ? ? ? too many chunks to print, use verbose if you want to force print

? ? ? ? {? "_id" : "test_employ",? "primary" : "shard2",? "partitioned" : true,? "version" : {? "uuid" : UUID("7fff4bee-c4e4-47c4-a23c-1b7daed90a1b"),? "lastMod" : 1 } }

? ? ? ? ? ? ? ? test_employ.employ_datas

? ? ? ? ? ? ? ? ? ? ? ? shard key: { "name" : "hashed" }

? ? ? ? ? ? ? ? ? ? ? ? unique: false

? ? ? ? ? ? ? ? ? ? ? ? balancing: true

? ? ? ? ? ? ? ? ? ? ? ? chunks:

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shard1? 2

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? shard2? 2

? ? ? ? ? ? ? ? ? ? ? ? { "name" : { "$minKey" : 1 } } -->> { "name" : NumberLong("-4611686018427387902") } on : shard1 Timestamp(1, 0)?

? ? ? ? ? ? ? ? ? ? ? ? { "name" : NumberLong("-4611686018427387902") } -->> { "name" : NumberLong(0) } on : shard1 Timestamp(1, 1)?

? ? ? ? ? ? ? ? ? ? ? ? { "name" : NumberLong(0) } -->> { "name" : NumberLong("4611686018427387902") } on : shard2 Timestamp(1, 2)?

? ? ? ? ? ? ? ? ? ? ? ? { "name" : NumberLong("4611686018427387902") } -->> { "name" : { "$maxKey" : 1 } } on : shard2 Timestamp(1, 3)

可以查看到test_employ數(shù)據(jù)庫及對(duì)于的集合分片存儲(chǔ)已經(jīng)開啟

3.6.2 測試數(shù)據(jù)分片存儲(chǔ)

查看分片是否同步集合

shard1:PRIMARY> show dbs;

admin? ? ? ? 0.000GB

config ? ? ? 0.001GB

local? ? ? ? 0.001GB

test_employ? 0.000GB

shard1:PRIMARY>

shard2:PRIMARY> show dbs;

admin? ? ? ? 0.000GB

config ? ? ? 0.001GB

local? ? ? ? 0.001GB

test_employ? 0.000GB

shard2:PRIMARY>

測試數(shù)據(jù)是否被插入到不同的分片副本集中

mongos> use test_employ

switched to db test_employ

mongos> for (i=1; i<=500; i++){db.employ_datas.insert({name:'test01' + i, age:i})}

WriteResult({ "nInserted" : 1 })

mongos> db.test_employ.count()

500

mongos>

查看分片集群的數(shù)據(jù)庫存儲(chǔ)情況

shard1:PRIMARY> show tables;

employ_datas

shard1:PRIMARY> db.employ_datas.count()

272

shard1:PRIMARY>

shard2:PRIMARY> show tables;

employ_datas

shard2:PRIMARY> db.employ_datas.count()

228

shard2:PRIMARY>

備注:配置服務(wù)或者路由服務(wù)如果掛掉一臺(tái),對(duì)數(shù)據(jù)也不會(huì)有影響。如果切換了一臺(tái)新的路由服務(wù)器,則需要配置表的分片存儲(chǔ),否則插入數(shù)據(jù)不會(huì)被均分到數(shù)據(jù)庫集群中,只會(huì)固定插入到某一臺(tái)數(shù)據(jù)庫實(shí)例中文章來源地址http://www.zghlxwxcb.cn/news/detail-803220.html

到了這里,關(guān)于MongoDB分片集群搭建的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • MongoDB 分片集群

    MongoDB 分片集群

    在了解分片集群之前,務(wù)必要先了解復(fù)制集技術(shù)! 一組Mongodb復(fù)制集,就是一組mongod進(jìn)程,這些進(jìn)程維護(hù)同一個(gè)數(shù)據(jù)集合。復(fù)制集提供了數(shù)據(jù)冗余和高等級(jí)的可靠性,這是生產(chǎn)部署的基礎(chǔ)。 1.1.1 復(fù)制集的目的 保證數(shù)據(jù)在生產(chǎn)部署時(shí)的冗余和可靠性,通過在不同的機(jī)器上保存副

    2024年02月13日
    瀏覽(21)
  • MongoDB分片集群架構(gòu)詳解

    MongoDB分片集群架構(gòu)詳解

    分片(shard)是指在將數(shù)據(jù)進(jìn)行水平切分之后,將其存儲(chǔ)到多個(gè)不同的服務(wù)器節(jié)點(diǎn)上的一種擴(kuò)展方式。分片在概念上非常類似于應(yīng)用開發(fā)中的“水平分表”。不同的點(diǎn)在于,MongoDB 本身就自帶了分片管理的能力,對(duì)于開發(fā)者來說可以做到開箱即用。 為什么要使用分片? MongoD

    2024年02月01日
    瀏覽(21)
  • 53.MongoDB分片集群&高級(jí)集群架構(gòu)詳解

    為什么要使用分片 分片(shard)是指在將數(shù)據(jù)進(jìn)行水平切分之后,將其存儲(chǔ)到多個(gè)不同的服務(wù)器節(jié)點(diǎn)上的一種擴(kuò)展方式。 一個(gè)復(fù)制集能承載的容量和負(fù)載是有限的,遇到以下場景就需要考慮使用分片 存儲(chǔ)容量需求超出單機(jī)的磁盤容量。 活躍的數(shù)據(jù)集超出單機(jī)內(nèi)存容量,導(dǎo)致

    2024年02月08日
    瀏覽(59)
  • 使用 Docker 部署高可用 MongoDB 分片集群

    使用 Docker 部署高可用 MongoDB 分片集群

    mongodb 集群搭建的方式有三種: 主從備份(Master - Slave)模式,或者叫主從復(fù)制模式。 副本集(Replica Set)模式。 分片(Sharding)模式。 其中,第一種方式基本沒什么意義,官方也不推薦這種方式搭建。另外兩種分別就是副本集和分片的方式。今天介紹副本集的方式搭建 mo

    2024年02月06日
    瀏覽(19)
  • CentOS 8自動(dòng)化安裝MongoDB并安裝和實(shí)驗(yàn)master-slave集群、副本集群(Replica Set)、分片集群(Sharding)

    CentOS 8自動(dòng)化安裝MongoDB并安裝和實(shí)驗(yàn)master-slave集群、副本集群(Replica Set)、分片集群(Sharding)

    注意實(shí)驗(yàn)使用的是ARM架構(gòu)的CentOS 8 虛擬機(jī) 首先,更新系統(tǒng)并安裝必要的依賴項(xiàng): 添加 MongoDB 官方倉庫: 創(chuàng)建一個(gè)新的倉庫文件 /etc/yum.repos.d/mongodb-org.repo : 將以下內(nèi)容添加到文件中,保存并退出: 使用nano編輯器打開/etc/yum.repos.d/mongodb-org.repo文件后,按下 Ctrl + X 組合鍵退出

    2023年04月22日
    瀏覽(20)
  • mongodb集群搭建

    mongodb集群搭建

    下載mongodb-linux-x86_64-rhel70-5.0.18 vi mongodb.conf 導(dǎo)入環(huán)境變量中 以上操作在每個(gè)機(jī)器都做一遍 之后選擇一個(gè)作為主節(jié)點(diǎn),啟動(dòng)mongodb 啟動(dòng): mongod -f ../conf/mongodb.conf --replSet \\\"grap\\\" 每個(gè)機(jī)器都做一邊,最后連接一臺(tái)機(jī)器 Mongo進(jìn)去數(shù)據(jù)庫中,(注意一定不要先執(zhí)行rs.initiate(),否則后

    2024年02月16日
    瀏覽(24)
  • 基于Docker的MongoDB集群搭建

    基于Docker的MongoDB集群搭建

    原文:https://www.yuque.com/wfzx/ninzck/dagu5akm0ztfuobw?singleDoc# 《基于Docker的MongoDB集群搭建》 在Window 10上使用 VMware 開啟四個(gè) Ubuntu 18 的虛擬主機(jī)。 MongoDB A:192.168.204.156(主節(jié)點(diǎn)) MongoDB B:192.168.204.157 MongoDB C:192.168.204.158 MongoDB D:192.168.204.159 host文件位于 C:WindowsSystem32driversetch

    2024年02月08日
    瀏覽(21)
  • MongoDB 7.0 搭建 Sharding 副本集群

    本文是在ubuntu 22.03 系統(tǒng)版本上部署的,最低支持mongodb-6.0.4以上,所以這里安裝mongodb7.0 安裝方式有多種,本人是使用的第一種方式,時(shí)間也就20分鐘吧,能接受。 S1.導(dǎo)入 MongoDB GPG 公鑰,用于驗(yàn)證下載的軟件包的完整性,使用以下命令導(dǎo)入公鑰 具體需要導(dǎo)入的版本號(hào),可以去

    2024年02月19日
    瀏覽(21)
  • MongoDB搭建復(fù)制集集群(Docker版)

    MongoDB搭建復(fù)制集集群(Docker版)

    復(fù)制集注意事項(xiàng) 關(guān)于復(fù)制集: 復(fù)制集為 MongoDB 提供了數(shù)據(jù)可靠性,當(dāng)某個(gè)節(jié)點(diǎn)掛掉,可以重新選舉出主節(jié)點(diǎn); 復(fù)制集為 MongoDB 提供了數(shù)據(jù)安全性,當(dāng)節(jié)點(diǎn)宕機(jī)后,備份數(shù)據(jù)保證數(shù)據(jù)不丟失; 復(fù)制集為 MOngoDB 提供了高性能,可通過配置主從讀寫分離提高服務(wù)性能; 關(guān)于硬件

    2024年01月22日
    瀏覽(29)
  • Mongodb Replica Sets 副本集群搭建

    Mongodb Replica Sets 副本集群搭建

    Replica Sets 復(fù)制集搭建 MongoDB 有三種集群架構(gòu)模式,分別為主從復(fù)制(Master-Slaver)、副本集(Replica Set)和分片(Sharding)模式。 Master-Slaver 是一種主從復(fù)制的模式,目前已經(jīng)不推薦使用 ReplicaSet模式取代了Master-Slaver模式,是一種互為主從的關(guān)系。Replica Set 將數(shù)據(jù)復(fù)制多份保存

    2024年01月22日
    瀏覽(21)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包