准备

image.png

IP端口规划

  1. db01:
  2. shard1_master db01:28100
  3. shard3_slave db03:28200
  4. shard2_arbiter db02:28300
  5. congfig server db01:40000
  6. mongos server db01:60000
  7. db02:
  8. shard2_master db02:28100
  9. shard1_slave db01:28200
  10. shard3_arbiter db03:28300
  11. congfig server db02:40000
  12. mongos server db02:60000
  13. db03:
  14. shard3_master db03:28100
  15. shard2_slave db02:28200
  16. shard1_arbiter db01:28300
  17. congfig server db03:40000
  18. mongos server db03:60000

目录规划

  1. #服务目录
  2. /opt/master/{conf,log,pid}
  3. /opt/slave/{conf,log,pid}
  4. /opt/arbiter/{conf,log,pid}
  5. /data/config/{conf,log,pid}
  6. /data/mongos/{conf,log,pid}
  7. #数据目录
  8. /data/master/
  9. /data/slave/
  10. /data/arbiter/
  11. /data/config/

分片集群搭建

1.搭建副本集

配置文件

DB01:

  1. #MASTER配置
  2. cat >/opt/master/conf/mongod.conf <<EOF
  3. systemLog:
  4. destination: file
  5. logAppend: true
  6. path: /opt/master/log/mongodb.log
  7. storage:
  8. journal:
  9. enabled: true
  10. dbPath: /data/master/
  11. directoryPerDB: true
  12. wiredTiger:
  13. engineConfig:
  14. cacheSizeGB: 0.5
  15. directoryForIndexes: true
  16. collectionConfig:
  17. blockCompressor: zlib
  18. indexConfig:
  19. prefixCompression: true
  20. processManagement:
  21. fork: true
  22. pidFilePath: /opt/master/pid/mongodb.pid
  23. timeZoneInfo: /usr/share/zoneinfo
  24. net:
  25. port: 28100
  26. bindIp: 127.0.0.1,10.0.0.51
  27. replication:
  28. oplogSizeMB: 1024
  29. replSetName: shard1
  30. sharding:
  31. clusterRole: shardsvr
  32. EOF
  33. #SLAVE配置
  34. cat >/opt/slave/conf/mongod.conf<<EOF
  35. systemLog:
  36. destination: file
  37. logAppend: true
  38. path: /opt/slave/log/mongodb.log
  39. storage:
  40. journal:
  41. enabled: true
  42. dbPath: /data/slave/
  43. directoryPerDB: true
  44. wiredTiger:
  45. engineConfig:
  46. cacheSizeGB: 0.5
  47. directoryForIndexes: true
  48. collectionConfig:
  49. blockCompressor: zlib
  50. indexConfig:
  51. prefixCompression: true
  52. processManagement:
  53. fork: true
  54. pidFilePath: /opt/slave/pid/mongodb.pid
  55. timeZoneInfo: /usr/share/zoneinfo
  56. net:
  57. port: 28200
  58. bindIp: 127.0.0.1,10.0.0.51
  59. replication:
  60. oplogSizeMB: 1024
  61. replSetName: shard3
  62. sharding:
  63. clusterRole: shardsvr
  64. EOF
  65. #Arbiter配置
  66. cat >/opt/arbiter/conf/mongod.conf<<EOF
  67. systemLog:
  68. destination: file
  69. logAppend: true
  70. path: /opt/arbiter/log/mongodb.log
  71. storage:
  72. journal:
  73. enabled: true
  74. dbPath: /data/arbiter/
  75. directoryPerDB: true
  76. wiredTiger:
  77. engineConfig:
  78. cacheSizeGB: 0.5
  79. directoryForIndexes: true
  80. collectionConfig:
  81. blockCompressor: zlib
  82. indexConfig:
  83. prefixCompression: true
  84. processManagement:
  85. fork: true
  86. pidFilePath: /opt/arbiter/pid/mongodb.pid
  87. timeZoneInfo: /usr/share/zoneinfo
  88. net:
  89. port: 28300
  90. bindIp: 127.0.0.1,10.0.0.51
  91. replication:
  92. oplogSizeMB: 1024
  93. replSetName: shard2
  94. sharding:
  95. clusterRole: shardsvr
  96. EOF

DB02:

#MASTER配置文件:
cat >/opt/master/conf/mongod.conf<<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/master/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/master/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/master/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28100
  bindIp: 127.0.0.1,10.0.0.52

replication:
  oplogSizeMB: 1024 
  replSetName: shard2

sharding:
  clusterRole: shardsvr
EOF

#SLAVE配置文件:
cat >/opt/slave/conf/mongod.conf<<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/slave/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/slave/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/slave/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28200
  bindIp: 127.0.0.1,10.0.0.52

replication:
  oplogSizeMB: 1024 
  replSetName: shard1

sharding:
  clusterRole: shardsvr
EOF


#Arbiter配置文件
cat >/opt/arbiter/conf/mongod.conf<<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/arbiter/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/arbiter/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/arbiter/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28300
  bindIp: 127.0.0.1,10.0.0.52

replication:
  oplogSizeMB: 1024 
  replSetName: shard3

sharding:
  clusterRole: shardsvr
EOF

DB03:

#MASTER配置文件:
cat >/opt/master/conf/mongod.conf<<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/master/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/master/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/master/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28100
  bindIp: 127.0.0.1,10.0.0.53

replication:
  oplogSizeMB: 1024 
  replSetName: shard3

sharding:
  clusterRole: shardsvr
EOF

#SLAVE配置文件:
cat >/opt/slave/conf/mongod.conf<<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/slave/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/slave/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/slave/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28200
  bindIp: 127.0.0.1,10.0.0.53

replication:
  oplogSizeMB: 1024 
  replSetName: shard2

sharding:
  clusterRole: shardsvr
EOF


#Arbiter配置文件
cat >/opt/arbiter/conf/mongod.conf<<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/arbiter/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/arbiter/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/arbiter/pid/mongodb.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 28300
  bindIp: 127.0.0.1,10.0.0.53

replication:
  oplogSizeMB: 1024 
  replSetName: shard1

sharding:
  clusterRole: shardsvr
EOF

启动

mongod -f /opt/master/conf/mongod.conf 
mongod -f /opt/slave/conf/mongod.conf 
mongod -f /opt/arbiter/conf/mongod.conf

#检查
netstat -lntup|grep mongod

初始化副本集

#db01节点创建shard1副本集:
mongo --port 28100
rs.initiate()
rs.add("10.0.0.52:28200")
rs.addArb("10.0.0.53:28300")

#db02节点创建shard2副本集:
mongo --port 28100
rs.initiate()
rs.add("10.0.0.53:28200")
rs.addArb("10.0.0.51:28300")

#db03节点创建shard3副本集:
mongo --port 28100
rs.initiate()
rs.add("10.0.0.51:28200")
rs.addArb("10.0.0.52:28300")

2.搭建config副本集

配置文件

DB01、DB02、DB03

cat >/opt/config/conf/mongod.conf<<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/config/log/mongodb.log

storage:
  journal:
    enabled: true
  dbPath: /data/config/
  directoryPerDB: true

  wiredTiger:
    engineConfig:
      cacheSizeGB: 0.5
      directoryForIndexes: true
    collectionConfig:
      blockCompressor: zlib
    indexConfig:
      prefixCompression: true

processManagement:
  fork: true
  pidFilePath: /opt/config/pid/mongod.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 40000
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

replication:
  replSetName: configset

sharding:
  clusterRole: configsvr
EOF

启动服务

mongod -f /opt/config/conf/mongod.conf

初始化config副本集

mongo --port 40000
rs.initiate()
rs.add("10.0.0.52:40000")
rs.add("10.0.0.53:40000")
rs.status()

3.搭建mongos副本集

配置文件

cat >/opt/mongos/conf/mongos.conf  <<EOF   
systemLog:
  destination: file 
  logAppend: true 
  path: /opt/mongos/log/mongos.log

processManagement:
  fork: true
  pidFilePath: /opt/mongos/pid/mongos.pid
  timeZoneInfo: /usr/share/zoneinfo

net:
  port: 60000
  bindIp: 127.0.0.1,$(ifconfig eth0|awk 'NR==2{print $2}')

sharding:
  configDB: 
    configset/10.0.0.51:40000,10.0.0.52:40000,10.0.0.53:40000
EOF

启动

mongos -f /opt/mongos/conf/mongos.conf

初始化

#登录到mongos
mongo --port 60000

#添加分片成员信息
use admin
db.runCommand({addShard:'shard1/10.0.0.51:28100,10.0.0.52:28200,10.0.0.53:28300'})
db.runCommand({addShard:'shard2/10.0.0.52:28100,10.0.0.53:28200,10.0.0.51:28300'})
db.runCommand({addShard:'shard3/10.0.0.53:28100,10.0.0.51:28200,10.0.0.52:28300'})

#查看分片信息
db.runCommand( { listshards : 1 } )

4.测试分片集群

#开启分片
mongo --port 60000
use admin
db.runCommand( { enablesharding : "database_name" } )

#创建索引
use database_name
db.hash_table.ensureIndex( {id: "hashed"} )

#集合开启哈希分片
use admin
sh.shardCollection( "database_name.hash_table", {id: "hashed" } )

#生成数据
use database_name
for(i=1;i<10000;i++){db.hash.insert({"id":i,"name":"rocket","age":i});}

#检查
shard1:
mongo db01:28100
use database_name
db.hash_table.count()
3349

shard2:
mongo db02:28100
use database_name
db.hash_table.count()
3366

shard3:
mongo db03:28100
use database_name
db.hash_table.count()
3284

分片集群常用管理命令

#1.列出分片所有详细信息
db.printShardingStatus()
sh.status()

#2.列出所有分片成员信息
use admin
db.runCommand({ listshards : 1})

#3.列出开启分片的数据库
use config
db.databases.find({"partitioned": true })

#4.查看分片的片键
use confige
db.collections.find().pretty()

#5.查看集合的分片信息
db.getCollection('range').getShardDistribution()
db.getCollection('hash_table').getShardDistribution()