一、该文档仅为了总结经验,方便下次部署时跳过踩过的坑。其中MongoDB的复制原理、特点等参照的菜鸟教程,附地址:https://www.runoob.com/mongodb/mongodb-replication.html
二、背景:4台服务器,dn11,dn22,dn33,dn44
1)dn11:主服务器;
2)dn22,dn33:从服务器,用来备份;
3)dn44:仲裁节点,用来当主服务挂掉,从服务器是偶数个时投票产生一个新的主服务器。
三、副本集搭建过程(直接在root下操作的):
在每一台上执行):
1)已添加服务的停止方式:
systemctl stop mongodb.service;
2)命令停止方式:
mongod --shutdown --config /etc/mongodb/mongodb.conf
2、修改配置文件(在每一台上执行):vim /etc/mongodb/mongodb.conf,修改后如下:
dbpath=/usr/local/mongodb/data
logpath=/usr/local/mongodb/logs/mongo.log
logappend=true
journal=true
quiet=true
port=27017
fork=true #后台运行
bind_ip=0.0.0.0 #允许任何IP进行连接
auth=true #是否授权连接
keyFile=/usr/local/mongodb/db/keyfile #keyFile
oplogSize=512 #oplog大小
replSet=wetest #集群名称
使用主从(这里不做介绍)或副本集时,如果需要开启安全认证方式登录,那么必须配置keyFile属性,并且每台服务器的keyFile必须相同
主服务器上执行),然后将keyfile拷贝到上面配置文件所在路径:
1)生成90个字节大小的keyfile命令:具体设置多大看你喜欢了
openssl rand -base64 90 > /usr/local/mongodb/db/keyfile;
2)更改keyfile权限:
chmod 600 keyfile
3)将keyfile拷贝到从服务器:
scp /usr/local/mongodb/db/keyfile root@dn22: /usr/local/mongodb/db/keyfile;
scp /usr/local/mongodb/db/keyfile root@dn33: /usr/local/mongodb/db/keyfile;
scp /usr/local/mongodb/db/keyfile root@dn44: /usr/local/mongodb/db/keyfile;
在每一台上执行):
1)已添加服务的启动方式:
systemctl start mongodb.service;
2)命令启动方式:
mongod -f /etc/mongodb/mongodb.conf
主服务器上执行):
1、执行mongo命令;
2、副本集初始化:
1)直接初始化:
> rs.initiate();
2)先配置,再初始化,该方式不需要再添加从节点
> rsConfig = { _id:"fleet", members:[{ _id:0, host:"dn22:27017" }, { _id:1, host:"dn33:27017" }] };
> rs.initiate(rsConfig);
初始化几秒过后> 变为wetest:PRIMARY> ,由于是总结性文档,我的截图并不连贯,变化过程如下面两图:
3、添加从节点:
wetest:PRIMARY> rs.add("dn22:27017");
wetest:PRIMARY> rs.add("dn33:27017");
添加完成后从服务器上> 变为wetest:SECONDARY>
4、添加仲裁节点:
wetest:PRIMARY> rs.addArb("dn44:27017");
添加完成后仲裁服务器上> 变为wetest:ARBITER>
如果副本集只有一主一从,该操作可以避免重启服务后主从关系调换(血的教训)
1)先将配置文件保存;
wetest:PRIMARY> cfg=rs.conf();
{
"_id" : "wetest",
"version" : 7,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "dn11:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "dn22:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "dn33:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "dn44:27017",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5ee9e542473bf3ef79612476")
}
}
2)设置优先级:
wetest:PRIMARY> cfg.members[0].priority = 2;
wetest:PRIMARY> cfg.members[0].priority = 1;
wetest:PRIMARY> cfg.members[0].priority = 1;
wetest:PRIMARY> cfg.members[0].priority = 0;
3)重新加载配置项:
wetest:PRIMARY> rs.reconfig(cfg);
4)查看配置是否生效:
wetest:PRIMARY> cfg=rs.conf();
{
"_id" : "wetest",
"version" : 7,
"protocolVersion" : NumberLong(1),
"writeConcernMajorityJournalDefault" : true,
"members" : [
{
"_id" : 0,
"host" : "dn11:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 2,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 1,
"host" : "dn22:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 2,
"host" : "dn33:27017",
"arbiterOnly" : false,
"buildIndexes" : true,
"hidden" : false,
"priority" : 1,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
},
{
"_id" : 3,
"host" : "dn44:27017",
"arbiterOnly" : true,
"buildIndexes" : true,
"hidden" : false,
"priority" : 0,
"tags" : {
},
"slaveDelay" : NumberLong(0),
"votes" : 1
}
],
"settings" : {
"chainingAllowed" : true,
"heartbeatIntervalMillis" : 2000,
"heartbeatTimeoutSecs" : 10,
"electionTimeoutMillis" : 10000,
"catchUpTimeoutMillis" : -1,
"catchUpTakeoverDelayMillis" : 30000,
"getLastErrorModes" : {
},
"getLastErrorDefaults" : {
"w" : 1,
"wtimeout" : 0
},
"replicaSetId" : ObjectId("5ee9e542473bf3ef79612476")
}
}
可以看到主服务器的优先级变为了2。
5)接下来就是执行一些创建用户、数据库、表等操作,详细可以看菜鸟教程,有需要也可以在评论区留言。
6)在从服务器和仲裁服务器需要执行的操作:设置slave为true
wetest:ARBITER> rs.slaveOk();