单机mongodb测试集群

在这里插入图片描述

三个角色

mongos 单节点或双节点
configserver 至少三节点副本集
shard 至少三节点副本集

分片集群生产搭建参考文档
https://www.mongodb.com/docs/v4.2/core/sharded-cluster-components/
https://www.mongodb.com/docs/v4.2/tutorial/deploy-shard-cluster/

注意一定要关闭SELINUX
查看sestatus

安装mongodb

vim /etc/yum.repos.d/mongodb-org-4.2.repo

[mongodb-org-4.2]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/4.2/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.2.asc

yum install -y mongodb-org mongodb-org-tools mongodb-org-shell

创建pid目录

mkdir /var/run/mongodb/

config搭建

config集群启动配置文件为3个组成replicaset

config1.conf
config2.conf
config3.conf

创建目录

mkdir -p /dbstorage/mongoconfig1
mkdir -p /dbstorage/mongoconfig2
mkdir -p /dbstorage/mongoconfig3

启动命令:

mongod --config config1.conf
mongod --config config2.conf
mongod --config config3.conf

配置文件

[root@localhost mongocluster]# cat config1.conf 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongodconfig1.log

# Where and how to store data.
storage:
  dbPath: /dbstorage/mongoconfig1
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongodconfig1.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

sharding:
  clusterRole: configsvr
replication:
  replSetName: mc0
net:
  port: 27019
  bindIp: localhost,10.1.1.10

[root@localhost mongocluster]# cat config2.conf  
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongodconfig2.log

# Where and how to store data.
storage:
  dbPath: /dbstorage/mongoconfig2
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongodconfig2.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

sharding:
  clusterRole: configsvr
replication:
  replSetName: mc0
net:
  port: 27020
  bindIp: localhost,10.1.1.10

[root@localhost mongocluster]# cat config3.conf  
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongodconfig3.log

# Where and how to store data.
storage:
  dbPath: /dbstorage/mongoconfig3
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongodconfig3.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

sharding:
  clusterRole: configsvr
replication:
  replSetName: mc0
net:
  port: 27021
  bindIp: localhost,10.1.1.10

初始化replicaset

rs.initiate(
  {
    _id: "mc0",
    configsvr: true,
    members: [
      { _id : 0, host : "10.1.1.10:27019" },
      { _id : 1, host : "10.1.1.10:27020" },
      { _id : 2, host : "10.1.1.10:27021" }
    ]
  }
)

shard搭建

shard集群启动配置文件为3个组成replicaset

shard1.conf
shard2.conf
shard3.conf

创建目录

mkdir /dbstorage/mongoshard3
mkdir /dbstorage/mongoshard2
mkdir /dbstorage/mongoshard1

启动命令:

mongod --config shard1.conf
mongod --config shard2.conf
mongod --config shard3.conf

配置文件内容

[root@localhost mongocluster]# cat shard1.conf
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongodshard1.log

# Where and how to store data.
storage:
  dbPath: /dbstorage/mongoshard1
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongodshard1.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

sharding:
  clusterRole: shardsvr
replication:
  replSetName: s0
net:
  port: 27016
  bindIp: localhost,10.1.1.10

[root@localhost mongocluster]# cat shard2.conf
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongodshard2.log

# Where and how to store data.
storage:
  dbPath: /dbstorage/mongoshard2
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongodshard2.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

sharding:
  clusterRole: shardsvr
replication:
  replSetName: s0
net:
  port: 27017
  bindIp: localhost,10.1.1.10

[root@localhost mongocluster]# cat shard3.conf
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongodshard3.log

# Where and how to store data.
storage:
  dbPath: /dbstorage/mongoshard3
  journal:
    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongodshard3.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

sharding:
  clusterRole: shardsvr
replication:
  replSetName: s0
net:
  port: 27018
  bindIp: localhost,10.1.1.10

设置replicaset

rs.initiate(
  {
    _id : "s0",
    members: [
      { _id : 0, host : "10.1.1.10:27016" },
      { _id : 1, host : "10.1.1.10:27017" },
      { _id : 2, host : "10.1.1.10:27018" }
    ]
  }
)

mongos节点

mongos集群启动配置文件为1个节点

mongos1.conf

创建目录

mkdir /dbstorage/mongos1

启动命令:

mongos --config mongos1.conf

配置文件内容

[root@localhost mongocluster]# cat mongos1.conf 
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongodmongos1.log

# Where and how to store data.
#storage:
#  dbPath: /dbstorage/mongos1
#  journal:
#    enabled: true
#  engine:
#  wiredTiger:

# how the process runs
processManagement:
  fork: true  # fork and run in background
  pidFilePath: /var/run/mongodb/mongodmongos1.pid  # location of pidfile
  timeZoneInfo: /usr/share/zoneinfo

sharding:
  configDB: mc0/10.1.1.10:27019,10.1.1.10:27020,10.1.1.10:27021
net:
  port: 3717
  bindIp: localhost,10.1.1.10

使用mongos连接mongo shard

mongo --host localhost --port 3717

将之前创建好的shard加入cluster,或者可以创建新的shard,加入集群

sh.addShard( "s0/10.1.1.10:27016,10.1.1.10:27017,10.1.1.10:27018")

查看状态

sh.status()

将一个数据库做sharding

sh.enableSharding("<database>")

对collection做sharding

sh.shardCollection("<database>.<collection>", { <shard key field> : "hashed" } )
sh.shardCollection("<database>.<collection>", { <shard key field> : 1, ... } )

附启动脚本,酌情修改

#!/bin/bash

/usr/bin/mongod --config /root/mongocluster/config1.conf
/usr/bin/mongod --config /root/mongocluster/config2.conf
/usr/bin/mongod --config /root/mongocluster/config3.conf
/usr/bin/mongod --config /root/mongocluster/shard1.conf
/usr/bin/mongod --config /root/mongocluster/shard2.conf
/usr/bin/mongod --config /root/mongocluster/shard3.conf
/usr/bin/mongos --config /root/mongocluster/mongos1.conf
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MongoDB 是一种文档型数据库,它可以通过分片来实现高可用性和扩展性。MongoDB 分片是将一个大的 MongoDB 数据库分成多个部分,每个部分称为一个 Shard。一个 MongoDB 分片集群通常由多个 Shard、多个复制集和多个配置服务器组成。 下面是MongoDB数据库集群的基本架构: 1. 分片(Sharding):将数据划分成多个片段,每个片段称为一个Shard。Shard是MongoDB中存储数据的最小单位,每个Shard可以是单台服务器或是一个复制集。 2. 复制集(Replica Set):MongoDB中的复制集是一组维护相同数据副本的MongoDB服务器。每个复制集包含一个Primary节点和多个Secondary节点,Primary节点负责处理所有的写操作,Secondary节点负责复制Primary节点的数据。 3. 配置服务器(Config Server):配置服务器维护了整个集群的元数据信息,包括分片信息、复制集信息等。每个配置服务器都保存了所有集群的元数据信息的一份拷贝。 4. Mongos路由器(Mongos Router):Mongos路由器是一个轻量级的进程,用于将客户端请求路由到正确的Shard上。 在MongoDB集群中,每个Shard都存储了部分数据,Mongos路由器根据某种规则将请求路由到相应的Shard上,Shard接收到请求后将数据返回给Mongos路由器,最终路由器将结果返回给客户端。配置服务器用于记录集群的元数据信息,包括Shard信息、数据分布信息等等。 通过分片来实现数据的水平扩展,可以将数据存储在多台服务器上,提高数据的可扩展性和可用性。同时,MongoDB还提供了复制集实现数据的高可用性,确保数据不会因为单点故障而丢失。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值