mongodb 3.4 集群搭建升级版 五台集群

mongodb 3.4 集群搭建升级版 五台集群

纯洁的微笑 2017-09-21 原文html

 

最新版mongodb推荐使用yaml语法来作配置,另一些旧的配置在最新版本中已经不在生效,因此咱们在生产实际搭建mongodb集群的时候作了一些改进。若是你们不熟悉什么是分片、副本集、仲裁者的话请先移步查看上一篇文章:mongodb 3.4 集群搭建:分片+副本集node

和前一个版本相比,改动点有:linux

  • 配置文件采用yaml方式来配置
  • 生产中取消了仲裁者的角色,由于仲裁者也不会存储数据,只是起到选举的做用,线上为了保证数据安全,每份数据都会配置两个副本集,也就是每份数据存储了三份。
  • 优化配置,采用五台集群
  • 使用非root帐户搭建mongodb集群。

环境准备

系统系统 centos6.9
五台服务器:192.168.0.31/32/33/34/35
安装包: mongodb-linux-x86_64-3.4.6.tgzgit

服务器规划github

服务器31 服务器32 服务器33 服务器34 服务器35
mongos server mongos server config server config server config server
shard1 server shard2 server shard3 server shard4 server shard5 server
shard5 server shard1 server shard2 server shard3 server shard4 server
shard4 server shard5 server shard1 server shard2 server shard3 server

端口分配:mongodb

 
  1. mongos:20000
  2. config:21000
  3. shard1:27001
  4. shard2:27002
  5. shard3:27003
  6. shard4:27004
  7. shard5:27005

权限分配:数据库

登陆root帐户,将安装目录和数据目录权限分配给平常操做(youknow)帐户vim

 
  1. chown -R youknow:youknow /usr/local/
  2. chown -R youknow:youknow /data

mongodb安装

一、下载

下载 mongodb 3.4.6 安装包centos

 
  1. curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-3.4.6.tgz
 
  1. #解压
  2. tar -xzvf mongodb-linux-x86_64-3.4.6.tgz -C /usr/local/
  3. #更名
  4. mv mongodb-linux-x86_64-3.4.6 mongodb

二、建立相关目录

根据服务器的规范,分别在对应的服务器上创建conf、mongos、config、shard一、shard二、shard三、shard四、shard5等目录,由于mongos不存储数据,只须要创建日志文件目录便可。安全

 
  1. mkdir -p /usr/local/mongodb/conf
  2. mkdir -p /data/mongos/log
  3. mkdir -p /data/config/data
  4. mkdir -p /data/config/log
  5. mkdir -p /data/shard1/data
  6. mkdir -p /data/shard1/log
  7. mkdir -p /data/shard2/data
  8. mkdir -p /data/shard2/log
  9. mkdir -p /data/shard3/data
  10. mkdir -p /data/shard3/log
  11. mkdir -p /data/shard4/data
  12. mkdir -p /data/shard4/log
  13. mkdir -p /data/shard5/data
  14. mkdir -p /data/shard5/log

三、环境变量

为了后续方便操做,配置mongodb的环境变量,须要切到root用户下面

 
  1. vim /etc/profile
  2. # 内容
  3. export MONGODB_HOME=/usr/local/mongodb
  4. export PATH=$MONGODB_HOME/bin:$PATH
  5. # 使当即生效,在安装用户下(youknow)执行
  6. source /etc/profile

查看mongodb版本信息mongod -v 输出版本信息代表配置环境变量成功

集群配置

一、config server配置服务器

在服务器3三、3四、35上配置如下内容:

添加配置文件:

添加配置文件

 
  1. vi /usr/local/mongodb/conf/config.conf
  2.  
  3. ## content
  4. systemLog:
  5. destination: file
  6. logAppend: true
  7. path: /data/config/log/config.log
  8.  
  9. # Where and how to store data.
  10. storage:
  11. dbPath: /data/config/data
  12. journal:
  13. enabled: true
  14. # how the process runs
  15. processManagement:
  16. fork: true
  17. pidFilePath: /data/config/log/configsrv.pid
  18.  
  19. # network interfaces
  20. net:
  21. port: 21000
  22. bindIp: 192.168.0.33
  23.  
  24. #operationProfiling:
  25. replication:
  26. replSetName: config
  27.  
  28. sharding:
  29. clusterRole: configsvr

启动三台服务器的config server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/config.conf

登陆任意一台配置服务器,初始化配置副本集

 
  1. #链接
  2. mongo 192.168.0.33:21000
  3. #config变量
  4. config = {
  5. ... _id : "config",
  6. ... members : [
  7. ... {_id : 0, host : "192.168.0.33:21000" },
  8. ... {_id : 1, host : "192.168.0.34:21000" },
  9. ... {_id : 2, host : "192.168.0.35:21000" }
  10. ... ]
  11. ... }
  12.  
  13. #初始化副本集
  14. rs.initiate(config)
  15.  
  16. #查看分区状态
  17. rs.status();

其中,"_id" : "configs"应与配置文件中配置的 replicaction.replSetName 一致,"members" 中的 "host" 为三个节点的ip和port

这样配置服务器就配置好了

二、配置分片、副本集

配置第一个分片副本集

在服务器 3一、3二、33上面作如下配置

配置文件

 
  1. vi /usr/local/mongodb/conf/shard1.conf
  2.  
  3. #配置文件内容
  4. # where to write logging data.
  5. systemLog:
  6. destination: file
  7. logAppend: true
  8. path: /data/shard1/log/shard1.log
  9.  
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /data/shard1/data
  13. journal:
  14. enabled: true
  15. wiredTiger:
  16. engineConfig:
  17. cacheSizeGB: 20
  18.  
  19. # how the process runs
  20. processManagement:
  21. fork: true
  22. pidFilePath: /data/shard1/log/shard1.pid
  23.  
  24. # network interfaces
  25. net:
  26. port: 27001
  27. bindIp: 192.168.0.33
  28.  
  29. #operationProfiling:
  30. replication:
  31. replSetName: shard1
  32. sharding:
  33. clusterRole: shardsvr

启动三台服务器的shard1 server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard1.conf

登录任意一台服务器,初始化副本集

 
  1. mongo 192.168.0.31:27001
  2. #使用admin数据库
  3. use admin
  4. #定义副本集配置
  5. config = {
  6. ... _id : "shard1",
  7. ... members : [
  8. ... {_id : 0, host : "192.168.0.31:27001" },
  9. ... {_id : 1, host : "192.168.0.32:27001" },
  10. ... {_id : 2, host : "192.168.0.33:27001" }
  11. ... ]
  12. ... }
  13.  
  14. #初始化副本集配置
  15. rs.initiate(config);
  16.  
  17. #查看分区状态
  18. rs.status();

配置第二个分片副本集

在服务器3二、3三、34上面作如下配置

配置文件

 
  1. vi /usr/local/mongodb/conf/shard2.conf
  2.  
  3. #配置文件内容
  4. # where to write logging data.
  5. systemLog:
  6. destination: file
  7. logAppend: true
  8. path: /data/shard2/log/shard2.log
  9.  
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /data/shard2/data
  13. journal:
  14. enabled: true
  15. wiredTiger:
  16. engineConfig:
  17. cacheSizeGB: 20
  18.  
  19. # how the process runs
  20. processManagement:
  21. fork: true
  22. pidFilePath: /data/shard2/log/shard2.pid
  23.  
  24. # network interfaces
  25. net:
  26. port: 27002
  27. bindIp: 192.168.0.33
  28.  
  29. #operationProfiling:
  30. replication:
  31. replSetName: shard2
  32. sharding:
  33. clusterRole: shardsvr

启动三台服务器的shard2 server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard2.conf

登录任意一台服务器,初始化副本集

 
  1. mongo 192.168.0.32:27002
  2. #使用admin数据库
  3. use admin
  4. #定义副本集配置
  5. config = {
  6. ... _id : "shard2",
  7. ... members : [
  8. ... {_id : 0, host : "192.168.0.32:27002" },
  9. ... {_id : 1, host : "192.168.0.33:27002" },
  10. ... {_id : 2, host : "192.168.0.34:27002" }
  11. ... ]
  12. ... }
  13.  
  14. #初始化副本集配置
  15. rs.initiate(config);
  16.  
  17. #查看分区状态
  18. rs.status();

配置第三个分片副本集

在服务器3三、3四、35上面作如下配置

配置文件

 
  1. vi /usr/local/mongodb/conf/shard3.conf
  2.  
  3. #配置文件内容
  4. # where to write logging data.
  5. systemLog:
  6. destination: file
  7. logAppend: true
  8. path: /data/shard3/log/shard3.log
  9.  
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /data/shard3/data
  13. journal:
  14. enabled: true
  15. wiredTiger:
  16. engineConfig:
  17. cacheSizeGB: 20
  18. # how the process runs
  19. processManagement:
  20. fork: true
  21. pidFilePath: /data/shard3/log/shard3.pid
  22.  
  23. # network interfaces
  24. net:
  25. port: 27003
  26. bindIp: 192.168.0.33
  27.  
  28. #operationProfiling:
  29. replication:
  30. replSetName: shard3
  31. sharding:
  32. clusterRole: shardsvr

启动三台服务器的shard3 server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard3.conf

登录任意一台服务器,初始化副本集

 
  1. mongo 192.168.0.33:27003
  2. #使用admin数据库
  3. use admin
  4. #定义副本集配置
  5. config = {
  6. ... _id : "shard3",
  7. ... members : [
  8. ... {_id : 0, host : "192.168.0.33:27003" },
  9. ... {_id : 1, host : "192.168.0.34:27003" },
  10. ... {_id : 2, host : "192.168.0.35:27003" }
  11. ... ]
  12. ... }
  13.  
  14. #初始化副本集配置
  15. rs.initiate(config);
  16.  
  17. #查看分区状态
  18. rs.status();

配置第四个分片副本集

在服务器3四、3五、31上面作如下配置

配置文件

 
  1. vi /usr/local/mongodb/conf/shard4.conf
  2.  
  3. #配置文件内容
  4. # where to write logging data.
  5. systemLog:
  6. destination: file
  7. logAppend: true
  8. path: /data/shard4/log/shard4.log
  9.  
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /data/shard4/data
  13. journal:
  14. enabled: true
  15. wiredTiger:
  16. engineConfig:
  17. cacheSizeGB: 20
  18.  
  19. # how the process runs
  20. processManagement:
  21. fork: true
  22. pidFilePath: /data/shard4/log/shard4.pid
  23.  
  24. # network interfaces
  25. net:
  26. port: 27004
  27. bindIp: 192.168.0.35
  28.  
  29. #operationProfiling:
  30. replication:
  31. replSetName: shard4
  32. sharding:
  33. clusterRole: shardsvr

启动三台服务器的shard4 server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard4.conf

登录任意一台服务器,初始化副本集

 
  1. mongo 192.168.0.34:27004
  2. #使用admin数据库
  3. use admin
  4. #定义副本集配置
  5. config = {
  6. ... _id : "shard4",
  7. ... members : [
  8. ... {_id : 0, host : "192.168.0.34:27004" },
  9. ... {_id : 1, host : "192.168.0.35:27004" },
  10. ... {_id : 2, host : "192.168.0.31:27004" }
  11. ... ]
  12. ... }
  13.  
  14. #初始化副本集配置
  15. rs.initiate(config);
  16.  
  17. #查看分区状态
  18. rs.status();

配置第五个分片副本集

在服务器3五、3一、32上面作如下配置

配置文件

 
  1. vi /usr/local/mongodb/conf/shard5.conf
  2.  
  3. #配置文件内容
  4. # where to write logging data.
  5. systemLog:
  6. destination: file
  7. logAppend: true
  8. path: /data/shard5/log/shard5.log
  9.  
  10. # Where and how to store data.
  11. storage:
  12. dbPath: /data/shard5/data
  13. journal:
  14. enabled: true
  15. wiredTiger:
  16. engineConfig:
  17. cacheSizeGB: 20
  18.  
  19. # how the process runs
  20. processManagement:
  21. fork: true
  22. pidFilePath: /data/shard5/log/shard5.pid
  23.  
  24. # network interfaces
  25. net:
  26. port: 27005
  27. bindIp: 192.168.0.35
  28.  
  29. #operationProfiling:
  30. replication:
  31. replSetName: shard5
  32. sharding:
  33. clusterRole: shardsvr

启动三台服务器的shard5 server

 
  1. numactl --interleave=all mongod --config /usr/local/mongodb/conf/shard5.conf

登录任意一台服务器,初始化副本集

 
  1. mongo 192.168.0.35:27005
  2. #使用admin数据库
  3. use admin
  4. #定义副本集配置
  5. config = {
  6. ... _id : "shard5",
  7. ... members : [
  8. ... {_id : 0, host : "192.168.0.35:27005" },
  9. ... {_id : 1, host : "192.168.0.31:27005" },
  10. ... {_id : 2, host : "192.168.0.32:27005" }
  11. ... ]
  12. ... }
  13.  
  14. #初始化副本集配置
  15. rs.initiate(config);
  16.  
  17. #查看分区状态
  18. rs.status();

至此,五个分片和副本集搭建完毕

三、配置路由服务器 mongos

如下配置在服务器3一、32上执行

注意:先启动配置服务器和分片服务器,后启动路由实例

 
  1. vi /usr/local/mongodb/conf/mongos.conf
  2.  
  3. systemLog:
  4. destination: file
  5. logAppend: true
  6. path: /data/mongos/log/mongos.log
  7. processManagement:
  8. fork: true
  9. # pidFilePath: /usr/local/mongodb/mongos.pid
  10.  
  11. # network interfaces
  12. net:
  13. port: 20000
  14. bindIp: 192.168.0.31
  15. #监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字
  16. sharding:
  17. configDB: configs/192.168.0.33:21000,192.168.0.34:21000,192.168.0.35:21000

启动二台服务器的mongos server

 
  1. mongos --config /usr/local/mongodb/conf/mongos.conf

四、启用分片

目前搭建了mongodb配置服务器、路由服务器,各个分片服务器,不过应用程序链接到mongos路由服务器并不能使用分片机制,还须要在程序里设置分片配置,让分片生效。

登录任意一台mongos

 
  1. mongo 192.168.0.31:20000
  2. #使用admin数据库
  3. use admin
  4. #串联路由服务器与分配副本集
  5. sh.addShard("shard1/192.168.0.31:27001,192.168.0.32:27001,192.168.0.33:27001")
  6. sh.addShard("shard2/192.168.0.32:27002,192.168.0.33:27002,192.168.0.34:27002")
  7. sh.addShard("shard3/192.168.0.33:27003,192.168.0.34:27003,192.168.0.35:27003")
  8. sh.addShard("shard4/192.168.0.34:27004,192.168.0.35:27004,192.168.0.31:27004")
  9. sh.addShard("shard5/192.168.0.35:27005,192.168.0.31:27005,192.168.0.32:27005")
  10. #查看集群状态
  11. sh.status()

这样mongodb的五台集群搭建就已经完成了,后期如何优化和运营请查看下一篇文章。

错误

rs.initiate报错

执行 rs.initiate(config); 报错:

 
  1. rs.initiate(config);
  2. {
  3. "ok" : 0,
  4. "errmsg" : "No host described in new configuration 1 for replica set shard1 maps to this node",
  5. "code" : 93,
  6. "codeName" : "InvalidReplicaSetConfig"
  7. }

最后发现是本身的一个端口号写错了。

启动mongos报错

启动mongos的时候报错:

 
  1. about to fork child process, waiting until server is ready for connections.
  2. forked process: 1436
  3. ERROR: child process failed, exited with error number 1

这个问题卡了咱们半天,找了不少的资料,不是说清理lock文件,就是说清理log文件总无解,最后看到这个网站的提示

ERROR: child process failed, exited with error number 1

去掉了配置文件中 --fork,才将真正的错误日志打印了出来,是咱们的配置文件中的路径写错了,原本是log写成了logs

原来:path: /data/logs/mongos.log

改成:path: /data/log/mongos.log

成功

为了方便你们拿取配置文件,我在github上面放置了一份:mongodb-five-cluster-conf

相关文章
相关标签/搜索