/ 288浏览

mongo集群搭建

文章目录

mongo集群搭建

1. 环境介绍

192.168.100.10 u10
192.168.100.11 u12
192.168.100.12 u11

2. 配置hosts文件

配置路径 /etc/hosts

3. 下载mongo

https://downloads.mongodb.com/linux/mongodb-linux-x86_64-enterprise-rhel70-4.2.8.tgz

4. 解压创建文件夹

执行以下命令
mkdir -p /momgodb
cd mongodb
tar -zxvf mongodb-linux-x86_64-enterprise-rhel70-4.2.8.tgz
mv mongodb-linux-x86_64-enterprise-rhel70-4.2.8 mongodb-4.2.8
mkdir -p logs
mkdir -p conf
mkdir -p data/{0,1,2,scvr}

5. 配置副本集

每台机器创建以下三个文件到conf
#mongod0.yaml
systemLog:
   destination: file
   path: "/mongodb/logs/mongod0.log"
   logAppend: true
storage:
   journal:
      enabled: true
   dbPath: "/mongodb/data/0"
processManagement:
   fork: true
net:
   bindIp: 0.0.0.0
   port: 27016
setParameter:
   enableLocalhostAuthBypass: false
replication:
   replSetName: "rs0"
sharding:
   clusterRole: shardsvr
#开启密码验证
#security:
#  keyFile: /mongodb/conf/auth_key.conf
#  authorization: enabled
#mongod1.yaml
systemLog:
   destination: file
   path: "/mongodb/logs/mongod1.log"
   logAppend: true
storage:
   journal:
      enabled: true
   dbPath: "/mongodb/data/1"
processManagement:
   fork: true
net:
   bindIp: 0.0.0.0
   port: 27017
setParameter:
   enableLocalhostAuthBypass: false
replication:
   replSetName: "rs1"
sharding:
   clusterRole: shardsvr
#security:
#  keyFile: /mongodb/conf/auth_key.conf
#  authorization: enabled
#mongod2.yaml
systemLog:
   destination: file
   path: "/mongodb/logs/mongod2.log"
   logAppend: true
storage:
   journal:
      enabled: true
   dbPath: "/mongodb/data/2"
processManagement:
   fork: true
net:
   bindIp: 0.0.0.0
   port: 27018
setParameter:
   enableLocalhostAuthBypass: false
replication:
   replSetName: "rs2"
sharding:
   clusterRole: shardsvr
#security:
#  keyFile: /mongodb/conf/auth_key.conf
#  authorization: enabled

6. 生成auth_key.conf

openssl rand -base64 756 > /mongodb/conf/auth_key.conf

chmod 400 /mongodb/conf/auth_key.conf

将文件分别复制到三台机器上的conf目录下

例如:scp /mongodb/conf/auth_key.conf 192.168.100.11:/mongodb/conf/

7. 配置服务器

将下面文件分别复制到三台服务器conf
#csvr.yaml
systemLog:
  destination: file
  #日志存储位置
  path: "/mongodb/logs/csvr.log"
  logAppend: true
storage:
  journal:
    enabled: true
  #数据文件存储位置
  dbPath: "/mongodb/data/csvr"
  #是否一个库一个文件夹
  directoryPerDB: true
  #WT引擎配置
  wiredTiger:
    engineConfig:
      #WT最大使用cache(根据服务器实际情况调节)
      cacheSizeGB: 1
      #是否将索引也按数据库名单独存储
      directoryForIndexes: true
    #表压缩配置
    collectionConfig:
      blockCompressor: zlib
    #索引配置
    indexConfig:
      prefixCompression: true
#端口配置
net:
  bindIp: 0.0.0.0
  port: 27019
replication:
  oplogSizeMB: 2048
  replSetName: csvr
sharding:
  clusterRole: configsvr
processManagement:
  fork: true
 #登录密码验证
#security:
#  keyFile: /mongodb/conf/auth_key.conf
#  authorization: enabled

8. 配置mongos

将下面文件分别复制到三台服务器conf
#mongos.yaml
systemLog:
  destination: file
  path: "/mongodb/logs/mongos.log"
  logAppend: true
net:
  bindIp: 0.0.0.0
  port: 27020
# 将confige server 添加到路由
sharding:
  configDB: csvr/u10:27019,u11:27019,u12:27019
processManagement:
  fork: true
 #登录密码验证
#security:
#  keyFile: /mongodb/conf/auth_key.conf

9. 启动配置服务

三台机器分别启动 
在mongodb目录下执行如下命令 
mongodb-4.2.8/bin/mongod -f conf/csvr.yaml

启动完成后随便一台机器中执行
mongodb-4.2.8/bin/mongo -port 27019

#在mongo shell中执行
rs.initiate( {
   _id : "csvr",
   configsvr: true,
   members: [
      { _id: 0, host: "u10:27019" },
      { _id: 1, host: "u11:27019" },
      { _id: 2, host: "u12:27019" }
   ]
   
})

10. 启动配置副本集

三台机器分别启动 
在mongodb目录下执行如下命令 
mongodb-4.2.8/bin/mongod -f conf/mongod0.yaml
mongodb-4.2.8/bin/mongod -f conf/mongod1.yaml
mongodb-4.2.8/bin/mongod -f conf/mongod2.yaml

启动完成后随便一台机器中执行
mongodb-4.2.8/bin/mongo -port 27016
#在mongo shell中执行
rs.initiate( {
   _id : "rs0",
   members: [
      { _id: 0, host: "u10:27016" },
      { _id: 1, host: "u11:27016" },
      { _id: 2, host: "u12:27016" }
   ]
})

mongodb-4.2.8/bin/mongo -port 27017
#在mongo shell中执行
rs.initiate( {
   _id : "rs1",
   members: [
      { _id: 0, host: "u10:27017" },
      { _id: 1, host: "u11:27017" },
      { _id: 2, host: "u12:27017" }
   ]
})

mongodb-4.2.8/bin/mongo -port 27018
#在mongo shell中执行
rs.initiate( {
   _id : "rs2",
   members: [
      { _id: 0, host: "u10:27018" },
      { _id: 1, host: "u11:27018" },
      { _id: 2, host: "u12:27018" }
   ]
})

11. 启动配置mongos

 三台机器分别启动 
在mongodb目录下执行如下命令 
mongodb-4.2.8/bin/mongos -f conf/mongos.yaml
 启动完成后随便一台机器中执行
mongodb-4.2.8/bin/mongo -port 27020

添加分片
#使用admin数据库
use  admin

#串联路由服务器与分配副本集
db.runCommand( { addshard : "rs0/u10:27016,u11:27016,u12:27016",name:"shard1"} )
db.runCommand( { addshard : "rs1/u10:27017,u11:27017,u12:27017",name:"shard2"} )
db.runCommand( { addshard : "rs2/u10:27018,u11:27018,u12:27018",name:"shard3"} )


#查看集群状态
sh.status()
#对数据库进行分片
db.runCommand( { enablesharding :"dbName"});

12. 开启密码验证

首先创建root用户 
随便一台机器中执行
mongodb-4.2.8/bin/mongo -port 27020

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

创建指定数据库用户
db.createUser({user:"normal", pwd:"isiteam", roles:[{role:"dbOwner", db:"product"}]})

取消conf下面yaml配置文件中注释的登录验证配置(一共四个配置)

重新启动服务

启动顺序只能为:
1.启动配置服务
2.启动副本集
3.启动mongos

然后可以用创建的账号进行登录
地址为192.168.100.10:27020;192.168.100.11:27020;192.168.100.12:27020中的任何一个


启动脚本
#1_start.sh
mongodb-4.2.8/bin/mongod -f conf/csvr.yaml
#2_start.sh
mongodb-4.2.8/bin/mongod -f conf/mongod0.yaml
mongodb-4.2.8/bin/mongod -f conf/mongod1.yaml
mongodb-4.2.8/bin/mongod -f conf/mongod2.yaml
#3_start.sh
mongodb-4.2.8/bin/mongos -f conf/mongos.yaml

13. springboot配置链接

spring:
   data:
     mongodb:
       uri: mongodb://normal:isiteam@192.168.100.10:27020,192.168.100.11:27020,192.168.100.12:27020/product?authSource=admin&authMechanism=SCRAM-SHA-1&maxPoolSize=500&minPoolSize=10