MongoDB 5 安装&副本集搭建&集群搭建

Nobody / 2024-11-07 / 原文

单节点安装

需要一个数据目录地址,一个日志文件地址,配置文件

## 数据目录地址
--dapath=
## 日志文件
--logpath=
## 配置文件
--config=***.conf

下载

https://fastdl.mongodb.org/osx/mongodb-macos-x86_64-5.0.27.tgz

启动命令

/data/dba/yanhao/application/mongodb/bin/mongod --config=/data/dba/yanhao/application/mongodbData/single/mongodb1.conf

客户端登录命令

## --host指定ip,--port指定端口
/data/dba/yanhao/application/mongodb/bin/mongo --host 127.0.0.1 --port 27030

## 创建用户,关闭mongodb
## 配置文件打开认证
## 重启mongodb

主从搭建

  1. 创建另一个节点,需要data目录,log文件,配置文件

  2. 启动

rs.add("ip:port");
## 进入从节点执行,表明允许从从节点上读取数据
rs.secondaryOk();

开启认证

注意:密钥的读写权限不能太大,400即可,否则启动失败

## 生成密钥文件
openssl rand -base64 666 > mongodb.key

集群搭建

configServer IP: 
127.0.0.1:27017

routerServer IP
127.0.0.1:27018

configServer IP:
     shard1:
         127.0.0.1:27020
         127.0.0.1:27021
         127.0.0.1:27022
     shard2:
         127.0.0.1:27023
         127.0.0.1:27024
         127.0.0.1:27025
 
           

建立联系

mongos中用sh.addShard添加分片信息
mongos的配置文件中指定了config文件的数据库地址

启动顺序

config服务器必须在mongos服务器启动之前启动


config服务器启动
config服务器初始化
shard服务器
mongos服务器

configServer配置文件

systemLog:
   destination: file
   logAppend: true
   path: /data/dba/yanhao/configServer/config1/mongodb/mongodb.log

storage:
   dbPath: /data/dba/yanhao/configServer/config1/mongo
   journal:
     enabled: true

processManagement:
   fork: true  # fork and run in background
   pidFilePath: /data/dba/yanhao/configServer/config1/mongodb/mongodb.pid  # location of pidfile
   

# network interfaces
net:
   port: 27017
   bindIp: 127.0.0.1  

# shard
sharding:
  clusterRole: configsvr
  
# repliuca set
replication:
  replSetName: config

mongos配置文件

systemLog:
   destination: file
   logAppend: true
   path: /data/dba/yanhao/routerServer/mongodb/mongodb.log
   
processManagement:
   fork: true  # fork and run in background
   pidFilePath: /data/dba/yanhao/routerServer/mongodb/mongodb.pid  # location of pidfile
#   timeZoneInfo: /usr/share/zoneinfo

# network interfaces
net:
   port: 27018
   bindIp: 127.0.0.1 

sharding:
  configDB: config/127.0.0.1:27017

shardServer配置文件

  • shard1

    • shard_node1

      systemLog:
         destination: file
         logAppend: true
         path: /data/dba/yanhao/shardServer/shard1/shard_node1/mongodb/mongodb.log
      
      storage:
         dbPath: /data/dba/yanhao/shardServer/shard1/shard_node1/mongo
         journal:
           enabled: true
      
      processManagement:
         fork: true  # fork and run in background
         pidFilePath: /data/dba/yanhao/shardServer/shard1/shard_node1/mongodb/mongod.pid  # location of pidfile
      #   timeZoneInfo: /usr/share/zoneinfo
      
      # network interfaces
      net:
         port: 27020
         bindIp: 127.0.0.1  
      
      # shard
      sharding:
        clusterRole: shardsvr
        
      # repliuca set
      replication:
        replSetName: shard01
      
    • shard_node2

      systemLog:
         destination: file
         logAppend: true
         path: /data/dba/yanhao/shardServer/shard1/shard_node2/mongodb/mongodb.log
      
      storage:
         dbPath: /data/dba/yanhao/shardServer/shard1/shard_node2/mongo
         journal:
           enabled: true
      
      processManagement:
         fork: true  # fork and run in background
         pidFilePath: /data/dba/yanhao/shardServer/shard1/shard_node2/mongodb/mongodb.pid  # location of pidfile
      #   timeZoneInfo: /usr/share/zoneinfo
      
      # network interfaces
      net:
         port: 27021
         bindIp: 127.0.0.1  
      # shard
      sharding:
        clusterRole: shardsvr
        
      # repliuca set
      replication:
        replSetName: shard01
      
    • shard_node3

      systemLog:
         destination: file
         logAppend: true
         path: /data/dba/yanhao/shardServer/shard1/shard_node3/mongodb/mongodb.log
      
      storage:
         dbPath: /data/dba/yanhao/shardServer/shard1/shard_node3/mongo
         journal:
           enabled: true
      
      processManagement:
         fork: true  # fork and run in background
         pidFilePath: /data/dba/yanhao/shardServer/shard1/shard_node3/mongodb/mongodb.pid  # location of pidfile
      #   timeZoneInfo: /usr/share/zoneinfo
      
      # network interfaces
      net:
         port: 27022
         bindIp: 127.0.0.1  
      
      # shard
      sharding:
        clusterRole: shardsvr
        
      # repliuca set
      replication:
        replSetName: shard01
      
  • shard2

    • shard_node1

      systemLog:
         destination: file
         logAppend: true
         path: /data/dba/yanhao/shardServer/shard2/shard_node1/mongodb/mongodb.log
      
      storage:
         dbPath: /data/dba/yanhao/shardServer/shard2/shard_node1/mongo
         journal:
           enabled: true
      
      processManagement:
         fork: true  # fork and run in background
         pidFilePath: /data/dba/yanhao/shardServer/shard2/shard_node1/mongodb/mongodb.pid  # location of pidfile
      #   timeZoneInfo: /usr/share/zoneinfo
      
      # network interfaces
      net:
         port: 27023
         bindIp: 127.0.0.1  
      # shard
      sharding:
        clusterRole: shardsvr
        
      # repliuca set
      replication:
        replSetName: shard02
      
    • shard_node2

      systemLog:
         destination: file
         logAppend: true
         path: /data/dba/yanhao/shardServer/shard2/shard_node2/mongodb/mongodb.log
      
      storage:
         dbPath: /data/dba/yanhao/shardServer/shard2/shard_node2/mongo
         journal:
           enabled: true
      
      processManagement:
         fork: true  # fork and run in background
         pidFilePath: /data/dba/yanhao/shardServer/shard2/shard_node2/mongodb/mongodb.pid  # location of pidfile
      #   timeZoneInfo: /usr/share/zoneinfo
      
      # network interfaces
      net:
         port: 27024
         bindIp: 127.0.0.1  
      # shard
      sharding:
        clusterRole: shardsvr
        
      # repliuca set
      replication:
        replSetName: shard02
      
    • shard_node3

      systemLog:
         destination: file
         logAppend: true
         path: /data/dba/yanhao/shardServer/shard2/shard_node3/mongodb/mongodb.log
      
      storage:
         dbPath: /data/dba/yanhao/shardServer/shard2/shard_node3/mongo
         journal:
           enabled: true
      
      processManagement:
         fork: true  # fork and run in background
         pidFilePath: /data/dba/yanhao/shardServer/shard2/shard_node3/mongodb/mongodb.pid  # location of pidfile
      #   timeZoneInfo: /usr/share/zoneinfo
      
      # network interfaces
      net:
         port: 27025
         bindIp: 127.0.0.1  
      # shard
      sharding:
        clusterRole: shardsvr
        
      # repliuca set
      replication:
        replSetName: shard02
      

分片操作

  • 对数据库启用分片

    sh.enableSharding("dbName")
    
  • 对集合启用分片

    sh.shardCollection("dbName.collectionName",{"shardKeyFiled":"hashed"})
    

启动

  • 登入分片客户端,将分片内节点添加到replSet内,rs.add("ip:port")

  • 登入mongos客户端,添加分片,sh.addShard("shardName/ip:port,ip:port,ip:port")

    • 注意,启动mongos客户端命令是mongos -f ...mongodb.conf,不是mongod -f ...