MongoDB分片集群搭建

这篇具有很好参考价值的文章主要介绍了MongoDB分片集群搭建。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

MongoDB 有三种集群架构模式,分别为主从复制(Master-Slaver)、副本集(Replica Set)和分片(Sharding)模式。

Master-Slaver 是一种主从复制的模式,目前已经不推荐使用

ReplicaSet模式取代了Master-Slaver模式,是一种互为主从的关系。Replica Set 将数据复制多份保存,不同服务器保存同一份数据,在出现故障时自动切换,实现故障转移。

MongoDB复制集主要用于实现服务的高可用性,与Redis中的哨兵模式相似。它的核心作用是数据的备份和故障转移。

Sharding 模式适合处理大量数据,它将数据分开存储,不同服务器保存不同的数据,所有服务器数据的总和即为整个数据集。

本文档主要内容为分片集群的搭建和操作

一、分片集群组件介绍

构建一个MongoDB的分片集群,需要三个重要组件,分别是分片服务器(Shard Server)、配置服务器(Config Server)、路由服务器(Router Server)。

Shard Server

每个分片服务器都是一个mongod数据库实例,用于存储实际的数据块,整个数据库集合分成多个存储在不同的分片服务器中。在实际生产中,一个Shard Server可以由多台机器组成一个副本集来承担,防止因主节点单点故障导致整个系统崩溃。

Config Server

这是独立的一个mongod进程,存储所有数据库元信息(路由、分片)的配置。mongos本身没有物理存储分片服务器和数据路由信息,只是缓存在内存里,配置服务器则实际存储这些数据。mongos第一次启动或者关掉重启就会从config server加载配置信息,以后如果配置服务器信息变化会通知到所有的mongos更新自己的状态,这样mongos就能继续准确路由。在生产环境通常设置多个config server,因为它存储了分片路由的元数据,防止单点数据丢失!

Router Server

这是独立的一个mongod进程,Router Server在集群中可作为路由使用,客户端由此

接入,让整个集群看起来像是一个单一的数据库,提供客户端应用程序和分片集群之间的接口。router Server本身不保存数据,启动时从Config Server加载集群信息到缓存中,并将客户端的请求路由给每个Shard Server,在各Shard Server返回结果后进行聚合并返回给客户端。

在实际生产环境中,副本集和分片是结合起来使用的,可满足实际应用场景中高可用性和高可扩展性的需求

副本集介绍

replica set

中文翻译副本集,其实就是shard的备份,防止shard挂掉之后数据丢失。复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。

仲裁者(Arbiter),是复制集中的一个MongoDB实例,它并不保存数据。仲裁节点使用最小的资源并且不要求硬件设备,不能将Arbiter部署在同一个数据集节点中,可以部署在其他应用服务器或者监视服务器中,也可部署在单独的虚拟机中。为了确保复制集中有奇数的投票成员(包括primary),需要添加仲裁节点做为投票,否则primary不能运行时不会自动切换primary。Secondary为primary的备用节点、在primary出现故障时通过arbiter使secondary成为primary,而当原来的primary节点恢复时自动成为secondary。

二、操作系统参数优化
2.1 关闭内存大页

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

增加下列内容:

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添加可执行权限。

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

最后重启。

2.2 禁用numa架构

使用普通用户启动命令之前添加 sudo -u mongo numactl --interleave=all

修改内核参数

echo 0 > /proc/sys/vm/zone_reclaim_mode

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

2.3 设置 vm.swappiness

临时修改

sysctl -w vm.swappiness=0

永久修改

vim /etc/sysctl.conf

vm.swappiness = 0

2.4 修改打开⽂件数限制

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

三、实施部署
3.1 服务规划

节点名称

IP

端口

角色

Mongos1

127.0.0.1

30001

Mongos(路由)

Mongos2

127.0.0.1

30002

Mongos(路由)

Config1

127.0.0.1

20001

Config(配置服务主)

Config2

127.0.0.1

20002

Config(配置服务从)

Config3

127.0.0.1

20003

Config(配置服务从)

Shard1_1

127.0.0.1

27017

分片集群1数据节点1主

Shard1_2

127.0.0.1

27018

分片集群1数据节点2从

Shard1_3

127.0.0.1

27019

分片集群1数据节点3从

Shard2_1

127.0.0.1

28017

分片集群2数据节点1主

Shard2_2

127.0.0.1

28018

分片集群2数据节点2从

Shard2_3

127.0.0.1

28019

分片集群2数据节点3从

3.2下载并安装

cd /u01

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

移动目录

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

创建目录

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

创建启动账号

groupadd mongo

useradd -M -g mongo mongo

生成keyfile,如果部署到不同主机,需要拷贝到其他主机上

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

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

修改目录权限

chown -R mongo:mongo /data/mongodb

3.3 配置config集群
3.3.1 config节点的配置文件

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 #存储引擎

 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 # 集群角色,这里配置的角色是配置节点#

其他2节点根据上面配置文件进行相应的修改

3.3.2 启动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);

执行结果

> 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 查看集群状态

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 创建管理用户

use admin

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

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

3.4 配置shard集群
3.4.1 shard节点的配置文件

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 #存储引擎

 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节点#

3.4.2 启动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);

执行结果

> 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 查看集群状态

> 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 创建管理用户

use admin

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

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

3.4.6 Shard2集群配置

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

启动并配置

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);

查看集群状态

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

}

创建管理用户

use admin

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

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

3.5 配置mongos

mongos负责查询与数据写入的路由,是实例访问的统一入口,是一个无状态的节点,每一个节点都可以从config-server节点获取到配置信息

注意点:

Mongos不需要存储数据,所有不需要配置storage相关属性值

Mongos不是副本集的概念,所有不需要配置replication相关属性值

Mongos需要配置configDB信息

3.5.1 mongos节点的配置文件

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 启动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

查看路由状态

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 配置分片信息

因为mongos是无中心的配置,所有需要每一台mongos都需要进行分片配置

添加shard1和shard2分片节点,

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 查看路由状态

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 开启数据库和集合分片(指定片键)
3.6.1 开启数据和集合分片

默认添加数据是没有使用分片存储的,操作都是在路由服务中

为数据库开启分片功能:sh.enableSharding("test_employ")

为指定集合开启分片功能:sh.shardCollection("test_employ.employ_datas",{"name":"hashed"})

##根据test_employ数据库中employ_datas表的name字段开启hash分片存储

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")

                }

        }

}

查看分片集群状态

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数据库及对于的集合分片存储已经开启

3.6.2 测试数据分片存储

查看分片是否同步集合

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>

测试数据是否被插入到不同的分片副本集中

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>

查看分片集群的数据库存储情况

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>

备注:配置服务或者路由服务如果挂掉一台,对数据也不会有影响。如果切换了一台新的路由服务器,则需要配置表的分片存储,否则插入数据不会被均分到数据库集群中,只会固定插入到某一台数据库实例中文章来源地址https://www.toymoban.com/news/detail-803220.html

到了这里,关于MongoDB分片集群搭建的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • mongodb 分片集群部署

    config 初始化,任意一台执行 shard1 安装 3台服务器分别添加配置文件 shard1 初始化 连接任意一台非“arbiterOnly: true”节点shard1,192.168.221.173、192.168.221.174、192.168.221.175分别替换为3台机器的IP地址(建议节点1执行) shard2 安装 3台服务器分别添加配置文件 shard2初始化, 连接任意一

    2024年02月10日
    浏览(24)
  • MongoDB分片集群架构详解

    分片(shard)是指在将数据进行水平切分之后,将其存储到多个不同的服务器节点上的一种扩展方式。分片在概念上非常类似于应用开发中的“水平分表”。不同的点在于,MongoDB 本身就自带了分片管理的能力,对于开发者来说可以做到开箱即用。 为什么要使用分片? MongoD

    2024年02月01日
    浏览(36)
  • 53.MongoDB分片集群&高级集群架构详解

    为什么要使用分片 分片(shard)是指在将数据进行水平切分之后,将其存储到多个不同的服务器节点上的一种扩展方式。 一个复制集能承载的容量和负载是有限的,遇到以下场景就需要考虑使用分片 存储容量需求超出单机的磁盘容量。 活跃的数据集超出单机内存容量,导致

    2024年02月08日
    浏览(26)
  • 使用 Docker 部署高可用 MongoDB 分片集群

    mongodb 集群搭建的方式有三种: 主从备份(Master - Slave)模式,或者叫主从复制模式。 副本集(Replica Set)模式。 分片(Sharding)模式。 其中,第一种方式基本没什么意义,官方也不推荐这种方式搭建。另外两种分别就是副本集和分片的方式。今天介绍副本集的方式搭建 mo

    2024年02月06日
    浏览(39)
  • CentOS 8自动化安装MongoDB并安装和实验master-slave集群、副本集群(Replica Set)、分片集群(Sharding)

    注意实验使用的是ARM架构的CentOS 8 虚拟机 首先,更新系统并安装必要的依赖项: 添加 MongoDB 官方仓库: 创建一个新的仓库文件 /etc/yum.repos.d/mongodb-org.repo : 将以下内容添加到文件中,保存并退出: 使用nano编辑器打开/etc/yum.repos.d/mongodb-org.repo文件后,按下 Ctrl + X 组合键退出

    2023年04月22日
    浏览(29)
  • mongodb集群搭建

    下载mongodb-linux-x86_64-rhel70-5.0.18 vi mongodb.conf 导入环境变量中 以上操作在每个机器都做一遍 之后选择一个作为主节点,启动mongodb 启动: mongod -f ../conf/mongodb.conf --replSet \\\"grap\\\" 每个机器都做一边,最后连接一台机器 Mongo进去数据库中,(注意一定不要先执行rs.initiate(),否则后

    2024年02月16日
    浏览(31)
  • 基于Docker的MongoDB集群搭建

    原文:https://www.yuque.com/wfzx/ninzck/dagu5akm0ztfuobw?singleDoc# 《基于Docker的MongoDB集群搭建》 在Window 10上使用 VMware 开启四个 Ubuntu 18 的虚拟主机。 MongoDB A:192.168.204.156(主节点) MongoDB B:192.168.204.157 MongoDB C:192.168.204.158 MongoDB D:192.168.204.159 host文件位于 C:WindowsSystem32driversetch

    2024年02月08日
    浏览(29)
  • MongoDB 7.0 搭建 Sharding 副本集群

    本文是在ubuntu 22.03 系统版本上部署的,最低支持mongodb-6.0.4以上,所以这里安装mongodb7.0 安装方式有多种,本人是使用的第一种方式,时间也就20分钟吧,能接受。 S1.导入 MongoDB GPG 公钥,用于验证下载的软件包的完整性,使用以下命令导入公钥 具体需要导入的版本号,可以去

    2024年02月19日
    浏览(26)
  • Mongodb Replica Sets 副本集群搭建

    Replica Sets 复制集搭建 MongoDB 有三种集群架构模式,分别为主从复制(Master-Slaver)、副本集(Replica Set)和分片(Sharding)模式。 Master-Slaver 是一种主从复制的模式,目前已经不推荐使用 ReplicaSet模式取代了Master-Slaver模式,是一种互为主从的关系。Replica Set 将数据复制多份保存

    2024年01月22日
    浏览(39)
  • MongoDB搭建复制集集群(Docker版)

    复制集注意事项 关于复制集: 复制集为 MongoDB 提供了数据可靠性,当某个节点挂掉,可以重新选举出主节点; 复制集为 MongoDB 提供了数据安全性,当节点宕机后,备份数据保证数据不丢失; 复制集为 MOngoDB 提供了高性能,可通过配置主从读写分离提高服务性能; 关于硬件

    2024年01月22日
    浏览(34)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包