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運維,能達到什麼樣的水平?

相關文章
相關標籤/搜索