MongoDB集群搭建步骤
架构:
路由:27017
配置集群:
17011,17013,17015
分片1:
37011,37013,37015,37017(仲裁
分片2:
47011,47013,47015,47017(仲裁
数据库lg_resume,账号lagou_gx、密码abc321,表lg_resume_datas
生成密钥
openssl rand -base64 756 > /data/mongo/testKeyFile.file
chmod 600 /data/mongo/testKeyFile.file
配置
分片1
#分片1-1
dbpath=/data/mongo/data/shard1/server1
bind_ip=0.0.0.0
port=37011
fork=true
logpath=/data/mongo/logs/shard1/server1.log
replSet=shard1
shardsvr=true
keyFile=/data/mongo/testKeyFile.file
auth=true
#分片1-2
dbpath=/data/mongo/data/shard1/server2
bind_ip=0.0.0.0
port=37013
fork=true
logpath=/data/mongo/logs/shard1/server2.log
replSet=shard1
shardsvr=true
keyFile=/data/mongo/testKeyFile.file
auth=true
#分片1-3
dbpath=/data/mongo/data/shard1/server3
bind_ip=0.0.0.0
port=37015
fork=true
logpath=/data/mongo/logs/shard1/server3.log
replSet=shard1
shardsvr=true
keyFile=/data/mongo/testKeyFile.file
auth=true
#分片1-4
dbpath=/data/mongo/data/shard1/server4
bind_ip=0.0.0.0
port=37017
fork=true
logpath=/data/mongo/logs/shard1/server4.log
replSet=shard1
shardsvr=true
keyFile=/data/mongo/testKeyFile.file
auth=true
分片2
分片2-1
dbpath=/data/mongo/data/shard2/server1
bind_ip=0.0.0.0
port=47011
fork=true
logpath=/data/mongo/logs/shard2/server1.log
replSet=shard2
shardsvr=true
keyFile=/data/mongo/testKeyFile.file
auth=true
#分片2-2
dbpath=/data/mongo/data/shard2/server2
bind_ip=0.0.0.0
port=47013
fork=true
logpath=/data/mongo/logs/shard2/server2.log
replSet=shard2
shardsvr=true
keyFile=/data/mongo/testKeyFile.file
auth=true
#分片2-3
dbpath=/data/mongo/data/shard2/server3
bind_ip=0.0.0.0
port=47015
fork=true
logpath=/data/mongo/logs/shard2/server3.log
replSet=shard2
shardsvr=true
keyFile=/data/mongo/testKeyFile.file
auth=true
#分片2-4
dbpath=/data/mongo/data/shard2/server4
bind_ip=0.0.0.0
port=47017
fork=true
logpath=/data/mongo/logs/shard2/server4.log
replSet=shard2
shardsvr=true
keyFile=/data/mongo/testKeyFile.file
auth=true
配置服务器(1-3个)
配置1
dbpath=/data/mongo/logs/config/config1
logpath=/data/mongo/logs/config/config1.log
logappend=true
fork = true
bind_ip=0.0.0.0
port = 17011
configsvr=true
replSet=configsvr
keyFile=/data/mongo/testKeyFile.file
auth=true
配置2
dbpath=/data/mongo/logs/config/config2
logpath=/data/mongo/logs/config/config2.log
logappend=true
fork = true
bind_ip=0.0.0.0
port = 17013
configsvr=true
replSet=configsvr
keyFile=/data/mongo/testKeyFile.file
auth=true
配置3
dbpath=/data/mongo/logs/config/config3
logpath=/data/mongo/logs/config/config3.log
logappend=true
fork = true
bind_ip=0.0.0.0
port = 17015
configsvr=true
replSet=configsvr
keyFile=/data/mongo/testKeyFile.file
auth=true
路由
port=27017
bind_ip=0.0.0.0
fork=true
logpath=/data/mongo/logs/route/route.log
configdb=configsvr/192.168.1.8:17011,192.168.1.8:17013,192.168.1.8:17015
keyFile=/data/mongo/testKeyFile.file
批量启动
./bin/mongod -f clusterconf/config1.conf
./bin/mongod -f clusterconf/config2.conf
./bin/mongod -f clusterconf/config3.conf
./bin/mongod -f clusterconf/server1-1.conf
./bin/mongod -f clusterconf/server1-2.conf
./bin/mongod -f clusterconf/server1-3.conf
./bin/mongod -f clusterconf/server1-4.conf
./bin/mongod -f clusterconf/server2-1.conf
./bin/mongod -f clusterconf/server2-2.conf
./bin/mongod -f clusterconf/server2-3.conf
./bin/mongod -f clusterconf/server2-4.conf
注意:初次登陆时候,先将后面步骤初始化完。然后再启动路由服务器
初始化
分片1, 37017为仲裁节点
./bin/mongo localhost:37011
var cfg ={"_id":"shard1",
"protocolVersion" : 1,
"members":[
{"_id":1,"host":"192.168.1.8:37011"},
{"_id":2,"host":"192.168.1.8:37013"},
{"_id":3,"host":"192.168.1.8:37015"},
{"_id":4,"host":"192.168.1.8:37017",arbiterOnly:true}
]
};
rs.initiate(cfg)
rs.status()
分片2
./bin/mongo localhost:47011
var cfg ={"_id":"shard2",
"protocolVersion" : 1,
"members":[
{"_id":1,"host":"192.168.1.8:47011"},
{"_id":2,"host":"192.168.1.8:47013"},
{"_id":3,"host":"192.168.1.8:47015"},
{"_id":4,"host":"192.168.1.8:47017",arbiterOnly:true}
]
};
rs.initiate(cfg)
rs.status()
配置服务器
./bin/mongo --port 17011
use admin
var cfg ={"_id":"configsvr",
"members":[
{"_id":1,"host":"192.168.1.8:17011"},
{"_id":2,"host":"192.168.1.8:17013"},
{"_id":3,"host":"192.168.1.8:17015"}]
};
rs.initiate(cfg)
rs.status()
初始化数据
分片
进入分片1和分片2的任一节点,初始化用户和库。分片2只创建库,不建集合,否则路由开启分片时会报
can't add shard 'shard2/192.168.1.8:47011,192.168.1.8:47013,192.168.1.8:47015' because a local database 'lg_resume' exists in another shard1
./bin/mongo localhost:37011
./bin/mongo localhost:47011
创建用户
use admin
db.createUser({
user:"root",
pwd:"root",
roles:[{role:"root",db:"admin"}]
})
db.auth("root","root")
use lg_resume
db.createUser({
user:"lagou_gx",
pwd:"abc123",
roles:[{role:"readWrite",db:"lg_resume"}]
})
进入从节点查看是否同步
use lg_resume
db.auth("lagou_gx","abc123")
默认从节点不能查看数据
rs.slaveOk()
show tables;
路由
启动路由
./bin/mongos -f clusterconf/route.conf
配置路由
./bin/mongo localhost:27017
use admin
db.createUser({
user:"root",
pwd:"root",
roles:[{role:"root",db:"admin"}]
})
db.auth("root","root")
sh.status()
sh.addShard("shard1/192.168.1.8:37011,192.168.1.8:37013,192.168.1.8:37015");
sh.addShard("shard2/192.168.1.8:47011,192.168.1.8:47013,192.168.1.8:47015");
sh.status()
为数据库开启分片功能
sh.enableSharding("lg_resume")
在route创建索引
创建索引为了效果明显可以使用hashed,如果用排序索引会把chunk占满后再分到下一个chunk
use lg_resume;
db.auth("lagou_gx","abc123")
mongos> db.lg_resume_datas.createIndex({"name":"hashed"})
{
"raw" : {
"shard2/192.168.1.8:47011,192.168.1.8:47013,192.168.1.8:47015" : {
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 2,
"note" : "all indexes already exist",
"ok" : 1
}
},
"ok" : 1,
"operationTime" : Timestamp(1611206499, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1611206504, 1),
"signature" : {
"hash" : BinData(0,"yC7rm9atsPltQq8qne7B5efG8+g="),
"keyId" : NumberLong("6920078296889688066")
}
}
}
此处会提示只在shard2上创建了索引,不影响后续分片。
如果直接在shard1或2上创建索引,后续在route上配置分片时会显示2个shard,1个chunk。插入数据会全部到有索引的服务器上。因此必须在route上创建索引
为指定集合开启分片功能
sh.shardCollection("lg_resume.lg_resume_datas",{"片键字段名如 name":索引说明})
sh.shardCollection("lg_resume.lg_resume_datas",{"name":"hashed"})
测试数据
use lg_resume;
db.createUser({
user:"lagou_gx",
pwd:"abc123",
roles:[{role:"readWrite",db:"lg_resume"}]
})
exit
use lg_resume;
db.auth("lagou_gx","abc123")
for(var i=1;i<= 100;i++){
db.lg_resume_datas.insert({"name":i,salary:(Math.random()*20000).toFixed(2)});
}
如果配置错误直接删除config数据重新启动配置即可
rm -rf /data/mongo/config/config1/*
rm -rf /data/mongo/config/config2/*
rm -rf /data/mongo/config/config3/*
参考
https://blog.51cto.com/13728740/2175637
https://blog.csdn.net/yyywyr/article/details/27238411
https://www.cnblogs.com/zping/p/11203545.html