准备
IP端口规划
db01:shard1_master db01:28100shard3_slave db03:28200shard2_arbiter db02:28300congfig server db01:40000mongos server db01:60000db02:shard2_master db02:28100shard1_slave db01:28200shard3_arbiter db03:28300congfig server db02:40000mongos server db02:60000db03:shard3_master db03:28100shard2_slave db02:28200shard1_arbiter db01:28300congfig server db03:40000mongos server db03:60000
目录规划
#服务目录/opt/master/{conf,log,pid}/opt/slave/{conf,log,pid}/opt/arbiter/{conf,log,pid}/data/config/{conf,log,pid}/data/mongos/{conf,log,pid}#数据目录/data/master//data/slave//data/arbiter//data/config/
分片集群搭建
1.搭建副本集
配置文件
DB01:
#MASTER配置cat >/opt/master/conf/mongod.conf <<EOFsystemLog:destination: filelogAppend: truepath: /opt/master/log/mongodb.logstorage:journal:enabled: truedbPath: /data/master/directoryPerDB: truewiredTiger:engineConfig:cacheSizeGB: 0.5directoryForIndexes: truecollectionConfig:blockCompressor: zlibindexConfig:prefixCompression: trueprocessManagement:fork: truepidFilePath: /opt/master/pid/mongodb.pidtimeZoneInfo: /usr/share/zoneinfonet:port: 28100bindIp: 127.0.0.1,10.0.0.51replication:oplogSizeMB: 1024replSetName: shard1sharding:clusterRole: shardsvrEOF#SLAVE配置cat >/opt/slave/conf/mongod.conf<<EOFsystemLog:destination: filelogAppend: truepath: /opt/slave/log/mongodb.logstorage:journal:enabled: truedbPath: /data/slave/directoryPerDB: truewiredTiger:engineConfig:cacheSizeGB: 0.5directoryForIndexes: truecollectionConfig:blockCompressor: zlibindexConfig:prefixCompression: trueprocessManagement:fork: truepidFilePath: /opt/slave/pid/mongodb.pidtimeZoneInfo: /usr/share/zoneinfonet:port: 28200bindIp: 127.0.0.1,10.0.0.51replication:oplogSizeMB: 1024replSetName: shard3sharding:clusterRole: shardsvrEOF#Arbiter配置cat >/opt/arbiter/conf/mongod.conf<<EOFsystemLog:destination: filelogAppend: truepath: /opt/arbiter/log/mongodb.logstorage:journal:enabled: truedbPath: /data/arbiter/directoryPerDB: truewiredTiger:engineConfig:cacheSizeGB: 0.5directoryForIndexes: truecollectionConfig:blockCompressor: zlibindexConfig:prefixCompression: trueprocessManagement:fork: truepidFilePath: /opt/arbiter/pid/mongodb.pidtimeZoneInfo: /usr/share/zoneinfonet:port: 28300bindIp: 127.0.0.1,10.0.0.51replication:oplogSizeMB: 1024replSetName: shard2sharding:clusterRole: shardsvrEOF
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()

