MongoDB——linux下配置分片与副本集

MongoDB 基于文档的分布式非关系数据库

分片:体现水平扩展性

副本集:体现高可用性

 

四台机器

M:172.31.140.161     mongos

A:172.31.140.157     configserver   shard1   shard2   shard3

B:172.31.140.158     configserver   shard1   shard2   shard3

C:172.31.140.159     configserver   shard1   shard2   shard3

configserver   shard1   shard2   shard3均为三节点副本集。

 

1、下载安装文件

cd /root/data/soft

wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.4.4.tgz

tar -xzvf mongodb-linux-x86_64-rhel70-3.4.4.tgz 

 

2、环境变量

vim ~/.bash_profile 

 

export MONGODB=/root/data/soft/mongodb-linux-x86_64-rhel70-3.4.4/bin

export PATH=$MONGODB:$PATH

source ~/.bash_profile 

 

mongod --version

 

3、configserver配置文件与启动方法

mongodb.conf

复制代码
dbpath=/root/data/soft/mongodb/configsvr/db/

logpath=/root/data/soft/mongodb/configsvr/log/mongodb.log

logappend=true

port = 27101

fork = true

maxConns = 1000

journal = true

# PID File 的完整路径,如果没有设置,则没有PID文件
pidfilepath = /root/data/soft/mongodb/configsvr/m.pid

#数据写入磁盘的时间秒数
syncdelay = 60

#设置oplog的大小(MB)
oplogSize = 1000

#设置信数据库.ns文件大小(MB)
nssize = 16

auth = false

configsvr = true

replSet=config_repl
复制代码

分别在A B C配置一份,启动三个节点,命令

 mongod -f /root/data/soft/mongodb/configsvr/mongodb.conf 

登入

mongo --port 27101

配置configserver的副本集

config = {
    _id : "config_repl",
     members : [
         {_id : 0, host : "172.31.140.157:27101" },
         {_id : 1, host : "172.31.140.158:27101" },
         {_id : 2, host : "172.31.140.159:27101" }
     ]
 }

初始化

复制代码
> 
> config = {
...     _id : "config_repl",
...      members : [
...          {_id : 0, host : "172.31.140.157:27101" },
...          {_id : 1, host : "172.31.140.158:27101" },
...          {_id : 2, host : "172.31.140.159:27101" }
...      ]
...  }
{
    "_id" : "config_repl",
    "members" : [
        {
            "_id" : 0,
            "host" : "172.31.140.157:27101"
        },
        {
            "_id" : 1,
            "host" : "172.31.140.158:27101"
        },
        {
            "_id" : 2,
            "host" : "172.31.140.159:27101"
        }
    ]
}
> 

 > rs.initiate(config)

  { "ok" : 1 }

config_repl:SECONDARY> 

config_repl:PRIMARY> 

config_repl:PRIMARY> 

config_repl:PRIMARY> rs.status()

复制代码

 

4、分片shard1的配置文件与启动方法

复制代码
dbpath=/root/data/soft/mongodb/shard1/db/

logpath=/root/data/soft/mongodb/shard1/log/mongodb.log

logappend=true

port = 27201

fork = true

maxConns = 1000

journal = true

# PID File 的完整路径,如果没有设置,则没有PID文件
pidfilepath = /root/data/soft/mongodb/shard1/m.pid

#数据写入磁盘的时间秒数
syncdelay = 60

#设置oplog的大小(MB)
oplogSize = 1000

#设置信数据库.ns文件大小(MB)
nssize = 16

auth = false

shardsvr = true

replSet=shard1_repl
复制代码

分别在 A  B  C各配一次,启动  

mongod -f /root/data/soft/mongodb/shard1/mongodb.conf 

登录

mongo --port 27201 

初始化副本集

复制代码
> 
> rs.status()
{
    "info" : "run rs.initiate(...) if not yet done for the set",
    "ok" : 0,
    "errmsg" : "no replset config has been received",
    "code" : 94,
    "codeName" : "NotYetInitialized"
}
> 
> 
> 
> 
> 
> 
> config = {
...     _id : "shard1_repl",
...      members : [
...          {_id : 0, host : "172.31.140.157:27201" },
...          {_id : 1, host : "172.31.140.158:27201" },
...          {_id : 2, host : "172.31.140.159:27201" }
...      ]
...  }
{
    "_id" : "shard1_repl",
    "members" : [
        {
            "_id" : 0,
            "host" : "172.31.140.157:27201"
        },
        {
            "_id" : 1,
            "host" : "172.31.140.158:27201"
        },
        {
            "_id" : 2,
            "host" : "172.31.140.159:27201"
        }
    ]
}
> 
> rs.initiate(config)
{ "ok" : 1 }
shard1_repl:OTHER> rs.status()
复制代码

以同样的方式启动、初始化 shard2、shard3。

 

5、mongos配置文件与启动方法

在M机器配置

mongodb.conf

复制代码
logpath=/root/data/soft/mongodb/mongos/log/mongodb.log

logappend=true

port = 27017

fork = true

maxConns = 1000

pidfilepath = /root/data/soft/mongodb/mongos/m.pid

configdb=config_repl/172.31.140.157:27101,172.31.140.158:27101,172.31.140.159:27101
复制代码

mongos只有log,不记录数据,从configserver取得数据存入内存,作路由使用

其中 configdb为 configserver的三节点副本集

启动   

mongos -f /root/data/soft/mongodb/mongos/mongodb.conf

登录   

mongo --port 27017

复制代码
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5efa2a6d6e156de96ae022a6")
}
  shards:
  active mongoses:
    "3.4.4" : 1
 autosplit:
    Currently enabled: yes
  balancer:
    Currently enabled:  yes
    Currently running:  no
        Balancer lock taken at Tue Jun 30 2020 01:52:49 GMT+0800 (CST) by ConfigServer:Balancer
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours: 
        No recent migrations
  databases:

mongos> 
mongos> 
复制代码

添加分片,每个分片都是一个副本集

sh.addShard("shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201")
sh.addShard("shard2_repl/172.31.140.157:27202,172.31.140.158:27202,172.31.140.159:27202")
sh.addShard("shard3_repl/172.31.140.157:27203,172.31.140.158:27203,172.31.140.159:27203")
复制代码
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5efa2a6d6e156de96ae022a6")
}
  shards:
    {  "_id" : "shard1_repl",  "host" : "shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201",  "state" : 1 }
    {  "_id" : "shard2_repl",  "host" : "shard2_repl/172.31.140.157:27202,172.31.140.158:27202,172.31.140.159:27202",  "state" : 1 }
    {  "_id" : "shard3_repl",  "host" : "shard3_repl/172.31.140.157:27203,172.31.140.158:27203,172.31.140.159:27203",  "state" : 1 }
  active mongoses:
    "3.4.4" : 1
 autosplit:
    Currently enabled: yes
  balancer:
    Currently enabled:  yes
    Currently running:  no
        Balancer lock taken at Tue Jun 30 2020 01:52:49 GMT+0800 (CST) by ConfigServer:Balancer
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours: 
        No recent migrations
  databases:
    {  "_id" : "loginserver",  "primary" : "shard1_repl",  "partitioned" : false }
    {  "_id" : "loginsaveserver",  "primary" : "shard2_repl",  "partitioned" : false }

mongos> 
mongos> 
mongos> 
复制代码

 

 

至此,基于分片、副本集的MongoDB集群搭完成。

 

 

 

6、启用分片功能

对库启用分片

sh.enableSharding("loginserver")

 

复制代码
mongos> 
mongos> 
mongos> sh.status()
--- Sharding Status --- 
  sharding version: {
    "_id" : 1,
    "minCompatibleVersion" : 5,
    "currentVersion" : 6,
    "clusterId" : ObjectId("5efa2a6d6e156de96ae022a6")
}
  shards:
    {  "_id" : "shard1_repl",  "host" : "shard1_repl/172.31.140.157:27201,172.31.140.158:27201,172.31.140.159:27201",  "state" : 1 }
    {  "_id" : "shard2_repl",  "host" : "shard2_repl/172.31.140.157:27202,172.31.140.158:27202,172.31.140.159:27202",  "state" : 1 }
    {  "_id" : "shard3_repl",  "host" : "shard3_repl/172.31.140.157:27203,172.31.140.158:27203,172.31.140.159:27203",  "state" : 1 }
  active mongoses:
    "3.4.4" : 1
 autosplit:
    Currently enabled: yes
  balancer:
    Currently enabled:  yes
    Currently running:  no
        Balancer lock taken at Tue Jun 30 2020 01:52:49 GMT+0800 (CST) by ConfigServer:Balancer
    Failed balancer rounds in last 5 attempts:  0
    Migration Results for the last 24 hours: 
        No recent migrations
  databases:
    {  "_id" : "loginserver",  "primary" : "shard1_repl",  "partitioned" : true }
    {  "_id" : "loginsaveserver",  "primary" : "shard2_repl",  "partitioned" : true }

mongos> 
复制代码

 

 

posted @   会飞的斧头  阅读(330)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示