毛蛋 發表於 2020-6-18 18:25:00

MongoDB主从复制(master-->slave)环境搭建

<p><strong><font color="#333333" style="background-color: rgba(204, 204, 204, 1)">导读:</font></strong></p><p><strong><font color="#333333" style="background-color: rgba(204, 204, 204, 1)">MongoDB一直都不推荐使用主从复制方式进行数据同步,而是推荐复制集(replicate set),相对于主从同步,复制集有更多优秀的功能,如自动故障转移等,但是,既然MongoDB到4.2版本还保留着该功能,我们不妨探索一下。</font></strong></p><p></p><p><strong><br></strong></p><strong>基础信息</strong><strong>
</strong><strong>
</strong><strong>
</strong><table width="900" border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td width="225" valign="top"><strong>IP地址</strong></td><td width="225" valign="top"><strong>操作系统版本</strong></td><td width="225" valign="top"><strong>数据库版本</strong></td><td width="225" valign="top"><strong>作用</strong></td>
</tr>
<tr>
<td width="225" valign="top">192.168.10.71</td>
<td width="225" valign="top">centos7.4</td>
<td width="225" valign="top">mongo 2.7.1</td>
<td width="225" valign="top">主节点(Master)</td>
</tr>
<tr>
<td width="225" valign="top">192.168.10.72</td>
<td width="225" valign="top">centos7.4</td>
<td width="225" valign="top">mongo 2.7.1</td>
<td width="225" valign="top">从节点(Slave)</td>
</tr>
</tbody>
</table>
<p><br></p><p><strong>(一)MongoDB安装</strong></p><p>MongoDB每个版本的安装方法都差不多,安装见:Linux下安装MongoDB 4.2数据库--使用tar包方式</p><p><br></p><p><strong>(二)MongoDB主从环境配置</strong></p><p>对于主从配置,相对于其它数据库而言,MongoDB的配置非常简单,只需要配置参数文件即可。</p><table width="900" border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td width="450" valign="top">主节点:192.168.10.71</td>
<td width="450" valign="top">从节点:192.168.10.72</td>
</tr>
<tr>
<td width="450" valign="top"><div class="cnblogs_code" style="padding: 5px; border: 1px solid rgba(204, 204, 204, 1); border-image: none; background-color: rgba(245, 245, 245, 1)"><pre>logpath=/mongo/log/<span style="color: rgba(0, 0, 0, 1)">mongod.log
logappend</span>=<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">

# fork and run </span><span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> background
fork</span>=<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">

port</span>=<span style="color: rgba(128, 0, 128, 1)">27017</span><span style="color: rgba(0, 0, 0, 1)">
dbpath</span>=/mongo/<span style="color: rgba(0, 0, 0, 1)">data

# location of pidfile
pidfilepath</span>=/mongo/<span style="color: rgba(0, 0, 0, 1)">mongod.pid

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip</span>=<span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span><span style="color: rgba(0, 0, 0, 1)">
master</span>=<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
keyFile</span>=/mongo/mongodb-<span style="color: rgba(0, 0, 0, 1)">keyfile
auth</span>=<span style="color: rgba(0, 0, 255, 1)">true</span></pre></div></td>
<td width="450" valign="top"><div class="cnblogs_code" style="padding: 5px; border: 1px solid rgba(204, 204, 204, 1); border-image: none; background-color: rgba(245, 245, 245, 1)"><pre>logpath=/mongo/log/<span style="color: rgba(0, 0, 0, 1)">mongod.log
logappend</span>=<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">

# fork and run </span><span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> background
fork</span>=<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">

port</span>=<span style="color: rgba(128, 0, 128, 1)">27017</span><span style="color: rgba(0, 0, 0, 1)">
dbpath</span>=/mongo/<span style="color: rgba(0, 0, 0, 1)">data

# location of pidfile
pidfilepath</span>=/mongo/<span style="color: rgba(0, 0, 0, 1)">mongod.pid

# Listen to local interface only. Comment out to listen on all interfaces.
bind_ip</span>=<span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span><span style="color: rgba(0, 0, 0, 1)">
autoresync</span>=<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
slave</span>=<span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">
source</span>=<span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.71</span>:<span style="color: rgba(128, 0, 128, 1)">27017</span><span style="color: rgba(0, 0, 0, 1)">
keyFile</span>=/mongo/mongodb-<span style="color: rgba(0, 0, 0, 1)">keyfile
auth</span>=<span style="color: rgba(0, 0, 255, 1)">true</span></pre></div></td>
</tr>
</tbody>
</table>
<p>特别注意:</p><ul><li>主节点需要配置:master=true参数;
</li><li>从节点需要配置:slave=true参数和source参数,source参数指定master数据库的IP及端口;
</li><li>如果启用了用户登录验证auth=true,那么还需要配置KeyFile参数,主从才能正常同步;
</li><li>从节点的autoresync=true是一个可选参数。主从同步的本质是主节点将重做日志(存放在local数据库下的oplog.$main集合中)传输到从节点,从节点再次根据日志执行一遍。但是oplog.$main是一个上限集合,如果主节点的日志还未拷贝到从节点就发生了日志覆盖,此时管理员必须手动重新搭建复制环境,假如使用了autoresync=true参数,当主从不同步时,从库将在10分钟内自动尝试重新同步一次。</li><li>oplogSize参数可以用来指定oplog.$main集合的最大空间尺寸,如果未指定mongod将在启动时分配5%的可用磁盘空间给oplog。</li></ul><p><br></p><p><strong>(三)keyFile注意事项</strong></p><p>当启用用户身份认证时,主从节点需要配置keyfil文件以便于主从节点可以正常通信,keyfile的配置见上面的mongoDB配置文件,这里讲一下如何生产keyfile。</p><p>在一台服务器上生成keyfile,并且将权限改为600</p><div class="cnblogs_code" style="padding: 5px; border: 1px solid rgba(204, 204, 204, 1); border-image: none; background-color: rgba(245, 245, 245, 1)"><pre>openssl rand -base64 <span style="color: rgba(128, 0, 128, 1)">745</span> &gt; /mongo/mongodb-<span style="color: rgba(0, 0, 0, 1)">keyfile
</span><span style="color: rgba(0, 0, 255, 1)">chmod</span> <span style="color: rgba(128, 0, 128, 1)">600</span> /mongo/mongodb-keyfile</pre></div><p>将这个文件拷贝到其它节点上即可,需要注意,各个节点的权限需要保持一致。</p><p><br></p><p><font color="#ff0000">遇到的问题:</font></p><p>在创建完keyfile并在配置文件中加入参数后,启动MongoDB,发现无法启动:</p><div class="cnblogs_code" style="padding: 5px; border: 1px solid rgba(204, 204, 204, 1); border-image: none; background-color: rgba(245, 245, 245, 1)"><pre># mongod -f /mongo/<span style="color: rgba(0, 0, 0, 1)">mongodb.conf
warning: bind_ip of </span><span style="color: rgba(128, 0, 128, 1)">0.0</span>.<span style="color: rgba(128, 0, 128, 1)">0.0</span><span style="color: rgba(0, 0, 0, 1)"> is unnecessary; listens on all ips by default
about to fork child process, waiting </span><span style="color: rgba(0, 0, 255, 1)">until</span> server is ready <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> connections.
forked process: </span><span style="color: rgba(128, 0, 128, 1)">2173</span><span style="color: rgba(0, 0, 0, 1)">
ERROR: child process failed, exited with error number </span><span style="color: rgba(128, 0, 128, 1)">1</span></pre></div><p>查看MongoDB错误日志,报错如下:</p><div class="cnblogs_code" style="padding: 5px; border: 1px solid rgba(204, 204, 204, 1); border-image: none; background-color: rgba(245, 245, 245, 1)"><pre># <span style="color: rgba(0, 0, 255, 1)">tail</span> -<span style="color: rgba(0, 0, 0, 1)">f mongod.log
</span><span style="color: rgba(128, 0, 128, 1)">2020</span>-<span style="color: rgba(128, 0, 128, 1)">06</span>-18T21:<span style="color: rgba(128, 0, 128, 1)">20</span>:<span style="color: rgba(128, 0, 128, 1)">13.900</span>+<span style="color: rgba(128, 0, 128, 1)">0800</span> invalid <span style="color: rgba(0, 0, 255, 1)">char</span> <span style="color: rgba(0, 0, 255, 1)">in</span> key <span style="color: rgba(0, 0, 255, 1)">file</span> /mongo/mon-keyfile: =</pre></div><p>打开keyfile,发现在文件末尾有2个“=”,删除2个等号之后,MongoDB启动正常。需要留意,该操作改变了feyfile的内容,需要重新同步到其它节点。</p><p><br></p><p><br></p><p>【完】</p><br><br>
来源:https://www.cnblogs.com/lijiaman/p/13159093.html
頁: [1]
查看完整版本: MongoDB主从复制(master-->slave)环境搭建