给你阳光就灿烂 發表於 2020-11-10 17:17:00

mongodb的oplog日志

<p>##########################################################################</p>
<h3>oplog不及binlog:</h3>
<p>首先,来一个综述,比较mongodb的oplog和mysql的binlog,oplog不如binlog,比如设置一个延迟节点的时间,由于oplog是一个固定大小的集合,延迟时间设置大了,就会无法同步数据,而mysql的binlog可以设置binlog过期时间,根本不用担心。</p>
<pre></pre>
<div>
<div>
<p>任何一种数据库都有各种各样的日志,MongoDB也不例外。MongoDB中有4种日志,分别是系统日志、Journal日志、oplog主从日志、慢查询日志等。这些日志记录着MongoDB数据库不同方面的踪迹。下面分别介绍这几种日志。</p>
<h3 class="heading" data-id="heading-0">系统日志</h3>
<p>系统日志在MongoDB数据库中很重要,它记录着MongoDB启动和停止的操作,以及服务器在运行过程中发生的任何异常信息。</p>
<p>配置系统日志的方法比较简单,在启动mongod时指定logpath参数即可</p>
<pre><code class="hljs bash copyable" lang="bash">mongod -logpath=/data/<span class="hljs-built_in">log/mongodb/serverlog.log -logappend
<span class="copy-code-btn">复制代码</span></span></code></pre>
<p>系统日志会向logpath指定的文件持续追加。</p>
<h3 class="heading" data-id="heading-1">Journal日志</h3>
<p>journaling(日记) 日志功能则是 MongoDB 里面非常重要的一个功能 , 它保证了数据库服务器在意外断电 、 自然灾害等情况下数据的完整性。它通过预写式的redo日志为MongoDB增加了额外的可靠性保障。开启该功能时,MongoDB会在进行写入时建立一条Journal日志,其中包含了此次写入操作具体更改的磁盘地址和字节。因此一旦服务器突然停机,可在启动时对日记进行重放,从而重新执行那些停机前没能够刷新到磁盘的写入操作。</p>
<p>MongoDB配置WiredTiger引擎使用内存缓冲区来保存journal记录,WiredTiger根据以下间隔或条件将缓冲的日志记录同步到磁盘</p>
<ol>
<li>从MongoDB 3.2版本开始每隔50ms将缓冲的journal数据同步到磁盘</li>
<li>如果写入操作设置了j:true,则WiredTiger强制同步日志文件</li>
<li>由于MongoDB使用的journal文件大小限制为100MB,因此WiredTiger大约每100MB数据创建一个新的日志文件。当WiredTiger创建新的journal文件时,WiredTiger会同步以前journal文件</li>
</ol>
<p>MongoDB达到上面的提交,便会将更新操作写入日志。这意味着MongoDB会批量地提交更改,即每次写入不会立即刷新到磁盘。不过在默认设置下,系统发生崩溃时,不可能丢失超过50ms的写入数据。</p>
<p>数据文件默认每60秒刷新到磁盘一次,因此Journal文件只需记录约60s的写入数据。日志系统为此预先分配了若干个空文件,这些文件存放在/data/db/journal目录中,目录名为_j.0、_j.1等 长时间运行MongoDB后,日志目录中会出现类似_j.6217、_j.6218的文件,这些是当前的日志文件,文件中的数值会随着MongoDB运行时间的增长而增大。数据库正常关闭后,日记文件会被清除(因为正常关闭后就不在需要这些文件了).</p>
<blockquote>
<p>向mongodb中写入数据是先写入内存,然后每隔60s在刷盘,同样写入journal,也是先写入对应的buffer,然后每隔50ms在刷盘到磁盘的journal文件 使用WiredTiger,即使没有journal功能,MongoDB也可以从最后一个检查点(checkpoint,可以想成镜像)恢复;但是,要恢复在上一个检查点之后所做的更改,还是需要使用Journal</p>
</blockquote>
<p>如发生系统崩溃或使用kill -9命令强制终止数据库的运行,mongod会在启动时重放journal文件,同时会显示出大量的校验信息。</p>
<blockquote>
<p>上面说的都是针对WiredTiger引擎,对于MMAPv1引擎来说有一点不一样,首先它是每100ms进行刷盘,其次它是通过private view写入journal文件,通过shared view写入数据文件。这里就不过多讲解了,因为MongoDB 4.0已经不推荐使用这个存储引擎了。 从MongoDB 3.2版本开始WiredTiger是MongoDB推荐的默认存储引擎</p>
</blockquote>
<p>需要注意的是如果客户端的写入速度超过了日记的刷新速度,mongod则会限制写入操作,直到日记完成磁盘的写入。这是mongod会限制写入的唯一情况。</p>
<h3 class="heading" data-id="heading-2">固定集合(Capped Collection)</h3>
<p>在讲下面两种日志之前先来认识下capped collection。</p>
<p>MongoDB中的普通集合是动态创建的,而且可以自动增长以容纳更多的数据。MongoDB中还有另一种不同类型的集合,叫做固定集合。固定集合需要事先创建好,而且它的大小是固定的。固定集合的行为类型与循环队列一样。如果没有空间了,最老的文档会被删除以释放空间,新插入的文档会占据这块空间。</p>
<p>创建固定集合:</p>
<pre><code class="hljs bash copyable" lang="bash">db.createCollection(<span class="hljs-string">"collectionName",{<span class="hljs-string">"capped":<span class="hljs-literal">true, <span class="hljs-string">"size":100000, <span class="hljs-string">"max":100})
<span class="copy-code-btn">复制代码</span></span></span></span></span></span></code></pre>
<p>创建了一个大小为100000字节的固定大小集合,文档数量为100.不管先到达哪个限制,之后插入的新文档就会把最老的文档挤出集合:<strong>固定集合的文档数量不能超过文档数量限制,也不能超过大小限制。</strong></p>
<p>固定集合创建之后就不能改变,无法将固定集合转换为非固定集合,但是可以将常规集合转换为固定集合。</p>
<pre><code class="hljs bash copyable" lang="bash">db.runCommand({<span class="hljs-string">"convertToCapped": <span class="hljs-string">"test", <span class="hljs-string">"size" : 10000});
<span class="copy-code-btn">复制代码</span></span></span></span></code></pre>
<p>固定集合可以进行一种特殊的排序,称为自然排序(natural sort),自然排序返回结果集中文档的顺序就是文档在磁盘的顺序。自然顺序就是文档的插入顺序,因此自然排序得到的文档是从旧到新排列的。当然也可以按照从新到旧:</p>
<pre><code class="hljs bash copyable" lang="bash">db.my_capped_collection.find().sort({<span class="hljs-string">"<span class="hljs-variable">$natural": -1});
<span class="copy-code-btn">复制代码</span></span></span></code></pre>
<h3 class="heading" data-id="heading-3">oplog主从日志</h3>
<p>Replica Sets复制集用于在多台服务器之间备份数据。MongoDB的复制功能是使用操作日志oplog实现的,操作日志包含了主节点的每一次写操作。oplog是主节点的local数据库中的一个固定集合。备份节点通过查询这个集合就可以知道需要进行复制的操作。</p>
<blockquote>
<p>一个mongod实例中的所有数据库都使用同一个oplog,也就是所有数据库的操作日志(插入,删除,修改)都会记录到oplog中</p>
</blockquote>
<p>每个备份节点都维护着自己的oplog,记录着每一次从主节点复制数据的操作。这样,每个成员都可以作为同步源给其他成员使用。 如图所示,备份节点从当前使用的同步源中获取需要执行的操作,然后在自己的数据集上执行这些操作,最后再将这些操作写入自己的oplog,如果遇到某个操作失败的情况(只有当同步源的数据损坏或者数据与主节点不一致时才可能发生),那么备份节点就会停止从当前的同步源复制数据。</p>
<p>&nbsp;</p>
<img src="https://user-gold-cdn.xitu.io/2019/8/6/16c65752be317356?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" alt="oplog-copy.png" class="lazyload inited loaded" data-src="https://user-gold-cdn.xitu.io/2019/8/6/16c65752be317356?imageView2/0/w/1280/h/960/format/webp/ignore-error/1" data-width="367" data-height="237">
<p>&nbsp;</p>
<p>oplog中按顺序保存着所有执行过的写操作,replica sets中每个成员都维护者一份自己的oplog,每个成员的oplog都应该跟主节点的oplog完全一致(可能会有一些延迟)</p>
<p>如果某个备份节点由于某些原因挂了,但它重新启动后,就会自动从oplog中最后一个操作开始进行同步。由于复制操作的过程是想复制数据在写入oplog,所以备份节点可能会在已经同步过的数据上再次执行复制操作。MongoDB在设计之初就考虑到了这种情况:将oplog中的同一个操作执行多次,与只执行一次的效果是一样的。</p>
<p>由于oplog大小是固定的,它只能保持特定数量的操作日志。通常,oplog使用空间的增长速度与系统处理写请求的速率几乎相同:如果主节点上每分钟处理了1KB的写入请求,那么oplog很可能也会在一分钟内写入1KB条操作日志。</p>
<p>但是,有一些例外:如果单次请求能够影响到多个文档(比如删除多个文档或者多文档更新),oplog中就会出现多条操作日志。如果单个操作会影响多个文档,那么每个受影响的文档都会对应oplog的一条日志。因此,如果执行db.student.remove()删除了10w个文档,那么oplog中也就会有10w条操作日志,每个日志对应一个被删除的文档。如果执行大量的批量操作,oplog很快就会被填满。</p>
<h3 class="heading" data-id="heading-4">慢查询日志</h3>
<p>MongoDB中使用系统分析器(system profiler)来查找耗时过长的操作。系统分析器记录固定集合system.profile中的操作,并提供大量有关耗时过长的操作信息,但相应的mongod的整体性能也会有所下降。因此我们一般定期打开分析器来获取信息。</p>
<p>默认情况下,系统分析器处于关闭状态,不会进行任何记录。可以在shell中运行<code>db.setProfilingLevel()</code>开启分析器</p>
<pre><code class="hljs bash copyable" lang="bash">db.setProfilingLevel(level,&lt;slowms&gt;) 0=off 1=slow 2=all
<span class="copy-code-btn">复制代码</span></code></pre>
<p>第一个参数是指定级别,不同的级别代表不同的意义,0表示关闭,1表示默认记录耗时大于100毫秒的操作,2表示记录所有操作。第二个参数则是自定义“耗时过长"标准,比如记录所有耗时操作500ms的操作</p>
<pre><code class="hljs bash copyable" lang="bash">db.setProfilingLevel(1,500);
<span class="copy-code-btn">复制代码</span></code></pre>
<p>如果开启了分析器而system.profile集合并不存在,MongoDB会为其建立一个大小为若干MB的固定集合(capped collection)。如希望分析器运行更长时间,可能需要更大的空间记录更多的操作。此时可以关闭分析器,删除并重新建立一个新的名为system.profile的固定集合,并令其容量符合要求。然后在数据库上重新启用分析器。</p>
<blockquote>
<p>可以通过db.system.profile.stats()查看集合的最大容量.</p>
</blockquote>
</div>
<br>作者:think123<br>链接:https://juejin.im/post/6844903907169140750<br>来源:掘金<br>著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3>动态更改oplog大小:</h3>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>db.adminCommand({replSetResizeOplog:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>, size: <span style="color: rgba(128, 0, 0, 1); font-weight: bold">16384</span>})</pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3>&nbsp;获取最旧的一个操作:</h3>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>glc<span style="color: rgba(128, 128, 128, 1)">-</span>test:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> local
switched </span><span style="color: rgba(0, 0, 255, 1)">to</span><span style="color: rgba(0, 0, 0, 1)"> db local
glc</span><span style="color: rgba(128, 128, 128, 1)">-</span>test:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> db.oplog.rs.find().sort({$natural: <span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>}).pretty().limit(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">)
{
      "ts" : </span><span style="color: rgba(0, 0, 255, 1)">Timestamp</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1605170004</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">),
      "t" : NumberLong(</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">115</span><span style="color: rgba(0, 0, 0, 1)">),
      "h" : NumberLong("</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">8786076471049145979</span><span style="color: rgba(0, 0, 0, 1)">"),
      "v" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,
      "op" : "n",
      "ns" : "",
      "wall" : ISODate("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2020</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">11</span><span style="color: rgba(128, 128, 128, 1)">-</span>12T08:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">33</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">24</span><span style="color: rgba(0, 0, 0, 1)">.005Z"),
      "o" : {
                "msg" : "periodic noop"
      }
}
glc</span><span style="color: rgba(128, 128, 128, 1)">-</span>test:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> </pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3>&nbsp;获取最新的一个操作:</h3>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>glc<span style="color: rgba(128, 128, 128, 1)">-</span>test:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> <span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> local
switched </span><span style="color: rgba(0, 0, 255, 1)">to</span><span style="color: rgba(0, 0, 0, 1)"> db local
glc</span><span style="color: rgba(128, 128, 128, 1)">-</span>test:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> db.oplog.rs.find().sort({$natural: <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>}).pretty().limit(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">)
{
      "ts" : </span><span style="color: rgba(0, 0, 255, 1)">Timestamp</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1604648269</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">8897</span><span style="color: rgba(0, 0, 0, 1)">),
      "t" : NumberLong(</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">113</span><span style="color: rgba(0, 0, 0, 1)">),
      "h" : NumberLong("</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">8471504986882691135</span><span style="color: rgba(0, 0, 0, 1)">"),
      "v" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2</span><span style="color: rgba(0, 0, 0, 1)">,
      "op" : "i",
      "ns" : "eagle.content_v1",
      "ui" : UUID("02ddbc0a</span><span style="color: rgba(128, 128, 128, 1)">-</span>e76b<span style="color: rgba(128, 128, 128, 1)">-</span>450e<span style="color: rgba(128, 128, 128, 1)">-</span>ae38<span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(0, 0, 0, 1)">bd5eb220c773"),
      "wall" : ISODate("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2020</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">11</span><span style="color: rgba(128, 128, 128, 1)">-</span>06T07:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">37</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">49</span><span style="color: rgba(0, 0, 0, 1)">.312Z"),
      "o" : {
                "_id" : "1478dong",
                "uid" : "",
                "lv3" : "",
                "lv4" : "",
                "lv5" : "",
                "comment_id" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1478</span><span style="color: rgba(0, 0, 0, 1)">",
                "creation_time" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2020</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">10</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">22</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">17</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">11</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">14</span><span style="color: rgba(0, 0, 0, 1)">",
                "content" : "内容",
                "skuid" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">77</span><span style="color: rgba(0, 0, 0, 1)">",
                "xxname" : "</span><span style="color: rgba(0, 0, 0, 1)">蓝牙",
                "score" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">5</span><span style="color: rgba(0, 0, 0, 1)">",
                "name" : "xxx",
                "rate" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0.99</span><span style="color: rgba(0, 0, 0, 1)">",
                "general_rate" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0.003</span><span style="color: rgba(0, 0, 0, 1)">",
                "poor_rate" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0.007</span><span style="color: rgba(0, 0, 0, 1)">",
                "poorcount" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">242</span><span style="color: rgba(0, 0, 0, 1)">",
                "general_count" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">132</span><span style="color: rgba(0, 0, 0, 1)">",
                "good" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">5759</span><span style="color: rgba(0, 0, 0, 1)">",
                "comment" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">10165</span><span style="color: rgba(0, 0, 0, 1)">",
                "fetchtime" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">2020</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">10</span><span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">23</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">01</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">17</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">57</span><span style="color: rgba(0, 0, 0, 1)">",
                "first" : "数码",
                "second" : "影",
                "third" : "音</span><span style="color: rgba(0, 0, 0, 1)">",
                "source" : "jingdong",
                "sub_comment" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
                        "内容"
                </span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">,
                "sub_comment_sa" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
                        0,
                        0
                </span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">,
                "sub_comment_entity" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
                        [
                              "功能"
                        </span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
                ],
                "sub_tags3" : </span><span style="color: rgba(255, 0, 0, 1)">[</span> <span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">,
                "sub_tags4" : </span><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">
                        [ </span><span style="color: rgba(255, 0, 0, 1)">]</span><span style="color: rgba(0, 0, 0, 1)">
                ],
                "su" : "箱",
                "tem_id" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1004</span><span style="color: rgba(0, 0, 0, 1)">",
                "sa_origin" : "",
                "sa" : "</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span><span style="color: rgba(0, 0, 0, 1)">",
                "category" : "狗狗",
                "pms_id" : "",
                "source_comment_id" : "",
                "update_time" : NumberLong("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1603405763132</span><span style="color: rgba(0, 0, 0, 1)">")
      }
}
glc</span><span style="color: rgba(128, 128, 128, 1)">-</span>test:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> </pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;oplog的时间滑动窗口范围就是两者之间的时间ts差值:</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>glc<span style="color: rgba(128, 128, 128, 1)">-</span>test:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> db.oplog.rs.find({},{ts:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>}).sort({$natural: <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>}).pretty().limit(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">)
{ "ts" : </span><span style="color: rgba(0, 0, 255, 1)">Timestamp</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1604648269</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">8897</span><span style="color: rgba(0, 0, 0, 1)">) }
glc</span><span style="color: rgba(128, 128, 128, 1)">-</span>test:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> db.oplog.rs.find({},{ts:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>}).sort({$natural: <span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>}).pretty().limit(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">)
{ "ts" : </span><span style="color: rgba(0, 0, 255, 1)">Timestamp</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1605171394</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">) }
glc</span><span style="color: rgba(128, 128, 128, 1)">-</span>test:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> </pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3>&nbsp;oplog的动态扩容与缩容:(mongodb版本至少3.6以上)</h3>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>mivpn:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)"> db.getReplicationInfo()
{
      "logSizeMB" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">65536</span><span style="color: rgba(0, 0, 0, 1)">,
      "usedMB" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3061.23</span><span style="color: rgba(0, 0, 0, 1)">,
      "timeDiff" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">769648</span><span style="color: rgba(0, 0, 0, 1)">,
      "timeDiffHours" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">213.79</span><span style="color: rgba(0, 0, 0, 1)">,
      "tFirst" : "Wed Jan </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">20</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2021</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">12</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">22</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">17</span> GMT<span style="color: rgba(128, 128, 128, 1)">+</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0800</span><span style="color: rgba(0, 0, 0, 1)"> (CST)",
      "tLast" : "Fri Jan </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">29</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2021</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">10</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">09</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">45</span> GMT<span style="color: rgba(128, 128, 128, 1)">+</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0800</span><span style="color: rgba(0, 0, 0, 1)"> (CST)",
      "now" : "Fri Jan </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">29</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2021</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">10</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">09</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">46</span> GMT<span style="color: rgba(128, 128, 128, 1)">+</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0800</span><span style="color: rgba(0, 0, 0, 1)"> (CST)"
}
mivpn:</span><span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> db.adminCommand({replSetResizeOplog:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span>,size:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">102400</span><span style="color: rgba(0, 0, 0, 1)">})
{
      "ok" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">1</span><span style="color: rgba(0, 0, 0, 1)">,
      "operationTime" : </span><span style="color: rgba(0, 0, 255, 1)">Timestamp</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1611886191</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">79</span><span style="color: rgba(0, 0, 0, 1)">),
      "$clusterTime" : {
                "clusterTime" : </span><span style="color: rgba(0, 0, 255, 1)">Timestamp</span>(<span style="color: rgba(128, 0, 0, 1); font-weight: bold">1611886191</span>, <span style="color: rgba(128, 0, 0, 1); font-weight: bold">79</span><span style="color: rgba(0, 0, 0, 1)">),
                "signature" : {
                        "hash" : BinData(</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0</span>,"OUP6mdMfsdZU<span style="color: rgba(128, 128, 128, 1)">/</span>YM98XDnlhdXlzg<span style="color: rgba(128, 128, 128, 1)">=</span><span style="color: rgba(0, 0, 0, 1)">"),
                        "keyId" : NumberLong("</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">6919692836459773955</span><span style="color: rgba(0, 0, 0, 1)">")
                }
      }
}
mivpn:</span><span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)"> db.getReplicationInfo()
{
      "logSizeMB" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">102400</span><span style="color: rgba(0, 0, 0, 1)">,
      "usedMB" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">3061.24</span><span style="color: rgba(0, 0, 0, 1)">,
      "timeDiff" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">769654</span><span style="color: rgba(0, 0, 0, 1)">,
      "timeDiffHours" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">213.79</span><span style="color: rgba(0, 0, 0, 1)">,
      "tFirst" : "Wed Jan </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">20</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2021</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">12</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">22</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">17</span> GMT<span style="color: rgba(128, 128, 128, 1)">+</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0800</span><span style="color: rgba(0, 0, 0, 1)"> (CST)",
      "tLast" : "Fri Jan </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">29</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2021</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">10</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">09</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">51</span> GMT<span style="color: rgba(128, 128, 128, 1)">+</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0800</span><span style="color: rgba(0, 0, 0, 1)"> (CST)",
      "now" : "Fri Jan </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">29</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2021</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">10</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">09</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">58</span> GMT<span style="color: rgba(128, 128, 128, 1)">+</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0800</span><span style="color: rgba(0, 0, 0, 1)"> (CST)"
}
mivpn:</span><span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span> </pre>
</div>
<p>&nbsp;</p>
<h3>&nbsp;回收空间:</h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> local;
db.runCommand({ "compact" : "oplog.rs" });</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3>&nbsp;oplog文件大小:local.oplog.rs对应的物理文件是压缩文件:1/3</h3>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre>glc:<span style="color: rgba(0, 0, 255, 1)">PRIMARY</span><span style="color: rgba(128, 128, 128, 1)">&gt;</span><span style="color: rgba(0, 0, 0, 1)"> db.getReplicationInfo()
{
      "logSizeMB" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">900000</span><span style="color: rgba(0, 0, 0, 1)">,
      "usedMB" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">892698.48</span><span style="color: rgba(0, 0, 0, 1)">,
      "timeDiff" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">213954</span><span style="color: rgba(0, 0, 0, 1)">,
      "timeDiffHours" : </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">59.43</span><span style="color: rgba(0, 0, 0, 1)">,
      "tFirst" : "Wed Jan </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">27</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2021</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">05</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">09</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">20</span> GMT<span style="color: rgba(128, 128, 128, 1)">+</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0800</span><span style="color: rgba(0, 0, 0, 1)"> (CST)",
      "tLast" : "Fri Jan </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">29</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2021</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">16</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">35</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">14</span> GMT<span style="color: rgba(128, 128, 128, 1)">+</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0800</span><span style="color: rgba(0, 0, 0, 1)"> (CST)",
      "now" : "Fri Jan </span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">29</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">2021</span> <span style="color: rgba(128, 0, 0, 1); font-weight: bold">16</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">35</span>:<span style="color: rgba(128, 0, 0, 1); font-weight: bold">14</span> GMT<span style="color: rgba(128, 128, 128, 1)">+</span><span style="color: rgba(128, 0, 0, 1); font-weight: bold">0800</span><span style="color: rgba(0, 0, 0, 1)"> (CST)"
}</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(255, 0, 0, 1)">[</span><span style="color: rgba(255, 0, 0, 1)">work@xxx collection</span><span style="color: rgba(255, 0, 0, 1)">]</span>$ ll <span style="color: rgba(128, 128, 128, 1)">-</span><span style="color: rgba(0, 0, 0, 1)">h
total 304G
</span><span style="color: rgba(128, 128, 128, 1)">-</span>rw<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">----- 1 work work32K Nov 30 15:02 0-979332807111034247.wt</span>
<span style="color: rgba(128, 128, 128, 1)">-</span>rw<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">----- 1 work work 304G Jan 29 16:38 14-979332807111034247.wt</span>
<span style="color: rgba(128, 128, 128, 1)">-</span>rw<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">----- 1 work work32K Dec 15 12:24 17-979332807111034247.wt</span>
<span style="color: rgba(128, 128, 128, 1)">-</span>rw<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">----- 1 work work36K Nov 30 15:02 19-979332807111034247.wt</span>
<span style="color: rgba(128, 128, 128, 1)">-</span>rw<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">----- 1 work work16K Dec 15 12:24 2-979332807111034247.wt</span>
<span style="color: rgba(128, 128, 128, 1)">-</span>rw<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">----- 1 work work 320K Jan 29 15:30 40-979332807111034247.wt</span>
<span style="color: rgba(128, 128, 128, 1)">-</span>rw<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">----- 1 work work36K Jun 302020 4-979332807111034247.wt</span>
<span style="color: rgba(128, 128, 128, 1)">-</span>rw<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">----- 1 work work16K Dec 15 12:24 6-979332807111034247.wt</span>
<span style="color: rgba(128, 128, 128, 1)">-</span>rw<span style="color: rgba(0, 128, 128, 1)">--</span><span style="color: rgba(0, 128, 128, 1)">----- 1 work work16K Dec3 16:31 8-979332807111034247.wt</span></pre>
</div>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>####################################################</p>

</div>
<div id="MySignature" role="contentinfo">
    igoodful@qq.com<br><br>
来源:https://www.cnblogs.com/igoodful/p/13954888.html
頁: [1]
查看完整版本: mongodb的oplog日志