MongoDB4.0.2集群搭建

MongoDB4.0.2集群搭建

2018年09月30日 10:03:08 TOTORO_zip 阅读数 916更多linux

分类专栏: MangoDBnginx

https://www.jianshu.com/p/e1855a1d38b7mongodb

MongoDB4.0.2集群搭建

根据对象存储平台Django+MongoDB+Ceph的需求,现搭建部署一个十节点的MongoDB集群,主要如下关键点:数据库

  • 根据最新版本MongoDB推荐,配置文件采用yaml方式来配置
  • 一共10台服务器,即10个节点。对数据集进行分片,共分10个shard
  • 每个shard都进行副本集配置,因为硬件磁盘已进行raid多副本备份,考虑到存储效率,本集群在副本集只须要一个备服务器,故采用1主+1备
    +1仲裁(必须有仲裁节点,否则被服务器没法经过心跳机制升级为主服务器)的副本及配置方式

环境准备

系统:CentOS7.0 64bitvim

十台服务器:mongo00-mongo09服务器

服务器规划:app

mongo00 mongo01 mongo02 mongo03 mongo04 mongo05 mongo06 mongo07 mongo08 mongo09
mongos mongos mongos mongos mongos mongos mongos config config config
shard0主 shard1主 shard2主 shard3主 shard4主 shard5主 shard6主 shard7主 shard8主 shard9主
shard9副 shard0副 shard1副 shard2副 shard3副 shard4副 shard5副 shard6副 shard7副 shard8副
shard8仲 shard9仲 shard0仲 shard1仲 shard2仲 shard3仲 shard4仲 shard5仲 shard6仲 shard7仲

端口分配:运维

 
  1. mongos:20000linux运维

  2. config:21000spa

  3. shard0:27000

  4. shard1:27001

  5. shard2:27002

  6. shard3:27003

  7. shard4:27004

  8. shard5:27005

  9. shard6:27006

  10. shard7:27007

  11. shard8:27008

  12. shard9:27009

MongoDB安装

  1. 下载、安装MongoDB
 
  1. #官网下载

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

  3.  
  4. #解压

  5. tar -xzvf mongodb-linux-x86_64-rhel70-4.0.2.tgz -C /usr/local/

  6. cd /use/local

  7. mv mongodb-linux-x86_64-rhel70-4.0.2 mongodb

  1. 相关目录规划
 
  1. #mongo服务经过配置文件启动,存放配置文件目录/usr/local/mongodb/conf

  2. #存放日志、进程管理信息的目录/var/log/nginx/mongodb

  3. #根据服务器规划,在每台服务器建立该节点所含shard的数据存放目录/mnt/mongodata/shard0-9

  4. #同时在mongo07-mongo09三台服务器建立存放config server数据的数据目录/mnt/mongodata/config

  5.  
  6. #mongo00:

  7. mkdir -p /usr/local/mongodb/conf

  8. mkdir -p /var/log/nginx/mongodb

  9. mkdir -p /mnt/mongodata/shard0

  10. mkdir -p /mnt/mongodata/shard9

  11. mkdir -p /mnt/mongodata/shard8

  12.  
  13. #mongo01:

  14. mkdir -p /usr/local/mongodb/conf

  15. mkdir -p /var/log/nginx/mongodb

  16. mkdir -p /mnt/mongodata/shard0

  17. mkdir -p /mnt/mongodata/shard1

  18. mkdir -p /mnt/mongodata/shard9

  19.  
  20. #mongo02:

  21. mkdir -p /usr/local/mongodb/conf

  22. mkdir -p /var/log/nginx/mongodb

  23. mkdir -p /mnt/mongodata/shard0

  24. mkdir -p /mnt/mongodata/shard1

  25. mkdir -p /mnt/mongodata/shard2

  26.  
  27. #mongo03:

  28. mkdir -p /usr/local/mongodb/conf

  29. mkdir -p /var/log/nginx/mongodb

  30. mkdir -p /mnt/mongodata/shard1

  31. mkdir -p /mnt/mongodata/shard2

  32. mkdir -p /mnt/mongodata/shard3

  33.  
  34. #mongo04:

  35. mkdir -p /usr/local/mongodb/conf

  36. mkdir -p /var/log/nginx/mongodb

  37. mkdir -p /mnt/mongodata/shard2

  38. mkdir -p /mnt/mongodata/shard3

  39. mkdir -p /mnt/mongodata/shard4

  40.  
  41. #mongo05:

  42. mkdir -p /usr/local/mongodb/conf

  43. mkdir -p /var/log/nginx/mongodb

  44. mkdir -p /mnt/mongodata/shard3

  45. mkdir -p /mnt/mongodata/shard4

  46. mkdir -p /mnt/mongodata/shard5

  47.  
  48. #mongo06:

  49. mkdir -p /usr/local/mongodb/conf

  50. mkdir -p /var/log/nginx/mongodb

  51. mkdir -p /mnt/mongodata/shard4

  52. mkdir -p /mnt/mongodata/shard5

  53. mkdir -p /mnt/mongodata/shard6

  54.  
  55. #mongo07:

  56. mkdir -p /usr/local/mongodb/conf

  57. mkdir -p /var/log/nginx/mongodb

  58. mkdir -p /mnt/mongodata/shard5

  59. mkdir -p /mnt/mongodata/shard6

  60. mkdir -p /mnt/mongodata/shard7

  61. mkdir -p /mnt/mongodata/config

  62.  
  63. #mongo08:

  64. mkdir -p /usr/local/mongodb/conf

  65. mkdir -p /var/log/nginx/mongodb

  66. mkdir -p /mnt/mongodata/shard6

  67. mkdir -p /mnt/mongodata/shard7

  68. mkdir -p /mnt/mongodata/shard8

  69. mkdir -p /mnt/mongodata/config

  70.  
  71. #mongo09:

  72. mkdir -p /usr/local/mongodb/conf

  73. mkdir -p /var/log/nginx/mongodb

  74. mkdir -p /mnt/mongodata/shard7

  75. mkdir -p /mnt/mongodata/shard8

  76. mkdir -p /mnt/mongodata/shard9

  77. mkdir -p /mnt/mongodata/config

  78.  
  1. 环境变量配置
 
  1. vim /etc/profile

  2.  
  3. #加入如下内容

  4. export MONGODB_HOME=/usr/local/mongodb

  5. export PATH=$MONGODB_HOME/bin:$PATH

  6.  
  7. #当即生效

  8. source /etc/profile

使用命令mongod -v输出信息版本信息验证环境变量是否配置成功

集群配置

一、config server配置服务器(副本集)

根据服务器规划,在mongo07-mongo09上部署三台config server副本集,在该三台服务器上分别添加如下配置文件:

 
  1. vim /usr/local/mongodb/conf/config.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/config.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/config

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/configsrv.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 21000

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: configs

  29.  
  30. sharding:

  31. clusterRole: configsvr

  32.  

启动这三台服务器的config server

mongod -f /usr/local/mongodb/conf/config.conf

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

 
  1. mongo --port 21000

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致)

  4. config = {

  5. ... _id : "configs",

  6. ... members : [

  7. ... {_id : 0, host : "mongo07:21000" },

  8. ... {_id : 1, host : "mongo08:21000" },

  9. ... {_id : 2, host : "mongo09:21000" }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

二、shard server分片服务器(副本集)

配置shard0副本集

在mongo00、mongo0一、mongo02服务器上作如下配置:

 
  1. vim /usr/local/mongodb/conf/shard0.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/shard0.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/shard0

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/shard0.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 27000

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: shard0

  29.  
  30. sharding:

  31. clusterRole: shardsvr

启动这三台服务器的shard0 server

mongod -f /usr/local/mongodb/conf/shard0.conf

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

 
  1. mongo --port 27000

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致,priority表明权重[1,100],大的被分配为主服务器,0永久不会变为主服务器)

  4. config = {

  5. ... _id : "shard0",

  6. ... members : [

  7. ... {_id : 0, host : "mongo00:27000", priority : 2 },

  8. ... {_id : 1, host : "mongo01:27000", priority : 1 },

  9. ... {_id : 2, host : "mongo02:27000", arbiterOnly :true }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

配置shard1副本集

在mongo0一、mongo0二、mongo03服务器上作如下配置:

 
  1. vim /usr/local/mongodb/conf/shard1.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/shard1.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/shard1

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/shard1.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 27001

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: shard1

  29.  
  30. sharding:

  31. clusterRole: shardsvr

启动这三台服务器的shard1 server

mongod -f /usr/local/mongodb/conf/shard1.conf

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

 
  1. mongo --port 27001

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致,priority表明权重[1,100],大的被分配为主服务器,0永久不会变为主服务器)

  4. config = {

  5. ... _id : "shard1",

  6. ... members : [

  7. ... {_id : 0, host : "mongo01:27001", priority : 2 },

  8. ... {_id : 1, host : "mongo02:27001", priority : 1 },

  9. ... {_id : 2, host : "mongo03:27001", arbiterOnly :true }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

配置shard2副本集

在mongo0二、mongo0三、mongo04服务器上作如下配置:

 
  1. vim /usr/local/mongodb/conf/shard2.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/shard2.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/shard2

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/shard2.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 27002

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: shard2

  29.  
  30. sharding:

  31. clusterRole: shardsvr

启动这三台服务器的shard2 server

mongod -f /usr/local/mongodb/conf/shard2.conf

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

 
  1. mongo --port 27002

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致,priority表明权重[1,100],大的被分配为主服务器,0永久不会变为主服务器)

  4. config = {

  5. ... _id : "shard2",

  6. ... members : [

  7. ... {_id : 0, host : "mongo02:27002", priority : 2 },

  8. ... {_id : 1, host : "mongo03:27002", priority : 1 },

  9. ... {_id : 2, host : "mongo04:27002", arbiterOnly :true }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

配置shard3副本集

在mongo0三、mongo0四、mongo05服务器上作如下配置:

 
  1. vim /usr/local/mongodb/conf/shard3.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/shard3.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/shard3

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/shard3.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 27003

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: shard3

  29.  
  30. sharding:

  31. clusterRole: shardsvr

启动这三台服务器的shard3 server

mongod -f /usr/local/mongodb/conf/shard3.conf

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

 
  1. mongo --port 27003

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致,priority表明权重[1,100],大的被分配为主服务器,0永久不会变为主服务器)

  4. config = {

  5. ... _id : "shard3",

  6. ... members : [

  7. ... {_id : 0, host : "mongo03:27003", priority : 2 },

  8. ... {_id : 1, host : "mongo04:27003", priority : 1 },

  9. ... {_id : 2, host : "mongo05:27003", arbiterOnly :true }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

配置shard4副本集

在mongo0四、mongo0五、mongo06服务器上作如下配置:

 
  1. vim /usr/local/mongodb/conf/shard4.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/shard4.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/shard4

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/shard4.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 27004

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: shard4

  29.  
  30. sharding:

  31. clusterRole: shardsvr

启动这三台服务器的shard4 server

mongod -f /usr/local/mongodb/conf/shard4.conf

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

 
  1. mongo --port 27004

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致,priority表明权重[1,100],大的被分配为主服务器,0永久不会变为主服务器)

  4. config = {

  5. ... _id : "shard4",

  6. ... members : [

  7. ... {_id : 0, host : "mongo04:27004", priority : 2 },

  8. ... {_id : 1, host : "mongo05:27004", priority : 1 },

  9. ... {_id : 2, host : "mongo06:27004", arbiterOnly :true }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

配置shard5副本集

在mongo0五、mongo0六、mongo07服务器上作如下配置:

 
  1. vim /usr/local/mongodb/conf/shard5.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/shard5.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/shard5

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/shard5.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 27005

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: shard5

  29.  
  30. sharding:

  31. clusterRole: shardsvr

启动这三台服务器的shard5 server

mongod -f /usr/local/mongodb/conf/shard5.conf

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

 
  1. mongo --port 27005

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致,priority表明权重[1,100],大的被分配为主服务器,0永久不会变为主服务器)

  4. config = {

  5. ... _id : "shard5",

  6. ... members : [

  7. ... {_id : 0, host : "mongo05:27005", priority : 2 },

  8. ... {_id : 1, host : "mongo06:27005", priority : 1 },

  9. ... {_id : 2, host : "mongo07:27005", arbiterOnly :true }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

配置shard6副本集

在mongo0六、mongo0七、mongo08服务器上作如下配置:

 
  1. vim /usr/local/mongodb/conf/shard6.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/shard6.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/shard6

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/shard6.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 27006

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: shard6

  29.  
  30. sharding:

  31. clusterRole: shardsvr

启动这三台服务器的shard6 server

mongod -f /usr/local/mongodb/conf/shard6.conf

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

 
  1. mongo --port 27006

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致,priority表明权重[1,100],大的被分配为主服务器,0永久不会变为主服务器)

  4. config = {

  5. ... _id : "shard6",

  6. ... members : [

  7. ... {_id : 0, host : "mongo06:27006", priority : 2 },

  8. ... {_id : 1, host : "mongo07:27006", priority : 1 },

  9. ... {_id : 2, host : "mongo08:27006", arbiterOnly :true }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

配置shard7副本集

在mongo0七、mongo0八、mongo09服务器上作如下配置:

 
  1. vim /usr/local/mongodb/conf/shard7.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/shard7.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/shard7

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/shard7.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 27007

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: shard7

  29.  
  30. sharding:

  31. clusterRole: shardsvr

启动这三台服务器的shard7 server

mongod -f /usr/local/mongodb/conf/shard7.conf

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

 
  1. mongo --port 27007

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致,priority表明权重[1,100],大的被分配为主服务器,0永久不会变为主服务器)

  4. config = {

  5. ... _id : "shard7",

  6. ... members : [

  7. ... {_id : 0, host : "mongo07:27007", priority : 2 },

  8. ... {_id : 1, host : "mongo08:27007", priority : 1 },

  9. ... {_id : 2, host : "mongo09:27007", arbiterOnly :true }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

配置shard8副本集

在mongo0八、mongo0九、mongo00服务器上作如下配置:

 
  1. vim /usr/local/mongodb/conf/shard8.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/shard8.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/shard8

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/shard8.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 27008

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: shard8

  29.  
  30. sharding:

  31. clusterRole: shardsvr

启动这三台服务器的shard8 server

mongod -f /usr/local/mongodb/conf/shard8.conf

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

 
  1. mongo --port 27008

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致,priority表明权重[1,100],大的被分配为主服务器,0永久不会变为主服务器)

  4. config = {

  5. ... _id : "shard8",

  6. ... members : [

  7. ... {_id : 0, host : "mongo08:27008", priority : 2 },

  8. ... {_id : 1, host : "mongo09:27008", priority : 1 },

  9. ... {_id : 2, host : "mongo00:27008", arbiterOnly :true }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

配置shard9副本集

在mongo0九、mongo00、mongo01服务器上作如下配置:

 
  1. vim /usr/local/mongodb/conf/shard9.conf

  2.  
  3.  
  4. # where to write logging data.

  5. systemLog:

  6. destination: file

  7. logAppend: true

  8. path: /var/log/nginx/mongodb/shard9.log

  9.  
  10. # Where and how to store data.

  11. storage:

  12. dbPath: /mnt/mongodata/shard9

  13. journal:

  14. enabled: true

  15.  
  16. # how the process runs

  17. processManagement:

  18. fork: true

  19. pidFilePath: /var/log/nginx/mongodb/shard9.pid

  20.  
  21. # network interfaces

  22. net:

  23. port: 27009

  24. bindIp: 0.0.0.0

  25.  
  26. #operationProfiling:

  27. replication:

  28. replSetName: shard9

  29.  
  30. sharding:

  31. clusterRole: shardsvr

启动这三台服务器的shard9 server

mongod -f /usr/local/mongodb/conf/shard9.conf

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

 
  1. mongo --port 27009

  2.  
  3. #定义副本集配置(键“_id”对应的值必须与配置文件中的replicaction.replSetName一致,priority表明权重[1,100],大的被分配为主服务器,0永久不会变为主服务器)

  4. config = {

  5. ... _id : "shard9",

  6. ... members : [

  7. ... {_id : 0, host : "mongo09:27009", priority : 2 },

  8. ... {_id : 1, host : "mongo00:27009", priority : 1 },

  9. ... {_id : 2, host : "mongo01:27009", arbiterOnly :true }

  10. ... ]

  11. ... }

  12.  
  13. #初始化副本集

  14. rs.initiate(config)

  15.  
  16. #查看分区状态

  17. rs.status();

三、mongos server路由服务器(在mongo00-mongo06上配置)

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

 
  1. vim /usr/local/mongodb/conf/mongos.conf

  2.  
  3. systemLog:

  4. destination: file

  5. logAppend: true

  6. path: /var/log/nginx/mongodbmongos.log

  7. processManagement:

  8. fork: true

  9. # pidFilePath: /var/log/nginx/mongodbmongos.pid

  10.  
  11. # network interfaces

  12. net:

  13. port: 20000

  14. bindIp: 0.0.0.0

  15. #监听的配置服务器,只能有1个或者3个 configs为配置服务器的副本集名字

  16. sharding:

  17. configDB: configs/mongo07:21000,mongo08:21000,mongo09:21000

启动这七台服务器的mongos server

mongod -f /usr/local/mongodb/conf/mongos.conf

四、启用分片

目前已经搭建好配置服务器、数据分片服务器、路由服务器,下面进行分片启用,使得app链接到路由服务器时可使用分片机制

登陆任意一台mongos

 
  1. mongo --port 20000

  2.  
  3. #使用admin数据库

  4. use admin

  5.  
  6. #串联路由服务器与分片副本集

  7. sh.addShard("shard0/mongo00:27000,mongo01:27000,mongo02:27000")

  8. sh.addShard("shard1/mongo01:27001,mongo02:27001,mongo03:27001")

  9. sh.addShard("shard2/mongo02:27002,mongo03:27002,mongo04:27002")

  10. sh.addShard("shard3/mongo03:27003,mongo04:27003,mongo05:27003")

  11. sh.addShard("shard4/mongo04:27004,mongo05:27004,mongo06:27004")

  12. sh.addShard("shard5/mongo05:27005,mongo06:27005,mongo07:27005")

  13. sh.addShard("shard6/mongo06:27006,mongo07:27006,mongo08:27006")

  14. sh.addShard("shard7/mongo07:27007,mongo08:27007,mongo09:27007")

  15. sh.addShard("shard8/mongo08:27008,mongo09:27008,mongo00:27008")

  16. sh.addShard("shard9/mongo09:27009,mongo00:27009,mongo01:27009")

  17.  
  18. #查看集群状态

  19. sh.status()

 

 

培训四个月的linux运维,能达到什么样的水平?

相关文章
相关标签/搜索