1.前置条件(官方推荐生产环境使用此种)
To avoid configuration updates due to IP address changes, use DNS hostnames instead of IP addresses. It is particularly important to use a DNS hostname instead of an IP address when configuring replica set members or sharded cluster members.
Use hostnames instead of IP addresses to configure clusters across a split network horizon. Starting in MongoDB 5.0, nodes that are only configured with an IP address will fail startup validation and will not start.
2.配置 Config Server
配置成副本集模式。config1,config2,config3
2.1创建文件夹
mkdir -p /data/dbs/config1/data
mkdir -p /data/dbs/config1/logs
2.2 vim /data/dbs/config1/mongod.conf
systemLog:
destination: file
logAppend: true
path: /data/dbs/config1/logs/mongod.log
storage:
dbPath: /data/dbs/config1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/dbs/config1/mongod.pid
net:
port: 27018
bindIp: 0.0.0.0
sharding:
clusterRole: configsvr
replication:
replSetName: config
2.3启动
/home/mongodb/mongodb/bin/mongod -f /data/dbs/config1/mongod.conf
/home/mongodb/mongodb/bin/mongod -f /data/dbs/config2/mongod.conf
/home/mongodb/mongodb/bin/mongod -f /data/dbs/config3/mongod.conf
2.4 初始化
/home/mongodb/mongodb/bin/mongo --port 27018
rs.initiate(
{
_id: "config",
configsvr: true,
members: [
{ _id : 0, host : "10.6.8.176:27018" },
{ _id : 1, host : "10.6.8.176:27028" },
{ _id : 2, host : "10.6.8.176:27038" }
]
}
)
3.配置Shard
配置成副本集模式。shard1,shard2,shard3
3.1创建文件夹
mkdir -p /data/dbs/shard1/data
mkdir -p /data/dbs/shard1/logs
3.2 vim /data/dbs/config1/mongod.conf
systemLog:
destination: file
logAppend: true
path: /data/dbs/shard1/logs/mongod.log
storage:
dbPath: /data/dbs/shard1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/dbs/shard1/mongod.pid
net:
port: 27019
bindIp: 0.0.0.0
sharding:
clusterRole: shardsvr
replication:
replSetName: shard1
3.3 启动
/home/mongodb/mongodb/bin/mongod -f /data/dbs/shard1/mongod.conf
/home/mongodb/mongodb/bin/mongod -f /data/dbs/shard2/mongod.conf
/home/mongodb/mongodb/bin/mongod -f /data/dbs/shard3/mongod.conf
3.4 初始化
shard1
/home/mongodb/mongodb/bin/mongo --port 27019
rs.initiate(
{
_id: "shard1",
members: [
{ _id : 0, host : "10.6.8.176:27019" }
]
}
)
shard2
/home/mongodb/mongodb/bin/mongo --port 27020
rs.initiate(
{
_id: "shard2",
members: [
{ _id : 0, host : "10.6.8.176:27020" }
]
}
)
shard3
/home/mongodb/mongodb/bin/mongo --port 27021
rs.initiate(
{
_id: "shard3",
members: [
{ _id : 0, host : "10.6.8.176:27021" }
]
}
)
4. 配置mongos-router
配置成副本集模式或者单个都可以。
4.1 创建文件夹
mkdir -p /data/dbs/router1/data
mkdir -p /data/dbs/router1/logs
4.2 vim /data/dbs/router1/mongod.conf
systemLog:
destination: file
logAppend: true
path: /data/dbs/router1/logs/mongod.log
storage:
dbPath: /data/dbs/router1/data
journal:
enabled: true
processManagement:
fork: true
pidFilePath: /data/dbs/router1/mongod.pid
net:
port: 27017
bindIp: 0.0.0.0
sharding:
configDB: config/10.6.8.186:27018
4.3 启动
/home/mongodb/mongodb/bin/mongos -f /data/dbs/router1/mongod.conf
4.4 初始化
/home/mongodb/mongodb/bin/mongo --port 27017
sh.addShard( "shard1/10.6.8.176:27019")
sh.addShard( "shard2/10.6.8.176:27020")
sh.addShard( "shard3/10.6.8.176:27021")
4.5 删除分片
db.runCommand( { removeShard: "shard1" } )
db.adminCommand( { removeShard : "shard1" } )
5.验证分片
/home/mongodb/mongodb/bin/mongo --port 27017
5.1为集合启用分片,设置分片规则
use db0001;
sh.enableSharding("db0001")
sh.shardCollection("db0001.student", {"_id": "hashed" })
5.2插入数据
for (i = 1; i <= 1000; i=i+1){
db.student.insert({'xh': i,'name':'name'+i})
}
5.3查看各分片数量
/home/mongodb/mongodb/bin/mongo --port 27029
/home/mongodb/mongodb/bin/mongo --port 27020
/home/mongodb/mongodb/bin/mongo --port 27021
use db0001;
db.student.find().count()