节点规划:
mongos:
28015、28016、28017


configserver:
28018、28019、28020(复制集)


mongod:
28021、28022、28023(复制集)

28024、28025、28026(复制集)



创建目录
for i in 15 16 17 18 19 20 21 22 23 24 25 26
do
mkdir -p /mongodb/280$i/conf
mkdir -p /mongodb/280$i/data
mkdir -p /mongodb/280$i/log
done


编辑shard集群配置文件

cat > /mongodb/28021/conf/mongod.conf <<'EOF'
systemLog:
destination: file
path: /mongodb/28021/log/mongodb.log
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/28021/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 127.0.0.1
port: 28021
replication:
oplogSizeMB: 2048
replSetName: sh1
sharding:
clusterRole: shardsvr
processManagement:
fork: true
EOF


复制shard集群配置文件
for i in 22 23 24 25 26
do
\cp /mongodb/28021/conf/mongod.conf /mongodb/280$i/conf/
done


修改配置文件端口
for i in 22 23 24 25 26
do
sed -i "s#28021#280$i#g" /mongodb/280$i/conf/mongod.conf
done


修改配置文件复制集名称(replSetName)
for i in 24 25 26
do
sed -i "s#sh1#sh2#g" /mongodb/280$i/conf/mongod.conf
done


启动shard集群
for i in 21 22 23 24 25 26
do
mongod -f /mongodb/280$i/conf/mongod.conf
done


配置复制集
mongo --host 127.0.0.1 --port 28021 admin

config = {_id: 'sh1', members: [
{_id: 0, host: '127.0.0.1:28021'},
{_id: 1, host: '127.0.0.1:28022'},
{_id: 2, host: '127.0.0.1:28023'}]
}

rs.initiate(config)


配置复制集
mongo --host 127.0.0.1 --port 28024 admin

config = {_id: 'sh2', members: [
{_id: 0, host: '127.0.0.1:28024'},
{_id: 1, host: '127.0.0.1:28025'},
{_id: 2, host: '127.0.0.1:28026'}]
}

rs.initiate(config)


config集群配置

创建主节点配置文件

cat > /mongodb/28018/conf/mongod.conf <<'EOF'
systemLog:
destination: file
path: /mongodb/28018/log/mongodb.conf
logAppend: true
storage:
journal:
enabled: true
dbPath: /mongodb/28018/data
directoryPerDB: true
#engine: wiredTiger
wiredTiger:
engineConfig:
cacheSizeGB: 1
directoryForIndexes: true
collectionConfig:
blockCompressor: zlib
indexConfig:
prefixCompression: true
net:
bindIp: 127.0.0.1
port: 28018
replication:
oplogSizeMB: 2048
replSetName: configReplSet
sharding:
clusterRole: configsvr
processManagement:
fork: true
EOF


将配置文件分发到从节点
for i in 19 20
do
\cp /mongodb/28018/conf/mongod.conf /mongodb/280$i/conf/
done


修改配置文件端口信息
for i in 19 20
do
sed -i "s#28018#280$i#g" /mongodb/280$i/conf/mongod.conf
done


启动configserver集群
for i in 18 19 20
do
mongod -f /mongodb/280$i/conf/mongod.conf
done


配置configserver复制集
mongo --host 127.0.0.1 --port 28018 admin


配置复制集信息
config = {_id: 'configReplSet', members: [
{_id: 0, host: '127.0.0.1:28018'},
{_id: 1, host: '127.0.0.1:28019'},
{_id: 2, host: '127.0.0.1:28020'}]
}


rs.initiate(config)




mongos节点配置

创建配置文件
cat > /mongodb/28017/conf/mongos.conf <<'EOF'
systemLog:
destination: file
path: /mongodb/28017/log/mongos.log
logAppend: true
net:
bindIp: 127.0.0.1
port: 28017
sharding:
configDB: configReplSet/127.0.0.1:28108,127.0.0.1:28019,127.0.0.1:28020
processManagement:
fork: true
EOF


复制配置文件
for i in 15 16
do
\cp /mongodb/28017/conf/mongos.conf /mongodb/280$i/conf/
done


修改配置文件端口信息
for i in 15 16
do
sed -i "s#28017#280$i#g" /mongodb/280$i/conf/mongos.conf
done


启动mongos
for i in 15 16 17
do
mongos -f /mongodb/280$i/conf/mongos.conf
done


登陆到mongos
mongo 127.0.0.1:28017/admin


添加分片节点
db.runCommand( { addshard : "sh1/127.0.0.1:28021,127.0.0.1:28022,127.0.0.1:28023",name:"shard1"} )
db.runCommand( { addshard : "sh2/127.0.0.1:28024,127.0.0.1:28025,127.0.0.1:28026",name:"shard2"} )


列出分片
mongos> use admin
mongos> db.runCommand( { listshards : 1 } )
{
"shards" : [
{
"_id" : "shard1",
"host" : "sh1/127.0.0.1:28021,127.0.0.1:28022,127.0.0.1:28023",
"state" : 1
},
{
"_id" : "shard2",
"host" : "sh2/127.0.0.1:28024,127.0.0.1:28025,127.0.0.1:28026",
"state" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1600679934, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1600679934, 1),
"signature" : {
"hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
"keyId" : NumberLong(0)
}
}
}

或者:
mongos> use config
mongos> db.shards.find()
{ "_id" : "shard1", "host" : "sh1/127.0.0.1:28021,127.0.0.1:28022,127.0.0.1:28023", "state" : 1 }
{ "_id" : "shard2", "host" : "sh2/127.0.0.1:28024,127.0.0.1:28025,127.0.0.1:28026", "state" : 1 }



整体状态查看
mongos> sh.status()