什么为什么 發表於 2019-6-6 11:04:00

006.MongoDB副本集

<h2 align="left">一 MongoDB 复制(副本集)</h2>
<h3 align="left">1.1 复制概述</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">MongoDB复制是将数据同步在多个服务器的过程。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">复制还允许从硬件故障和服务中断中恢复数据。</span></div>
<h3 align="left">1.2 复制意义</h3>
<div style="line-height: normal; -ms-word-break: normal">
<ul style="margin: 0; padding-left: 30pt">
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">保障数据的安全性</span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">数据高可用性 (24*7)</span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">灾难恢复</span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">无需停机维护(如备份,重建索引,压缩)</span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">分布式读取数据</span></li>
</ul>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">注意:副本集不是为了提高读性能存在的,在进行oplog的时候,读操作是被阻塞的;</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">提高读取性能应该使用分片和索引,它的存在更多是作为数据冗余,备份;</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">尤其当主库本来就面临着大量的写入压力,对于副本集的节点,也同样会面临写的压力。</span></div>
<h3 align="left">1.3 MongoDB复制原理</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">mongodb的复制至少需要两个节点。其中一个是主节点,负责处理客户端请求,其余的都是从节点,负责复制主节点上的数据。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">mongodb各个节点常见的搭配方式为:一主一从、一主多从。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">主节点记录在其上的所有操作oplog,从节点定期轮询主节点获取这些操作,然后对自己的数据副本执行这些操作,从而保证从节点的数据与主节点一致。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">MongoDB复制结构图如下所示:</span></div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="500" height="409" title="002" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="002" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110401318-904600078.png" border="0"></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">添加Heartbeat监控副本之间的心跳架构:</span></div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="700" height="265" title="003" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="003" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110402543-1064700797.png" border="0"></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">设置一个仲裁节点架构:</span></div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="700" height="265" title="004" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="004" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110403674-23070241.png" border="0"></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>说明:客户端从主节点读取数据,在客户端写入数据到主节点时, 主节点与从节点进行数据交互保障数据的一致性。</strong></span></div>
<h3 align="left">1.4 MongoDB复制过程</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">Primary节点写入数据,Secondary通过读取Primary的oplog得到复制信息,开始复制数据并且将复制信息写入到自己的oplog。如果某个操作失败,则备份节点停止从当前数据源复制数据。如果某个备份节点由于某些原因挂掉了,当重新启动后,就会自动从oplog的最后一个操作开始同步。同步完成后,将信息写入自己的oplog,由于复制操作是先复制数据,复制完成后再写入oplog,有可能相同的操作会同步两份,不过MongoDB在设计之初就考虑到这个问题,将oplog的同一个操作执行多次,与执行一次的效果是一样的。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">当Primary节点完成数据操作后,Secondary的数据同步过程如下:</span></div>
<ol>
<li>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">检查自己local库的oplog.rs集合找出最近的时间戳。</span></div>
</li>
<li><span style="font-family: 微软雅黑; font-size: small">检查Primary节点local库oplog.rs集合,找出大于此时间戳的记录。</span></li>
<li><span style="font-family: 微软雅黑; font-size: small">将找到的记录插入到自己的oplog.rs集合中,并执行这些操作。</span></li>
</ol>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small"> 副本集的同步和主从同步一样,都是异步同步的过程,不同的是副本集有个自动故障转移的功能。其原理是:slave端从primary端获取日志,然后在自己身上完全顺序的执行日志所记录的各种操作(该日志是不记录查询操作的),这个日志就是local数据库中的oplog.rs表,默认在64位机器上这个表是比较大的,占磁盘大小的5%,oplog.rs的大小可以在启动参数中设 定:--oplogSize 1000,单位是M。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>注意:在副本集的环境中,要是所有的Secondary都宕机了,只剩下Primary。最后Primary会变成Secondary,不能提供服务。</strong></span></div>
<h3 align="left">1.5 副本集</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">副本集特征:</span></div>
<div style="line-height: normal; -ms-word-break: normal">
<ul style="margin: 0; padding-left: 30pt">
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">N 个节点的集群;</span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">任何节点可作为主节点;</span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">所有写入操作都在主节点上;</span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">自动故障转移;</span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">自动恢复。</span></li>
</ul>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">集群中没有特定的主库,主库是选举产生,如果主库down了,会再选举出一台主库。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">mongoDB也可以配置成主从模式,但官方已经不建议使用主从模式了,替代方案是采用副本集的模式。</span></div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="500" height="379" title="005" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="005" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110404675-1519679301.png" border="0"></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">副本集有以下特点:</span></div>
<div style="line-height: normal; -ms-word-break: normal"><ol style="margin: 0; padding-left: 30pt">
<li style="text-align: left; line-height: 1.75; list-style-type: decimal; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">最小构成是:primary,secondary,arbiter,一般部署是:primary,2 secondary。</span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: decimal; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">成员数应该为奇数,如果为偶数的情况下添加arbiter,arbiter不保存数据,只投票。</span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: decimal; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="font-family: 微软雅黑; font-size: small">最大50 members,但是只能有 7 voting members,其他是non-voting members。</span></li>
</ol></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">注意:在副本集的环境中,如果所有的Secondary都宕机了,只剩下Primary。最后Primary会变成Secondary,且不能提供服务。</span></div>
<h2 align="left">二 前期准备</h2>
<ul>
<li>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">NTP同步;</span></div>
</li>
<li><span style="font-family: 微软雅黑; font-size: small">关闭SELinux;</span></li>
<li><span style="font-family: 微软雅黑; font-size: small">关闭防火墙或放通相应规则;</span></li>
<li><span style="font-family: 微软雅黑; font-size: small">所有节点安装MongoDB,参考《002.MongoDB社区版安装》;</span></li>
</ul>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">节点规划:</span></div>
<div style="line-height: normal; overflow: auto; -ms-word-break: normal">
<table style="overflow: visible; display: table; white-space: nowrap; border-collapse: collapse; table-layout: fixed; max-width: none; border-spacing: 2px 2px"><colgroup><col width="131"><col width="153"><col width="116"><col width="160"></colgroup>
<tbody>
<tr style="height: 40px">
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-0-0">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small"><strong>主机名</strong></span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-0-1">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small"><strong>IP</strong></span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-0-2">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small"><strong>类型</strong></span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-0-3">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small"><strong>备注</strong></span></div>
</td>
</tr>
<tr style="height: 40px">
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-1-0">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">mongodb01</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-1-1">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">172.24.8.71</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-1-2">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">primary</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-1-3">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">主节点</span></div>
</td>
</tr>
<tr style="height: 40px">
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-2-0">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">mongodb02</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-2-1">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">172.24.8.72</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-2-2">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">secondary</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-2-3">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">此节点</span></div>
</td>
</tr>
<tr style="height: 40px">
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-3-0">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">mongodb03</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-3-1">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">172.24.8.73</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-3-2">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">secondary</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-3-3">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">此节点</span></div>
</td>
</tr>
<tr style="height: 40px">
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-4-0">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">mongodb04</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-4-1">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">172.24.8.74</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-4-2">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">secondary</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-4-3">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">备节点(测试手动添加)</span></div>
</td>
</tr>
<tr style="height: 40px">
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-5-0">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">arbiter</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-5-1">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">172.24.8.75</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-5-2">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">arbiter</span></div>
</td>
<td align="center" style="padding: 1px; border: 1px solid rgba(167, 167, 167, 1); border-image: none; overflow: hidden; white-space: pre-wrap; -ms-word-wrap: break-word" data-cell-id="5964-1559030233718-cell-5-3">
<div class="table-cell-line"><span style="font-family: 微软雅黑; font-size: small">冲裁节点</span></div>
</td>
</tr>
</tbody>
</table>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">追加解析:</span></div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # vi /etc/hosts
<span style="color: rgba(0, 128, 128, 1)">2</span> 172.24.8.71mongodb01
<span style="color: rgba(0, 128, 128, 1)">3</span> 172.24.8.72mongodb02
<span style="color: rgba(0, 128, 128, 1)">4</span> 172.24.8.73mongodb03
<span style="color: rgba(0, 128, 128, 1)">5</span> 172.24.8.74mongodb04
<span style="color: rgba(0, 128, 128, 1)">6</span> 172.24.8.75arbite</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>提示:所有节点均建议添加以上解析。</strong></span></div>
<h2 align="left">三 副本集正式部署</h2>
<h3 align="left">3.1 开启远程连接及副本集</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # vi /etc/mongod.conf
<span style="color: rgba(0, 128, 128, 1)">2</span> ……
<span style="color: rgba(0, 128, 128, 1)">3</span> net:
<span style="color: rgba(0, 128, 128, 1)">4</span>   port: 27017
<span style="color: rgba(0, 128, 128, 1)">5</span>   bindIp: 172.24.8.71                                #根据不同节点配置
<span style="color: rgba(0, 128, 128, 1)">6</span> ……
<span style="color: rgba(0, 128, 128, 1)">7</span> replication:
<span style="color: rgba(0, 128, 128, 1)">8</span>   replSetName: my_rep                                #开启副本集,所有节点必须一致
<span style="color: rgba(0, 128, 128, 1)">9</span> ……
<span style="color: rgba(0, 128, 128, 1)"> 10</span> # systemctl start mongod.service
<span style="color: rgba(0, 128, 128, 1)"> 11</span> # systemctl enable mongod.service</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>注意:在完成复制集初始化、新建用户等操作之前必须保持security.authorization:enabled为注释状态。</strong></span></div>
<h3 align="left">3.2 配置复制级成员</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mongo --host 172.24.8.71
<span style="color: rgba(0, 128, 128, 1)">2</span> &gt; config = { _id: "my_rep", members: [
<span style="color: rgba(0, 128, 128, 1)">3</span> {_id: 0, host: "172.24.8.71:27017"},
<span style="color: rgba(0, 128, 128, 1)">4</span> {_id: 1, host: "172.24.8.72:27017"},
<span style="color: rgba(0, 128, 128, 1)">5</span> {_id: 2, host: "172.24.8.73:27017"}]
<span style="color: rgba(0, 128, 128, 1)">6</span> }</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">参数解释:</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">"_id": 副本集的名称</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">"members": 副本集的服务器列表</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">"_id": 服务器的唯一ID</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">"host": 服务器主机</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">"priority": 是优先级,默认为1,优先级0为被动节点,不能成为活跃节点。优先级不为0则按照有大到小选出活跃节点。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">"arbiterOnly": 仲裁节点,只参与投票,不接收数据,也不能成为活跃节点。</span></div>
<h3 align="left">3.3 初始化副本集</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> &gt; rs.initiate(config)
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep1:PRIMARY&gt; rs.status()                #查看集群状态</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">3.4 创建管理员用户</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mongo --host 172.24.8.71
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:PRIMARY&gt; use admin                                #进入admin数据库
<span style="color: rgba(0, 128, 128, 1)">3</span> my_rep:PRIMARY&gt; db.createUser({ user: "admin", pwd: "admin", roles: [{ role: "userAdminAnyDatabase", db: "admin" }] })
<span style="color: rgba(0, 128, 128, 1)">4</span> my_rep:PRIMARY&gt; db.auth("admin", "admin")                #验证创建结果
<span style="color: rgba(0, 128, 128, 1)">5</span> 1</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>提示:以上为建议项,建议创建一个管理员用于内部管理MongoDB。</strong></span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>mongodb中的用户是基于身份role的,该管理员账户的 role是 userAdminAnyDatabase。 ‘userAdmin’代表用户管理身份,’AnyDatabase’ 代表可以管理任何数据库。</strong></span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>db.auth()可以验证 用户。</strong></span></div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; db.getUsers()
<span style="color: rgba(0, 128, 128, 1)">2</span> &gt; db.system.users.find().pretty()                #查看全局所有账户</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="683" height="737" title="006" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="006" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110407386-748474824.png" border="0"></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>提示:mongo shell 可通过.pretty() 对输出进行JSON格式化,以便提高输出的可读性。</strong></span></div>
<h3 align="left">3.5 创建集群用户</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mongo --host 172.24.8.71 -u admin -p admin
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep1:PRIMARY&gt; use admin
<span style="color: rgba(0, 128, 128, 1)">3</span> switched to db admin
<span style="color: rgba(0, 128, 128, 1)">4</span> my_rep1:PRIMARY&gt; db.createUser({user:"clusteradmin",pwd:"clusteradmin",roles:[{role:"clusterAdmin",db:"admin"}]})</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>提示:3.2——3.5仅需在集群任何一台节点操作即可。</strong></span></div>
<h3 align="left">3.6 确认验证</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mongo --host 172.24.8.72 -u clusteradmin -p clusteradmin
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:SECONDARY&gt; rs.status()
<span style="color: rgba(0, 128, 128, 1)">3</span> my_rep:SECONDARY&gt; db.serverStatus().repl.primary        #查看主节点
<span style="color: rgba(0, 128, 128, 1)">4</span> 172.24.8.73:27017</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h2 align="left">四 集群开启权限</h2>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">建议使用keyfile访问控制的方式建立各个节点间的安全认证机制。</span></div>
<h3 align="left">4.1 配置key</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mkdir -p /usr/local/keyfile
<span style="color: rgba(0, 128, 128, 1)">2</span> # openssl rand -base64 100 &gt; /usr/local/keyfile/mongodb_keyfile
<span style="color: rgba(0, 128, 128, 1)">3</span> # chmod 600 /usr/local/keyfile/mongodb_keyfile
<span style="color: rgba(0, 128, 128, 1)">4</span> # chown -R mongod:mongod /usr/local/keyfile/
<span style="color: rgba(0, 128, 128, 1)">5</span> # scp -rp /usr/local/keyfile/ root@mongodb02:/usr/local/
<span style="color: rgba(0, 128, 128, 1)">6</span> # scp -rp /usr/local/keyfile/ root@mongodb03:/usr/local/
<span style="color: rgba(0, 128, 128, 1)">7</span> # scp -rp /usr/local/keyfile/ root@mongodb04:/usr/local/
<span style="color: rgba(0, 128, 128, 1)">8</span> # scp -rp /usr/local/keyfile/ root@arbiter:/usr/local/
<span style="color: rgba(0, 128, 128, 1)">9</span>
<span style="color: rgba(0, 128, 128, 1)"> 10</span> # chown -R mongod:mongod /usr/local/keyfile/
<span style="color: rgba(0, 128, 128, 1)"> 11</span> # chown -R mongod:mongod /usr/local/keyfile/
<span style="color: rgba(0, 128, 128, 1)"> 12</span> # chown -R mongod:mongod /usr/local/keyfile/
<span style="color: rgba(0, 128, 128, 1)"> 13</span> # chown -R mongod:mongod /usr/local/keyfile/</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">4.2 开启权限</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # vi /etc/mongod.conf
<span style="color: rgba(0, 128, 128, 1)">2</span> ……
<span style="color: rgba(0, 128, 128, 1)">3</span> security:                                        #取消注释
<span style="color: rgba(0, 128, 128, 1)">4</span>   authorization: enabled                        #开启验证
<span style="color: rgba(0, 128, 128, 1)">5</span>   keyFile: /usr/local/keyfile/mongodb_keyfile        #key文件
<span style="color: rgba(0, 128, 128, 1)">6</span> ……
<span style="color: rgba(0, 128, 128, 1)">7</span> # systemctl restart mongod.service</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>提示:需要在所有节点进行开启操作。</strong></span></div>
<h3 align="left">4.3 测试登陆</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mongo --host 172.24.8.71 -u clusteradmin -p clusteradmin
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:PRIMARY&gt; rs.status()
<span style="color: rgba(0, 128, 128, 1)">3</span> my_rep:PRIMARY&gt; db.serverStatus().repl.primary        #查看主节点
<span style="color: rgba(0, 128, 128, 1)">4</span> 172.24.8.71:27017</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h2 align="left">五 成员管理</h2>
<h3 align="left">5.1 SECONDARY节点增加</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # scp -rp /etc/mongod.conf root@mongodb04:/etc/mongod.conf
<span style="color: rgba(0, 128, 128, 1)">2</span> # vi /etc/mongod.conf                        #修改IP即可
<span style="color: rgba(0, 128, 128, 1)">3</span> net:
<span style="color: rgba(0, 128, 128, 1)">4</span>   port: 27017
<span style="color: rgba(0, 128, 128, 1)">5</span>   bindIp: 172.24.8.74
<span style="color: rgba(0, 128, 128, 1)">6</span> # systemctl restart mongod.service
<span style="color: rgba(0, 128, 128, 1)">7</span> # systemctl enable mongod.service
<span style="color: rgba(0, 128, 128, 1)">8</span> # mongo --host 172.24.8.71 -u clusteradmin -p clusteradmin
<span style="color: rgba(0, 128, 128, 1)">9</span> my_rep:PRIMARY&gt; rs.add("172.24.8.74:27017")</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">5.2 节点删除</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; rs.remove("172.24.8.74:27017")</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>提示:修改副本集成员配置时的限制:</strong></span></div>
<div style="line-height: normal; -ms-word-break: normal">
<ul style="margin: 0; padding-left: 30pt">
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>不能修改_id;</strong></span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>不能将当前执行rs.reconfig命令的成员的优先级设置为 0;</strong></span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>不能将仲裁者成员变为非仲裁者成员,反正亦然;</strong></span></li>
<li style="text-align: left; line-height: 1.75; list-style-type: disc; list-style-position: inside; white-space: pre-wrap; background-color: rgba(0, 0, 0, 0)"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>不能将buildIndexes由false改为 true。</strong></span></li>
</ul>
</div>
<h3 align="left">5.3 Secondary开放临时读</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">默认情况下,Secondary是不提供服务的,即不能读和写。在特殊情况下需要读的可执行rs.slaveOk() ,只对当前连接有效。</span></div>
<h3 align="left">5.4 节点提权</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">默认所有的节点priority都为1,自动选举primary,可通过以下方式手动在已运行的副本集中指定primary。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>注意:以下方式更换主节点必须在当前primary节点操作。</strong></span></div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:SECONDARY&gt; db.serverStatus().repl.primary        #查看主节点
<span style="color: rgba(0, 128, 128, 1)">2</span> 172.24.8.72:27017
<span style="color: rgba(0, 128, 128, 1)">3</span> # mongo --host 172.24.8.72 -u clusteradmin -p clusteradmin
<span style="color: rgba(0, 128, 128, 1)">4</span> my_rep:PRIMARY&gt; rs.status()                                #查看当前副本集状态</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="503" height="337" title="007" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="007" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110408110-238233424.png" border="0"></div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; rs.conf()</pre>
</div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="426" height="750" title="008" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="008" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110409264-531160499.png" border="0"></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; newcfg=rs.conf()                        #当前conf写入变量
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:PRIMARY&gt; newcfg.members.priority=2        #修改conf中members序号0,即172.24.8.71优先级为2
<span style="color: rgba(0, 128, 128, 1)">3</span> 2
<span style="color: rgba(0, 128, 128, 1)">4</span> my_rep:PRIMARY&gt; rs.reconfig(newcfg                        #重新载入conf
<span style="color: rgba(0, 128, 128, 1)">5</span> # mongo --host 172.24.8.71 -u clusteradmin -p clusteradmin
<span style="color: rgba(0, 128, 128, 1)">6</span> my_rep:PRIMARY&gt; db.serverStatus().repl.primary        #再次登录查看primary节点
<span style="color: rgba(0, 128, 128, 1)">7</span> 172.24.8.71:27017</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">5.5 ARBITER节点添加</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # scp -rp /etc/mongod.conf root@arbiter:/etc/mongod.conf
<span style="color: rgba(0, 128, 128, 1)">2</span> # vi /etc/mongod.conf                #修改IP即可
<span style="color: rgba(0, 128, 128, 1)">3</span> net:
<span style="color: rgba(0, 128, 128, 1)">4</span>   port: 27017
<span style="color: rgba(0, 128, 128, 1)">5</span>   bindIp: 172.24.8.75
<span style="color: rgba(0, 128, 128, 1)">6</span> # systemctl restart mongod.service
<span style="color: rgba(0, 128, 128, 1)">7</span> # systemctl enable mongod.service
<span style="color: rgba(0, 128, 128, 1)">8</span> # mongo --host 172.24.8.71 -u clusteradmin -p clusteradmin
<span style="color: rgba(0, 128, 128, 1)">9</span> my_rep:PRIMARY&gt; rs.addArb("172.24.8.75:27017")
<span style="color: rgba(0, 128, 128, 1)"> 10</span> my_rep:PRIMARY&gt; rs.status()</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="534" height="271" title="009" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="009" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110410262-952323034.png" border="0"></div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; rs.conf()</pre>
</div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="363" height="865" title="010" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="010" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110411480-139865142.png" border="0"></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>提示:副本集要求参与选举投票(vote)的节点数为奇数,当我们实际环境中因为机器等原因限制只有两个(或偶数)的节点,这时为了实现 Automatic Failover引入另一类节点:仲裁者(arbiter),仲裁者只参与投票不拥有实际的数据,并且不提供任何服务,因此它对物理资源要求不严格。</strong></span></div>
<h3 align="left">5.6 修改成员状态</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; rs.stepDown()</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>提示:rs.stepDown(60)表示让主节点退化为备份节点,并维持60秒。如果60s内没有新的主节点被选举出来,那么当前节点可以要求重新参与进行选举。</strong></span></div>
<h3 align="left">5.7 锁定主节点</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">将所有的备份节点的priority和votes都设置为0,这样只有主节点有投票权所以无论备份节点是否存在都不会导致主节点的状态由primary变成other。</span></div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; newconf=rs.config()
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:PRIMARY&gt; newconf.members.priority=0
<span style="color: rgba(0, 128, 128, 1)">3</span> my_rep:PRIMARY&gt; newconf.members.votes=0
<span style="color: rgba(0, 128, 128, 1)">4</span> my_rep:PRIMARY&gt; rs.reconfig(newconf)</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">5.8 阻止成员选举</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">如果需要对主节点进行维护操作,但是不希望这段时间内其它成员选举为主节点,可以在每个备份节点上执行freeze命令,以强制它们始终处于备份节点的状态。命令以秒为单位。</span></div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:SECONDARY&gt; rs.freeze(3600)                        #保持1个小时处于备份节点状态。
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:SECONDARY&gt; rs.freeze(0)                                #再次在备份节点执行且将时间指定为0就是“释放”备份节点。</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>注意:如果在退位的备份节点上执行rs.freeze(0),可以让退位的备份节点重新变为主节点。</strong></span></div>
<h2 align="left">六 副本集管理</h2>
<h3 align="left">6.1 查看复制情况</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; db.printSlaveReplicationInfo()
<span style="color: rgba(0, 128, 128, 1)">2</span> source: 172.24.8.72:27017
<span style="color: rgba(0, 128, 128, 1)">3</span>         syncedTo: Tue May 28 2019 19:43:40 GMT+0800 (CST)
<span style="color: rgba(0, 128, 128, 1)">4</span>         0 secs (0 hrs) behind the primary
<span style="color: rgba(0, 128, 128, 1)">5</span> source: 172.24.8.73:27017
<span style="color: rgba(0, 128, 128, 1)">6</span>         syncedTo: Tue May 28 2019 19:43:40 GMT+0800 (CST)
<span style="color: rgba(0, 128, 128, 1)">7</span>         0 secs (0 hrs) behind the primary</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">解释:</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">source:从库的ip和端口。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">syncedTo:目前的同步情况,以及最后一次同步的时间。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">在数据库内容不变的情况下是不同步的,数据库变动就会马上同步。</span></div>
<h3 align="left">6.2 副本集复制链配置</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">MongoDB根据ping时间选择同步源,一个成员向另一个成员发送心跳请求,获取心跳请求所耗费的时间(rs.status()中的"pingMs"记录了成员到达相关成员的所花费的平均时间)。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">MongosDB维护着不同成员间请求的平均花费时间。选择同步源时,会选择一个离自己比较近而且数据比自己新的成员。但是同一数据中心的成员可能会从同一数据中心的其他成员处复制,而不是从位于另一个数据中心的主节点处复制(这样可以减少网络流量),所以会出现复制链的情况,复制链越长会导致主节点的操作复制到所有的服务器所花费的时间越长,从而影响一定的性能。</span></div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="427" height="368" title="011" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="011" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110412106-1134245170.png" border="0"></div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mongo --host 172.24.8.73 -u clusteradmin -p clusteradmin
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:SECONDARY&gt; db.adminCommand({"replSetGetStatus":1})['syncingTo'];                #查看备份节点的复制源
<span style="color: rgba(0, 128, 128, 1)">3</span> 172.24.8.71:27017
<span style="color: rgba(0, 128, 128, 1)">4</span> my_rep:SECONDARY&gt; db.adminCommand({"replSetSyncFrom":"172.24.8.72:27017"})        #配置复制源</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">6.3 查看副本集状态</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; rs.status()</pre>
</div>
<h3 align="left">6.4 查看副本集配置</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; rs.conf()                        #查看节点配置</pre>
</div>
<h3 align="left">6.5 强制重新配置副本集</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">如果副本集无法选出新的主节点,这时需要重新配置副本集。可以在备份节点上调用rs.reconfig(conf,{"force":ture})强制重新配置副本集。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">备份节点收到新的配置文件之后,就会修改自身的配置,并且将新的配置发送给副本集中的其他成员。副本集的其他成员收到新的配置文件之后,会判断配置文件的发送者是否是它们当前配置中的一个成员,如果是,才会用新的配置文件对自己进行重新配置。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">所以,如果新的配置修改了某些成员的主机名,则应该关闭被修改主机名的节点,并以单机模式启动,手动修改locak.system.replset文档,然后以副本集的方式重新启动。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>注意:conf必须是正确、有效的配置。而且强制重新配置只允许在备份节点执行。</strong></span></div>
<h3 align="left">6.6 查看副本集log状态</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; rs.printReplicationInfo()
<span style="color: rgba(0, 128, 128, 1)">2</span> configured oplog size:   1194.595947265625MB
<span style="color: rgba(0, 128, 128, 1)">3</span> log length start to end: 14027secs (3.9hrs)
<span style="color: rgba(0, 128, 128, 1)">4</span> oplog first event time:Tue May 28 2019 17:21:54 GMT+0800 (CST)
<span style="color: rgba(0, 128, 128, 1)">5</span> oplog last event time:   Tue May 28 2019 21:15:41 GMT+0800 (CST)
<span style="color: rgba(0, 128, 128, 1)">6</span> now:                     Tue May 28 2019 21:15:48 GMT+0800 (CST)</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">解释:</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">configured oplog size:oplog配置的大小</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">log length start to end:oplog包含的操作时长。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">oplog first event time:oplog第一条操作的时间。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">oplog last event time:oplog最后一条操作的时间。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">now:当前时间。</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>注意:oplog中第一条操作与最后一条操作的时间差就是操作日志的长度。</strong></span></div>
<h3 align="left">6.7 查看复制延时</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; rs.printSlaveReplicationInfo()
<span style="color: rgba(0, 128, 128, 1)">2</span> source: 172.24.8.72:27017
<span style="color: rgba(0, 128, 128, 1)">3</span>         syncedTo: Tue May 28 2019 21:17:40 GMT+0800 (CST)
<span style="color: rgba(0, 128, 128, 1)">4</span>         0 secs (0 hrs) behind the primary
<span style="color: rgba(0, 128, 128, 1)">5</span> source: 172.24.8.73:27017
<span style="color: rgba(0, 128, 128, 1)">6</span>         syncedTo: Tue May 28 2019 21:17:40 GMT+0800 (CST)
<span style="color: rgba(0, 128, 128, 1)">7</span>         0 secs (0 hrs) behind the primary
<span style="color: rgba(0, 128, 128, 1)">8</span> source: 172.24.8.74:27017
<span style="color: rgba(0, 128, 128, 1)">9</span>         syncedTo: Tue May 28 2019 21:17:40 GMT+0800 (CST)
<span style="color: rgba(0, 128, 128, 1)"> 10</span>         0 secs (0 hrs) behind the primary</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">6.8 副本集信息查看(监控)命令汇总</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">1.复制集状态查询:rs.status()</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">2.查看当前副本集oplog状态:rs.printReplicationInfo()</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">3.查看复制延迟:rs.printSlaveReplicationInfo()</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">4.查看服务状态详情:db.serverStatus()</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">5.查询副本集配置:rs.conf()</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">6.主副本查询:db.isMaster()</span></div>
<h3 align="left">6.9 其他常见维护命令</h3>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">通过rs.help()命令,可以查看副本集相关操作命令:</span></div>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> replSetHO:PRIMARY&gt; rs.help()
<span style="color: rgba(0, 128, 128, 1)">2</span> rs.status()                               #查看副本集整体健康状态
<span style="color: rgba(0, 128, 128, 1)">3</span> rs.initiate()                           #使用默认配置初始化副本集
<span style="color: rgba(0, 128, 128, 1)">4</span> rs.initiate(cfg)                        #使用指定配置,初始化副本集;这是我们使用的命令
<span style="color: rgba(0, 128, 128, 1)">5</span> rs.conf()                                 #从local.system.replset获取副本集当前配置信息
<span style="color: rgba(0, 128, 128, 1)">6</span> rs.reconfig(cfg)                        #指定配置信息重置副本集;指定第二个参数{force:true},来强制更新
<span style="color: rgba(0, 128, 128, 1)">7</span> rs.add(hostportstr)                     #使用默认配置,给副本集添加新成员
<span style="color: rgba(0, 128, 128, 1)">8</span> rs.add(membercfgobj)                      #使用指定配置,给副本集添加新成员
<span style="color: rgba(0, 128, 128, 1)">9</span> rs.addArb(hostportstr)                  #给副本集添加一个仲裁节点,只投票,不会成为数据节点
<span style="color: rgba(0, 128, 128, 1)"> 10</span> rs.stepDown()#给PRIMARY降权,使之在指定时间内成为SECONDARY,会恢复
<span style="color: rgba(0, 128, 128, 1)"> 11</span> rs.syncFrom(hostportstr)                  #使SECONDARY从指定的服务器同步数据
<span style="color: rgba(0, 128, 128, 1)"> 12</span> rs.freeze(secs)                           #让自己在指定秒数内不会成为PRIMARY
<span style="color: rgba(0, 128, 128, 1)"> 13</span> rs.remove(hostportstr)                  #从副本集删除指定节点
<span style="color: rgba(0, 128, 128, 1)"> 14</span> rs.slaveOk()                              #SECONDARY节点默认是不能查询的,需要执行该命令,使之可以查询
<span style="color: rgba(0, 128, 128, 1)"> 15</span> rs.printReplicationInfo()               #查看操作日志以及日志时间
<span style="color: rgba(0, 128, 128, 1)"> 16</span> rs.printSlaveReplicationInfo()            #查看所有SECONDARY延迟情况
<span style="color: rgba(0, 128, 128, 1)"> 17</span> db.isMaster()                           #查询当前PRIMARY信息</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>提示:更多副本集选举参考官方:https://docs.mongodb.com/manual/core/replica-set-elections/。</strong></span></div>
<h2 align="left">七 副本集复制功能测试</h2>
<h3 align="left">7.1 授权</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mongo --host 172.24.8.71 -u admin -p admin
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:PRIMARY&gt; use admin
<span style="color: rgba(0, 128, 128, 1)">3</span> my_rep:PRIMARY&gt; db.grantRolesToUser( "admin",[{ role: "dbOwner",db:"mydb" }])</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">7.2 插入数据</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> my_rep:PRIMARY&gt; use mydb
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:PRIMARY&gt; db.age01.insert({name: 'zhangsan',
<span style="color: rgba(0, 128, 128, 1)">3</span> age: '18',
<span style="color: rgba(0, 128, 128, 1)">4</span> tel: '123456781',
<span style="color: rgba(0, 128, 128, 1)">5</span> love: ['apple','banana']
<span style="color: rgba(0, 128, 128, 1)">6</span> }</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">7.3 查看同步</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mongo --host 172.24.8.72 -u admin -p admin
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:SECONDARY&gt; rs.slaveOk()
<span style="color: rgba(0, 128, 128, 1)">3</span> my_rep:SECONDARY&gt; use mydb
<span style="color: rgba(0, 128, 128, 1)">4</span> my_rep:SECONDARY&gt; db.age01.count()
<span style="color: rgba(0, 128, 128, 1)">5</span> 1</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h2 align="left">八 副本集故障转移功能测试</h2>
<h3 align="left">8.1 关闭主节点</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mongo --host 172.24.8.71 -u clusteradmin -p clusteradmin
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:PRIMARY&gt; rs.status()
<span style="color: rgba(0, 128, 128, 1)">3</span> my_rep:PRIMARY&gt; use admin
<span style="color: rgba(0, 128, 128, 1)">4</span> switched to db admin
<span style="color: rgba(0, 128, 128, 1)">5</span> my_rep:PRIMARY&gt; db.shutdownServer()
<span style="color: rgba(0, 128, 128, 1)">6</span> # mongo --host 172.24.8.72 -u clusteradmin -p clusteradmin        #登录任意非关闭节点
<span style="color: rgba(0, 128, 128, 1)">7</span> my_rep:PRIMARY&gt; rs.status()
<span style="color: rgba(0, 128, 128, 1)">8</span> my_rep:PRIMARY&gt; db.serverStatus().repl.primary                        #查看主节点
<span style="color: rgba(0, 128, 128, 1)">9</span> 172.24.8.72:27017</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">8.2 插入数据</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # mongo --host 172.24.8.72 -u admin -p admin
<span style="color: rgba(0, 128, 128, 1)">2</span> my_rep:PRIMARY&gt; use mydb
<span style="color: rgba(0, 128, 128, 1)">3</span> db.age01.insertOne({name: 'wanger', age: '15', tel: '123456783', love: ['pear','orange']})</pre>
</div>
<div align="left" style="line-height: 1.6666; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<h3 align="left">8.3 开启关闭节点</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> # systemctl restart mongod.service
<span style="color: rgba(0, 128, 128, 1)">2</span> # mongo --host 172.24.8.71 -u admin -p admin
<span style="color: rgba(0, 128, 128, 1)">3</span> my_rep:SECONDARY&gt; use mydb
<span style="color: rgba(0, 128, 128, 1)">4</span> switched to db mydb
<span style="color: rgba(0, 128, 128, 1)">5</span> my_rep:SECONDARY&gt; rs.slaveOk()
<span style="color: rgba(0, 128, 128, 1)">6</span> my_rep:SECONDARY&gt; db.age01.find().pretty()</pre>
</div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="center" style="line-height: normal; float: none; -ms-word-break: normal"><img width="426" height="340" title="012" style="margin: 0 auto; border: 0 currentColor; border-image: none; float: none; display: block; background-image: none" alt="012" src="https://img2018.cnblogs.com/blog/680719/201906/680719-20190606110412856-1216298213.png" border="0"></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>提示:当关闭节点mongo服务重启启动后,能正常以SECONDARY加入副本集,并且自动同步相关数据。</strong></span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>注意:所有的Secondary都宕机、或则副本集中只剩下一个节点,则该节点只能为Secondary节点,也就意味着整个集群只能进行读操作而不能进行写操作,当其他节点恢复时,之前的primary节点仍然是primary节点。</strong></span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>当某个节点宕机后重新启动该节点会有一段的时间(时间长短视集群的数据量和宕机时间而定)导致整个集群中所有节点都成为secondary而无法进行写操作(如果应用程序没有设置相应的ReadReference也可能不能进行读取操作)。</strong></span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="color: rgba(255, 0, 0, 1); font-family: 微软雅黑; font-size: small"><strong>官方推荐的最小的副本集也应该具备一个primary节点和两个secondary节点。两个节点的副本集不具备真正的故障转移能力。</strong></span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><br></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">参考:https://www.cnblogs.com/zhoujinyi/p/3554010.html</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span style="font-family: 微软雅黑; font-size: small">https://www.cnblogs.com/chenmh/p/8681867.html</span></div>
<div align="left" style="line-height: 1.75; white-space: pre-wrap; -ms-word-break: normal"><span><span style="font-family: 微软雅黑; font-size: small">&nbsp;</span></span></div>

</div>
<div id="MySignature" role="contentinfo">
    <div style="background: #f7acbc; color: #0; font-size: small">
<p>
作者:木二
</p>
<p>
出处:http://www.cnblogs.com/itzgr/
</p>
<p>
关于作者:云计算、虚拟化,Linux,多多交流!
</p>
<p>
本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接!如有其他问题,可邮件(xhy@itzgr.com)咨询。
</p>
</div><br><br>
来源:https://www.cnblogs.com/itzgr/p/10983794.html
頁: [1]
查看完整版本: 006.MongoDB副本集