第7.1.1Spring操作mongodb之mongodb4.x集群搭建

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/warrah/article/details/84107049

mongodb官网下载
mongodb安装命令比较简单,如下

tar zxvf mongodb-linux-x86_64-4.0.4.tgz
ln -s mongodb-linux-x86_64-4.0.4 mongodb

配置bash_profile

MONGODB_HOME=/application/mongodb
export PATH=$PATH:$MONGODB_HOME/bin

关于mongo集群搭建,参考MongoDB4.0.2集群搭建,不过MongoDB 高可用集群搭建(3.4)写的更为清晰
我有三台机器192.168.5.97、98、99
端口定义为:mongos为 20000, config server 为 21000, shard1为 22001 , shard2为22002, shard3为22003.

#建立mongos及日志目录
mkdir -p /data/mongodb/mongos/log
#建立config server 数据文件存放目录
mkdir -p /data/mongodb/config/data
#建立config server 日志文件存放目录
mkdir -p /data/mongodb/config/log
#建立shard1 数据文件存放目录
mkdir -p /data/mongodb/shard1/data
#建立shard1 日志文件存放目录
mkdir -p /data/mongodb/shard1/log
#建立shard2 数据文件存放目录
mkdir -p /data/mongodb/shard2/data
#建立shard2 日志文件存放目录
mkdir -p /data/mongodb/shard2/log
#建立shard3 数据文件存放目录
mkdir -p /data/mongodb/shard3/data
#建立shard3 日志文件存放目录
mkdir -p /data/mongodb/shard3/log

1 Config server配置
执行cd /data/mongodb/config,vi config.conf

# config.conf
# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/config/log/config.log
 
# Where and how to store data.
storage:
  dbPath: /data/mongodb/config/data
  journal:
    enabled: true

# how the process runs
processManagement:
  fork: true
  pidFilePath: /data/mongodb/config/log/configsrv.pid
 
# network interfaces
net:
  port: 21000
  bindIp: 0.0.0.0
 
#operationProfiling:
replication:
    replSetName: configs       

sharding:
    clusterRole: configsvr

2 Shard server配置
可以看出来下面shared1、shared2、shared3三个配置并没有特别的差异的地方。
Shard 1副本集

# shared1.conf
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/shard1/log/shared1.log
 
# Where and how to store data.
storage:
  dbPath: /data/mongodb/shard1/data
  journal:
    enabled: true

# how the process runs
processManagement:
  fork: true
  pidFilePath: /data/mongodb/shard1/log/shard1.pid
 
# network interfaces
net:
  port: 27001
  bindIp: 0.0.0.0
 
#operationProfiling:
replication:
    replSetName: shard1     

sharding:
    clusterRole: shardsvr

Shard 2副本集

# shared2.conf
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/shard2/log/shared2.log
 
# Where and how to store data.
storage:
  dbPath: /data/mongodb/shard2/data
  journal:
    enabled: true

# how the process runs
processManagement:
  fork: true
  pidFilePath: /data/mongodb/shard2/log/shard2.pid
 
# network interfaces
net:
  port: 27002
  bindIp: 0.0.0.0
 
#operationProfiling:
replication:
    replSetName: shard2     

sharding:
    clusterRole: shardsvr
# shared3.conf
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/shard3/log/shared3.log
 
# Where and how to store data.
storage:
  dbPath: /data/mongodb/shard3/data
  journal:
    enabled: true

# how the process runs
processManagement:
  fork: true
  pidFilePath: /data/mongodb/shard3/log/shard3.pid
 
# network interfaces
net:
  port: 27003
  bindIp: 0.0.0.0
 
#operationProfiling:
replication:
    replSetName: shard3     

sharding:
    clusterRole: shardsvr

3 mongos server路由服务器

# mongo.conf
systemLog:
  destination: file
  logAppend: true
  path: /data/mongodb/mongos/log/mongodbmongos.log
processManagement:
  fork: true 
# network interfaces
net:
  port: 20000
  bindIp: 0.0.0.0
#监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
sharding:
   configDB: configs/test97:21000,test98:21000,test99:21000

4 启动服务
4.1 config server
注意97、98、99三台机器都要启动。

mongod -f /data/mongodb/config/config.conf 

在任何一台config server执行命令mongo --port 21000,进入配置窗口,注意host要改成你自己的

config = {
    _id : "configs",
     members : [
         {_id : 0, host : "host97:21000" },
         {_id : 1, host : "host98:21000" },
         {_id : 2, host : "host99:21000" }
     ]
}

执行下面命令

#初始化副本集
rs.initiate(config)
#查看分区状态
rs.status();

4.2 shard server分片服务器
在三台机器上都执行

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

在任意一台机器上执行mongo --port 27001,接着输入.需要注意的是我在test99上执行,那么就不能在test99上设置arbiterOnly :true,否则就会报章节5.2的错误。

config = {
    _id : "shard1",
     members : [
         {_id : 0, host : "test97:27001", priority : 2 },
         {_id : 1, host : "test98:27001" , arbiterOnly :true},
         {_id : 2, host : "test99:27001", priority : 1 }
     ]
}
rs.initiate(config)

1
另外两个副本按此模式执行,就可以。这样就实现三个仲裁节点的shard

mongod -f /data/mongodb/shard2/shard2.conf
# 在test99中执行
mongo --port 27002
config = {
    _id : "shard2",
     members : [
         {_id : 0, host : "hbbw97:27002",arbiterOnly :true },
         {_id : 1, host : "hbbw98:27002" , priority : 2},
         {_id : 2, host : "hbbw99:27002", priority : 1 }
     ]
}
rs.initiate(config)
mongod -f /data/mongodb/shard3/shard3.conf
# 在test98中执行,这个地方不是在test99的机器上,需要注意
mongo --port 27003
config = {
    _id : "shard3",
     members : [
         {_id : 0, host : "hbbw97:27003", priority : 2 },
         {_id : 1, host : "hbbw98:27003" , priority : 1},
         {_id : 2, host : "hbbw99:27003", arbiterOnly :true }
     ]
}
rs.initiate(config)

4.3 mongos server
注意:启动mongodb时,先启动配置服务器和分片服务器,最后启动路由服务器

mongos -f /data/mongodb/mongos/mongos.conf
# 在任意一台路由服务器执行
mongo --port 20000
use admin
sh.addShard("shard1/test97:27001,test98:27001,test99:27001")
sh.addShard("shard2/test97:27002,test98:27002,test99:27002")
sh.addShard("shard3/test97:27003,test98:27003,test99:27003")
#查看集群状态
sh.status()
# 新建数据manpower
sh.enableSharding('manpower')

查看分区状态
1
4.4 mongodb访问
配置mongodb集群后,访问端口不是27017,而是路由服务器的端口20000
4.4 启动命令汇总

mongod -f /data/mongodb/config/config.conf 
mongod -f /data/mongodb/shard1/shard1.conf
mongod -f /data/mongodb/shard2/shard2.conf
mongod -f /data/mongodb/shard3/shard3.conf
mongos -f /data/mongodb/mongos/mongos.conf

5 异常信息
5.1 replSetInitiate quorum check failed because not all proposed set members responded affirmatively
这个错误是我在执行rs.initiate(config),三台config server我都起来了,端口也都正确,那么错误在哪儿呢?关闭防火墙后ok了

# 停止firewall
systemctl stop firewalld.service
# 禁止firewall开机启动
systemctl disable firewalld.service 
# 查看防火墙状态
firewall-cmd --state

1
5.2 This node, test99:27001, with _id 2 is not electable under the new configuration version 1 for replica set shard1
导致出现这个问题的是,设置shard配置的节点,与arbiterOnly :true不应该是一台机器,在一台机器就不对了
5.3 No host described in new configuration 1 for replica set shard2 maps to this node
这个错误的原因是shard2与下面对应的端口不匹配。

config = {
    _id : "shard2",
     members : [
         {_id : 0, host : "test97:27001",arbiterOnly :true },
         {_id : 1, host : "test98:27001" , priority : 2},
         {_id : 2, host : "test99:27001", priority : 1 }
     ]
}

5.4 Unrecognized option: sharding.configDB
当我执行mongod -f /data/mongodb/mongos/mongos.conf出现了标题的错误,这篇MongoDB4.0.2集群搭建误导我了,应该使用mongos而不是mongod

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

猜你喜欢

转载自blog.csdn.net/warrah/article/details/84107049