M102: MongoDB for DBAs chapter 4 replication学习记录

M102: MongoDB for DBAs chapter 4 replication

运行环境

操作系统:windows 10 家庭中文版
Mongodb :Mongodb 3.4

Mongodb安装路径:E:>MongoDB\Server\3.4\bin\
Mongodb存储路径:E:>MongoDB\data

课后习题

4.1题目

Download Handouts:

replication.js

In this chapter’s homework we will create a replica set and add some data to it.

  1. Download the replication.js handout.

  2. We will create a three member replica set. Pick a root working directory to work in. Go to that directory in a console window.

Given we will have three members in the set, and three mongod processes, create three data directories:

mkdir 1
mkdir 2
mkdir 3
  1. We will now start a single mongod as a standalone server. Given that we will have three mongod processes on our single test server, we will explicitly specify the port numbers (this wouldn’t be necessary if we had three real machines or three virtual machines). We’ll also use the –smallfiles parameter and –oplogSize so the files are small given we have a lot of server processes running on our test PC.
# starting as a standalone server for problem 1:
mongod --dbpath 1 --port 27001 --smallfiles --oplogSize 50

Note: for all mongod startups in the homework this chapter, you can optionally use –logPath, –logappend, and –fork. Or, since this is just an exercise on a local PC, you could simply have a separate terminal window for all and forgo those settings. Run “mongod –help” for more info on those.

  1. In a separate terminal window (cmd.exe on Windows), run the mongo shell with the replication.js file:
mongo --port 27001 --shell replication.js

Then run in the shell:

homework.init()

This will load a small amount of test data into the database.

Now run:

homework.a()

and enter the result. This will simply confirm all the above happened ok.

解答

按题目要求下载了replication.js文件到路径E:\MongoDB\m201\chapter_4_replication

在data目录下创建3个空文件夹:

C:\Users\Shinelon>e:

E:\>cd MongoDB

E:\MongoDB>mkdir data\1

E:\MongoDB>mkdir data\2

E:\MongoDB>mkdir data\3

E:\MongoDB>dir data
 驱动器 E 中的卷是 tools
 卷的序列号是 7C2B-B6D8

 E:\MongoDB\data 的目录

2018/04/12  14:10    <DIR>          .
2018/04/12  14:10    <DIR>          ..
2018/04/12  14:10    <DIR>          1
2018/04/12  14:10    <DIR>          2
2018/04/12  14:10    <DIR>          3

按要求制定dbpath为创建的1文件夹,启动mongod服务:

E:\MongoDB>Server\3.4\bin\mongod.exe --dbpath data\1 --port 27001 --smallfiles --oplogSize 50
2018-04-11T23:12:34.999-0700 I CONTROL  [initandlisten] MongoDB starting : pid=8648 port=27001 dbpath=data\1 64-bit host=DESKTOP-MP9NVQ7
2018-04-11T23:12:34.999-0700 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-04-11T23:12:34.999-0700 I CONTROL  [initandlisten] db version v3.4.6
2018-04-11T23:12:34.999-0700 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2018-04-11T23:12:34.999-0700 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips  22 Sep 2016
2018-04-11T23:12:34.999-0700 I CONTROL  [initandlisten] allocator: tcmalloc
2018-04-11T23:12:35.000-0700 I CONTROL  [initandlisten] modules: none
2018-04-11T23:12:35.000-0700 I CONTROL  [initandlisten] build environment:
2018-04-11T23:12:35.000-0700 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2018-04-11T23:12:35.001-0700 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-11T23:12:35.001-0700 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-11T23:12:35.001-0700 I CONTROL  [initandlisten] options: { net: { port: 27001 }, replication: { oplogSizeMB: 50 }, storage: { dbPath: "data\1", mmapv1: { smallFiles: true } } }
2018-04-11T23:12:35.007-0700 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7656M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2018-04-11T23:12:35.684-0700 W STORAGE  [initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
2018-04-11T23:12:35.684-0700 I CONTROL  [initandlisten]
2018-04-11T23:12:35.685-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:12:35.685-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:12:35.686-0700 I CONTROL  [initandlisten]
2018-04-12T14:12:36.052+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'data/1/diagnostic.data'
2018-04-12T14:12:36.248+0800 I INDEX    [initandlisten] build index on: admin.system.version properties: { v: 2, key: { version: 1 }, name: "incompatible_with_version_32", ns: "admin.system.version" }
2018-04-12T14:12:36.248+0800 I INDEX    [initandlisten]          building index using bulk method; build may temporarily use up to 500 megabytes of RAM
2018-04-12T14:12:36.262+0800 I INDEX    [initandlisten] build index done.  scanned 0 total records. 0 secs
2018-04-12T14:12:36.265+0800 I COMMAND  [initandlisten] setting featureCompatibilityVersion to 3.4
2018-04-12T14:12:36.268+0800 I NETWORK  [thread1] waiting for connections on port 27001

开启新的客户端,按要求导入replication.js文件:

C:\Users\Shinelon>e:

E:\>MongoDB\Server\3.4\bin\mongo.exe --port 27001 --shell MongoDB\m201\chapter_4_replication\replication.js
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27001/
MongoDB server version: 3.4.6
type "help" for help
Server has startup warnings:
2018-04-11T23:12:35.684-0700 I CONTROL  [initandlisten]
2018-04-11T23:12:35.685-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:12:35.685-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:12:35.686-0700 I CONTROL  [initandlisten]
>

初始化homework包:

> homework.init()
ok

执行输出程序:

> homework.a()
5001

答案为5001

4.2题目

Now convert the mongod instance (the one in the problem 4.1 above, which uses “–dbpath 1”) to a single server replica set. To do this, you’ll need to stop the mongod (NOT the mongo shell instance) and restart it with “–replSet” on its command line. Give the set any name you like.

Then go to the mongo shell. Once there, run

rs.initiate()

Note: if you do not specify a configuration, the mongod will pick one based on your computer’s hostname.

When you first ran homework.init(), we loaded some data into the mongod. You should see it in the replication database. You can confirm with:

use replication
db.foo.find()

Once done with that, run

homework.b()

in the mongo shell and enter that result below.

解答

按要求使用ctrl+c强制关闭了mongod服务:

2018-04-12T14:22:25.799+0800 I CONTROL  [thread2] Ctrl-C signal
2018-04-12T14:22:25.799+0800 I CONTROL  [consoleTerminate] got CTRL_C_EVENT, will terminate after current cmd ends
2018-04-12T14:22:25.800+0800 I NETWORK  [consoleTerminate] shutdown: going to close listening sockets...
2018-04-12T14:22:25.800+0800 I NETWORK  [consoleTerminate] closing listening socket: 660
2018-04-12T14:22:25.801+0800 I NETWORK  [consoleTerminate] shutdown: going to flush diaglog...
2018-04-12T14:22:25.801+0800 I FTDC     [consoleTerminate] Shutting down full-time diagnostic data capture
2018-04-12T14:22:25.805+0800 I STORAGE  [consoleTerminate] WiredTigerKVEngine shutting down
2018-04-12T14:22:26.140+0800 I STORAGE  [consoleTerminate] shutdown: removing fs lock...
2018-04-12T14:22:26.143+0800 I CONTROL  [consoleTerminate] now exiting
2018-04-12T14:22:26.143+0800 I CONTROL  [consoleTerminate] shutting down with code:12

以replSet模式再次启动:

E:\MongoDB>Server\3.4\bin\mongod.exe --replSet sbc --dbpath data\1 --port 27001 --smallfiles --oplogSize 50
2018-04-11T23:23:21.650-0700 I CONTROL  [initandlisten] MongoDB starting : pid=1176 port=27001 dbpath=data\1 64-bit host=DESKTOP-MP9NVQ7
2018-04-11T23:23:21.650-0700 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-04-11T23:23:21.651-0700 I CONTROL  [initandlisten] db version v3.4.6
2018-04-11T23:23:21.651-0700 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2018-04-11T23:23:21.652-0700 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips  22 Sep 2016
2018-04-11T23:23:21.652-0700 I CONTROL  [initandlisten] allocator: tcmalloc
2018-04-11T23:23:21.652-0700 I CONTROL  [initandlisten] modules: none
2018-04-11T23:23:21.653-0700 I CONTROL  [initandlisten] build environment:
2018-04-11T23:23:21.655-0700 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2018-04-11T23:23:21.655-0700 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-11T23:23:21.655-0700 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-11T23:23:21.656-0700 I CONTROL  [initandlisten] options: { net: { port: 27001 }, replication: { oplogSizeMB: 50, replSet: "sbc" }, storage: { dbPath: "data\1", mmapv1: { smallFiles: true } } }
2018-04-11T23:23:21.661-0700 I -        [initandlisten] Detected data files in data\1 created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2018-04-11T23:23:21.662-0700 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7656M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2018-04-11T23:23:22.603-0700 W STORAGE  [initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
2018-04-11T23:23:22.603-0700 I CONTROL  [initandlisten]
2018-04-11T23:23:22.604-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:23:22.604-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:23:22.604-0700 I CONTROL  [initandlisten]
2018-04-12T14:23:22.835+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'data/1/diagnostic.data'
2018-04-12T14:23:22.932+0800 I REPL     [initandlisten] Did not find local voted for document at startup.
2018-04-12T14:23:22.932+0800 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset
2018-04-12T14:23:22.935+0800 I NETWORK  [thread1] waiting for connections on port 27001

切换至mongo shell端,执行集群初始化命令:

> rs.initiate()
2018-04-12T14:25:06.651+0800 I NETWORK  [thread1] Socket say send() 远程主机强迫关闭了一个现有的连接。 127.0.0.1:27001
2018-04-12T14:25:06.652+0800 E QUERY    [thread1] Error: socket exception [SEND_ERROR] for 127.0.0.1:27001 :
DB.prototype._runCommandImpl@src/mongo/shell/db.js:114:16
DB.prototype.runCommand@src/mongo/shell/db.js:125:19
DB.prototype.adminCommand@src/mongo/shell/db.js:150:16
rs.initiate@src/mongo/shell/utils.js:1183:12
@(shell):1:1
2018-04-12T14:25:06.653+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27001 (127.0.0.1) failed
2018-04-12T14:25:06.656+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27001 (127.0.0.1) ok

由于之前强制关闭了mongod服务,mongo shell需要重新连接,可以看到,它又自动重连到了服务,再次执行初始化命令:

> rs.initiate()
{
        "info2" : "no configuration specified. Using a default configuration for the set",
        "me" : "DESKTOP-MP9NVQ7:27001",
        "ok" : 1
}
sbc:SECONDARY>

我没指定主库,,,这台机器变成了secondary。。。

sbc:SECONDARY> rs.status()
{
        "set" : "sbc",
        "date" : ISODate("2018-04-12T06:28:15.352Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1523514485, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1523514485, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1523514485, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "DESKTOP-MP9NVQ7:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 294,
                        "optime" : {
                                "ts" : Timestamp(1523514485, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-12T06:28:05Z"),
                        "electionTime" : Timestamp(1523514314, 1),
                        "electionDate" : ISODate("2018-04-12T06:25:14Z"),
                        "configVersion" : 1,
                        "self" : true
                }
        ],
        "ok" : 1
}

但是我在查询集群状态后发现集群只有一台机器,且为primary状态,而且我的本机又变为了primary,,分析原因是集群刚创建的时候会先寻找合适的机器成为主机,在找到之前全都是secondary。
根据题目要求切换至replication库,执行查询操作:

sbc:PRIMARY> use replication
switched to db replication
sbc:PRIMARY> db.foo.find()
{ "_id" : ObjectId("5acef9bb5de67ee66187dc80"), "x" : 0, "y" : 0.3582105050126244 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc81"), "x" : 1, "y" : 0.43605728051988435 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc82"), "x" : 2, "y" : 0.565868021074715 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc83"), "x" : 3, "y" : 0.5403273220327718 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc84"), "x" : 4, "y" : 0.8942272891417642 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc85"), "x" : 5, "y" : 0.4646784416548614 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc86"), "x" : 6, "y" : 0.44004010869580135 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc87"), "x" : 7, "y" : 0.8088682319556076 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc88"), "x" : 8, "y" : 0.34143666457026 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc89"), "x" : 9, "y" : 0.05647049094497625 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc8a"), "x" : 10, "y" : 0.5721979355163033 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc8b"), "x" : 11, "y" : 0.8350262220617638 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc8c"), "x" : 12, "y" : 0.6847601298962991 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc8d"), "x" : 13, "y" : 0.9727141119429477 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc8e"), "x" : 14, "y" : 0.05027466510916434 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc8f"), "x" : 15, "y" : 0.4578722204148413 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc90"), "x" : 16, "y" : 0.8634957285307043 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc91"), "x" : 17, "y" : 0.8405409356679713 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc92"), "x" : 18, "y" : 0.13154246486186127 }
{ "_id" : ObjectId("5acef9bb5de67ee66187dc93"), "x" : 19, "y" : 0.11509856935698048 }
Type "it" for more

执行homework操作,得出结果

sbc:PRIMARY> homework.b()
5002

答案为5002

4.3题目

Now add two more members to the set. Use the 2/ and 3/ directories we created in homework 4.1. Run those two mongod’s on ports 27002 and 27003 respectively (the exact numbers could be different).

Remember to use the same replica set name as you used for the first member.

You will need to add these two new members to your replica set, which will initially have only one member. In the shell running on the first member, you can see your replica set status with

rs.status()

Initially it will have just that first member. Connecting to the other members will involve using

rs.add()

For example,

rs.add("localhost:27002")

Note that ‘localhost’ almost certainly won’t work for you unless you have already set it as ‘localhost’ in the previous problem. If not, try using the name in the “members.name” field in the document you get by calling rs.status(), but remember to use the correct port!.

You’ll know it’s added when you see an

{ "ok" : 1 }

document.

Once a secondary has spun up, you can connect to it with a new mongo shell instance. Use

rs.slaveOk()

to let the shell know you’re OK with (potentially) stale data, and run some queries. You can also insert data on your primary and then read it out on your secondary.

Once you have two secondary servers, both of which have sync’d with the primary and are caught up, run (on your primary):

homework.c()

and enter the result below.

解答

按题目要求开启2个新窗口,分别启动2,3机器:

2号机器:

C:\Users\Shinelon>e:

E:\>\MongoDB\Server\3.4\bin\mongod.exe --replSet sbc --dbpath MongoDB\data\2 --port 27002 --smallfiles --oplogSize 50
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten] MongoDB starting : pid=14632 port=27002 dbpath=MongoDB\data\2 64-bit host=DESKTOP-MP9NVQ7
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten] db version v3.4.6
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips  22 Sep 2016
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten] allocator: tcmalloc
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten] modules: none
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten] build environment:
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-11T23:37:30.209-0700 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-11T23:37:30.210-0700 I CONTROL  [initandlisten] options: { net: { port: 27002 }, replication: { oplogSizeMB: 50, replSet: "sbc" }, storage: { dbPath: "MongoDB\data\2", mmapv1: { smallFiles: true } } }
2018-04-11T23:37:30.216-0700 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7656M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2018-04-11T23:37:30.703-0700 W STORAGE  [initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten]
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten]
2018-04-12T14:37:31.046+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'MongoDB/data/2/diagnostic.data'
2018-04-12T14:37:31.156+0800 I REPL     [initandlisten] Did not find local voted for document at startup.
2018-04-12T14:37:31.156+0800 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset
2018-04-12T14:37:31.158+0800 I NETWORK  [thread1] waiting for connections on port 27002

3号机器:

E:\>\MongoDB\Server\3.4\bin\mongod.exe --replSet sbc --dbpath MongoDB\data\3 --port 27003 --smallfiles --oplogSize 50
2018-04-11T23:39:03.020-0700 I CONTROL  [initandlisten] MongoDB starting : pid=16076 port=27003 dbpath=MongoDB\data\3 64-bit host=DESKTOP-MP9NVQ7
2018-04-11T23:39:03.020-0700 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2018-04-11T23:39:03.020-0700 I CONTROL  [initandlisten] db version v3.4.6
2018-04-11T23:39:03.020-0700 I CONTROL  [initandlisten] git version: c55eb86ef46ee7aede3b1e2a5d184a7df4bfb5b5
2018-04-11T23:39:03.020-0700 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1u-fips  22 Sep 2016
2018-04-11T23:39:03.020-0700 I CONTROL  [initandlisten] allocator: tcmalloc
2018-04-11T23:39:03.021-0700 I CONTROL  [initandlisten] modules: none
2018-04-11T23:39:03.021-0700 I CONTROL  [initandlisten] build environment:
2018-04-11T23:39:03.021-0700 I CONTROL  [initandlisten]     distmod: 2008plus-ssl
2018-04-11T23:39:03.021-0700 I CONTROL  [initandlisten]     distarch: x86_64
2018-04-11T23:39:03.021-0700 I CONTROL  [initandlisten]     target_arch: x86_64
2018-04-11T23:39:03.021-0700 I CONTROL  [initandlisten] options: { net: { port: 27003 }, replication: { oplogSizeMB: 50, replSet: "sbc" }, storage: { dbPath: "MongoDB\data\3", mmapv1: { smallFiles: true } } }
2018-04-11T23:39:03.027-0700 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=7656M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2018-04-11T23:39:03.670-0700 W STORAGE  [initandlisten] Detected configuration for non-active storage engine mmapv1 when current storage engine is wiredTiger
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten]
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten]
2018-04-12T14:39:04.074+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory 'MongoDB/data/3/diagnostic.data'
2018-04-12T14:39:04.235+0800 I REPL     [initandlisten] Did not find local voted for document at startup.
2018-04-12T14:39:04.235+0800 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset
2018-04-12T14:39:04.238+0800 I NETWORK  [thread1] waiting for connections on port 27003

在1号机的mongo shell查看集群状态:

sbc:PRIMARY> rs.status()
{
        "set" : "sbc",
        "date" : ISODate("2018-04-12T06:39:30.687Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1523515165, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1523515165, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1523515165, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "DESKTOP-MP9NVQ7:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 969,
                        "optime" : {
                                "ts" : Timestamp(1523515165, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-12T06:39:25Z"),
                        "electionTime" : Timestamp(1523514314, 1),
                        "electionDate" : ISODate("2018-04-12T06:25:14Z"),
                        "configVersion" : 1,
                        "self" : true
                }
        ],
        "ok" : 1
}

还是只有1号机器在集群中,且状态为PRIMARY。
强制将2,3号机加入集群:

sbc:PRIMARY> rs.add("localhost:27002")
{
        "ok" : 0,
        "errmsg" : "Either all host names in a replica set configuration must be localhost references, or none must be; found 1 out of 2",
        "code" : 103,
        "codeName" : "NewReplicaSetConfigurationIncompatible"
}

按题目使用localhost果然失败了,因为我没有给集群设定membername,只能使用rs.status()查到的members.name来加入集群:

sbc:PRIMARY> rs.add("DESKTOP-MP9NVQ7:27002")
{ "ok" : 1 }
sbc:PRIMARY> rs.add("DESKTOP-MP9NVQ7:27003")
{ "ok" : 1 }

再查看集群状态,2,3号机器已经加入集群:

sbc:PRIMARY> rs.status()
{
        "set" : "sbc",
        "date" : ISODate("2018-04-12T06:45:58.520Z"),
        "myState" : 1,
        "term" : NumberLong(1),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1523515555, 1),
                        "t" : NumberLong(1)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1523515555, 1),
                        "t" : NumberLong(1)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1523515555, 1),
                        "t" : NumberLong(1)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "DESKTOP-MP9NVQ7:27001",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1357,
                        "optime" : {
                                "ts" : Timestamp(1523515555, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-12T06:45:55Z"),
                        "electionTime" : Timestamp(1523514314, 1),
                        "electionDate" : ISODate("2018-04-12T06:25:14Z"),
                        "configVersion" : 3,
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "DESKTOP-MP9NVQ7:27002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 147,
                        "optime" : {
                                "ts" : Timestamp(1523515555, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1523515555, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-12T06:45:55Z"),
                        "optimeDurableDate" : ISODate("2018-04-12T06:45:55Z"),
                        "lastHeartbeat" : ISODate("2018-04-12T06:45:57.765Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-12T06:45:56.575Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "DESKTOP-MP9NVQ7:27001",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "DESKTOP-MP9NVQ7:27003",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 140,
                        "optime" : {
                                "ts" : Timestamp(1523515555, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1523515555, 1),
                                "t" : NumberLong(1)
                        },
                        "optimeDate" : ISODate("2018-04-12T06:45:55Z"),
                        "optimeDurableDate" : ISODate("2018-04-12T06:45:55Z"),
                        "lastHeartbeat" : ISODate("2018-04-12T06:45:57.837Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-12T06:45:57.640Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "DESKTOP-MP9NVQ7:27002",
                        "configVersion" : 3
                }
        ],
        "ok" : 1
}

分别连接至2,3号机器,开启可读:

2号机器:

E:\>MongoDB\Server\3.4\bin\mongo.exe --port 27002
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27002/
MongoDB server version: 3.4.6
Server has startup warnings:
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten]
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten]
sbc:SECONDARY> rs.slaveOk()
sbc:SECONDARY> exit
bye

3号机器:

E:\>MongoDB\Server\3.4\bin\mongo.exe --port 27003
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27003/
MongoDB server version: 3.4.6
Server has startup warnings:
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten]
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten]
sbc:SECONDARY> rs.slaveOk()
sbc:SECONDARY> exit
bye

切换回1号机器,执行homework命令:

E:\>MongoDB\Server\3.4\bin\mongo.exe --port 27001 --shell MongoDB\m201\chapter_4_replication\replication.js
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27001/
MongoDB server version: 3.4.6
type "help" for help
Server has startup warnings:
2018-04-11T23:23:22.603-0700 I CONTROL  [initandlisten]
2018-04-11T23:23:22.604-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:23:22.604-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:23:22.604-0700 I CONTROL  [initandlisten]
sbc:PRIMARY> homework.c()
5

4.4题目

We will now remove the first member (@ port 27001) from the set.

As a first step to doing this we will shut it down. (Given the rest of the set can maintain a majority, we can still do a majority reconfiguration if it is down.)

We could simply terminate its mongod process, but if we use the replSetStepDown command, the failover may be faster. That is a good practice, though not essential. Connect to member 1 (port 27001) in the shell and run:

rs.stepDown()

Then cleanly terminate the mongod process for member 1.

Next, go to the new primary of the set. You will probably need to connect with the mongo shell, which you’ll want to run with ‘–shell replication.js’ since we’ll be getting the homework solution from there. Once you are connected, run rs.status() to check that things are as you expect. Then reconfigure to remove member 1.

Tip: You can either use rs.reconfig() with your new configuration that does not contain the first member, or rs.remove(), specifying the host:port of the server you wish to remove as a string for the input.

When done, run:

> homework.d()

and enter the result.

Trouble-Shooting Tips

  • Make sure that your replica set has _id’s 0, 1, and 2 set. If you didn’t use a custom config, you should be fine, but this is an issue that has come up when using custom configurations.
  • If you ran the shell without replication.js on the command line, restart the shell with it.

解答

按题目要求,使用mongo shell进入27001端口,执行rs.stepDown():

sbc:PRIMARY> rs.stepDown()
2018-04-12T14:54:11.921+0800 E QUERY    [thread1] Error: error doing query: failed: network error while attempting to run command 'replSetStepDown' on host '127.0.0.1:27001'  :
DB.prototype.runCommand@src/mongo/shell/db.js:132:1
DB.prototype.adminCommand@src/mongo/shell/db.js:150:16
rs.stepDown@src/mongo/shell/utils.js:1261:12
@(shell):1:1
2018-04-12T14:54:11.922+0800 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27001 (127.0.0.1) failed
2018-04-12T14:54:11.925+0800 I NETWORK  [thread1] reconnect 127.0.0.1:27001 (127.0.0.1) ok
sbc:SECONDARY>

观察到1号机器断开了集群后重连,并变为SECONDARY状态

再强制关闭1号机的mongod服务:

2018-04-12T14:55:25.371+0800 I CONTROL  [thread2] Ctrl-C signal
2018-04-12T14:55:25.371+0800 I CONTROL  [consoleTerminate] got CTRL_C_EVENT, will terminate after current cmd ends
2018-04-12T14:55:25.372+0800 I NETWORK  [consoleTerminate] shutdown: going to close listening sockets...
2018-04-12T14:55:25.372+0800 I NETWORK  [consoleTerminate] closing listening socket: 652
2018-04-12T14:55:25.372+0800 I NETWORK  [consoleTerminate] shutdown: going to flush diaglog...
2018-04-12T14:55:25.372+0800 I REPL     [consoleTerminate] shutting down replication subsystems
2018-04-12T14:55:25.372+0800 I REPL     [consoleTerminate] Stopping replication reporter thread
2018-04-12T14:55:25.372+0800 I REPL     [SyncSourceFeedback] SyncSourceFeedback error sending update to DESKTOP-MP9NVQ7:27003: CallbackCanceled: Reporter no longer valid
2018-04-12T14:55:25.373+0800 I REPL     [consoleTerminate] Stopping replication fetcher thread
2018-04-12T14:55:25.373+0800 I REPL     [consoleTerminate] Stopping replication applier thread
2018-04-12T14:55:25.373+0800 I ASIO     [NetworkInterfaceASIO-RS-0] Ending connection to host DESKTOP-MP9NVQ7:27003 due to bad connection status; 1 connections to that host remain open
2018-04-12T14:55:25.373+0800 I REPL     [rsBackgroundSync] Replication producer stopped after oplog fetcher finished returning a batch from our sync source.  Abandoning this batch of oplog entries and re-evaluating our sync source.
2018-04-12T14:55:25.374+0800 I REPL     [consoleTerminate] Stopping replication storage threads
2018-04-12T14:55:25.374+0800 I FTDC     [consoleTerminate] Shutting down full-time diagnostic data capture
2018-04-12T14:55:25.378+0800 I STORAGE  [consoleTerminate] WiredTigerKVEngine shutting down
2018-04-12T14:55:25.737+0800 I STORAGE  [consoleTerminate] shutdown: removing fs lock...
2018-04-12T14:55:25.740+0800 I CONTROL  [consoleTerminate] now exiting
2018-04-12T14:55:25.741+0800 I CONTROL  [consoleTerminate] shutting down with code:12
2018-04-12T14:55:25.741+0800 I CONTROL  [initandlisten] shutting down with code:12

E:\MongoDB>

找出新的主机,先登入2号机器:

E:\>MongoDB\Server\3.4\bin\mongo.exe --port 27002
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27002/
MongoDB server version: 3.4.6
Server has startup warnings:
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten]
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten]
sbc:SECONDARY> exit
bye

2号机还是SECONDARY状态,查看3号机

E:\>MongoDB\Server\3.4\bin\mongo.exe --port 27003
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27003/
MongoDB server version: 3.4.6
Server has startup warnings:
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten]
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten]
sbc:PRIMARY>

果然此时3号机成了主机,查看集群状态:

sbc:PRIMARY> rs.status()
{
        "set" : "sbc",
        "date" : ISODate("2018-04-12T07:01:03.367Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1523516457, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1523516457, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1523516457, 1),
                        "t" : NumberLong(2)
                }
        },
        "members" : [
                {
                        "_id" : 0,
                        "name" : "DESKTOP-MP9NVQ7:27001",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(0, 0),
                                "t" : NumberLong(-1)
                        },
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "optimeDurableDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2018-04-12T07:01:00.224Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-12T06:55:24.421Z"),
                        "pingMs" : NumberLong(0),
                        "lastHeartbeatMessage" : "Couldn't get a connection within the time limit",
                        "configVersion" : -1
                },
                {
                        "_id" : 1,
                        "name" : "DESKTOP-MP9NVQ7:27002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1040,
                        "optime" : {
                                "ts" : Timestamp(1523516457, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1523516457, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2018-04-12T07:00:57Z"),
                        "optimeDurableDate" : ISODate("2018-04-12T07:00:57Z"),
                        "lastHeartbeat" : ISODate("2018-04-12T07:01:02.416Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-12T07:01:02.447Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "DESKTOP-MP9NVQ7:27003",
                        "configVersion" : 3
                },
                {
                        "_id" : 2,
                        "name" : "DESKTOP-MP9NVQ7:27003",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1321,
                        "optime" : {
                                "ts" : Timestamp(1523516457, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2018-04-12T07:00:57Z"),
                        "electionTime" : Timestamp(1523516062, 1),
                        "electionDate" : ISODate("2018-04-12T06:54:22Z"),
                        "configVersion" : 3,
                        "self" : true
                }
        ],
        "ok" : 1
}

可观察到:3号机为PRIMARY状态;2号机还是SECONDARY状态,而1号机变为了(not reachable/healthy)的状态

按题目要求在新的主机端运行replication.js:

sbc:PRIMARY> exit
bye

E:\>MongoDB\Server\3.4\bin\mongo.exe --port 27003 --shell MongoDB\m201\chapter_4_replication\replication.js
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27003/
MongoDB server version: 3.4.6
type "help" for help
Server has startup warnings:
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten]
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:39:03.670-0700 I CONTROL  [initandlisten]
sbc:PRIMARY>

从集群中去除掉1号机:

sbc:PRIMARY> rs.remove("DESKTOP-MP9NVQ7:27001")
{ "ok" : 1 }

再次查看集群状态,1号机已经被提出集群

sbc:PRIMARY> rs.status()
{
        "set" : "sbc",
        "date" : ISODate("2018-04-12T07:04:39.615Z"),
        "myState" : 1,
        "term" : NumberLong(2),
        "heartbeatIntervalMillis" : NumberLong(2000),
        "optimes" : {
                "lastCommittedOpTime" : {
                        "ts" : Timestamp(1523516674, 1),
                        "t" : NumberLong(2)
                },
                "appliedOpTime" : {
                        "ts" : Timestamp(1523516674, 1),
                        "t" : NumberLong(2)
                },
                "durableOpTime" : {
                        "ts" : Timestamp(1523516674, 1),
                        "t" : NumberLong(2)
                }
        },
        "members" : [
                {
                        "_id" : 1,
                        "name" : "DESKTOP-MP9NVQ7:27002",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 1256,
                        "optime" : {
                                "ts" : Timestamp(1523516674, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDurable" : {
                                "ts" : Timestamp(1523516674, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2018-04-12T07:04:34Z"),
                        "optimeDurableDate" : ISODate("2018-04-12T07:04:34Z"),
                        "lastHeartbeat" : ISODate("2018-04-12T07:04:38.532Z"),
                        "lastHeartbeatRecv" : ISODate("2018-04-12T07:04:39.535Z"),
                        "pingMs" : NumberLong(0),
                        "syncingTo" : "DESKTOP-MP9NVQ7:27003",
                        "configVersion" : 4
                },
                {
                        "_id" : 2,
                        "name" : "DESKTOP-MP9NVQ7:27003",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 1537,
                        "optime" : {
                                "ts" : Timestamp(1523516674, 1),
                                "t" : NumberLong(2)
                        },
                        "optimeDate" : ISODate("2018-04-12T07:04:34Z"),
                        "electionTime" : Timestamp(1523516062, 1),
                        "electionDate" : ISODate("2018-04-12T06:54:22Z"),
                        "configVersion" : 4,
                        "self" : true
                }
        ],
        "ok" : 1
}

执行homework脚本:

sbc:PRIMARY> homework.d()
6

答案为6

4.5题目

Note our replica set now has an even number of members, and that is not a best practice. However, to keep the homework from getting too long we’ll leave it at that for now, and instead do one more exercise below involving the oplog.

To get the right answer on this problem, you must perform the homework questions in order. Otherwise, your oplog may look different than we expect.

Go to the secondary in the replica set. The shell should say SECONDARY at the prompt if you’ve done everything correctly.

Switch to the local database and then look at the oplog:

db.oplog.rs.find()

If you get a blank result, you are not on the right database.

Note: as the local database doesn’t replicate, it will let you query it without entering “rs.slaveOk()” first.

Next look at the stats on the oplog to get a feel for its size:

db.oplog.rs.stats()

What result does this expression give when evaluated?

db.oplog.rs.find( { } ).sort( { $natural : 1 } ).limit( 1 ).next( ).o.msg[0]

Note that if you inserted many documents (more than around 500,000), your oplog will roll over and eliminate the document that you need. If this happens, you’ll need to repeat the previous problems with a set of clean directories in order to find the answer to this question.

解答

按题目要求退出PRIMARY库,进入状态为SECONDARY的2号机器:

sbc:PRIMARY> exit
bye

E:\>MongoDB\Server\3.4\bin\mongo.exe --port 27002
MongoDB shell version v3.4.6
connecting to: mongodb://127.0.0.1:27002/
MongoDB server version: 3.4.6
Server has startup warnings:
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten]
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2018-04-11T23:37:30.703-0700 I CONTROL  [initandlisten]
sbc:SECONDARY>

进入local库,查看日志记录:

sbc:SECONDARY> use local
switched to db local
sbc:SECONDARY> db.oplog.rs.find()
{ "ts" : Timestamp(1523515425, 1), "t" : NumberLong(1), "h" : NumberLong("-6814095527691060344"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 2 } }
{ "ts" : Timestamp(1523515435, 1), "t" : NumberLong(1), "h" : NumberLong("-900578718934088288"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 3 } }
{ "ts" : Timestamp(1523515445, 1), "t" : NumberLong(1), "h" : NumberLong("-1162960502745150755"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "new primary" } }
{ "ts" : Timestamp(1523515455, 1), "t" : NumberLong(1), "h" : NumberLong("7882621072990175059"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "Reconfig set", "version" : 4 } }
{ "ts" : Timestamp(1523515465, 1), "t" : NumberLong(1), "h" : NumberLong("1723736715869394795"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515475, 1), "t" : NumberLong(1), "h" : NumberLong("4831518412969116592"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515485, 1), "t" : NumberLong(1), "h" : NumberLong("-6710285134015392063"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515495, 1), "t" : NumberLong(1), "h" : NumberLong("1029270571368801115"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515505, 1), "t" : NumberLong(1), "h" : NumberLong("20866442716538045"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515515, 1), "t" : NumberLong(1), "h" : NumberLong("178516824812989108"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515525, 1), "t" : NumberLong(1), "h" : NumberLong("7529052157279726901"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515535, 1), "t" : NumberLong(1), "h" : NumberLong("3558595287538965021"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515545, 1), "t" : NumberLong(1), "h" : NumberLong("-4513822356115517152"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515555, 1), "t" : NumberLong(1), "h" : NumberLong("6385668779262409104"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515565, 1), "t" : NumberLong(1), "h" : NumberLong("1515644593702254762"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515575, 1), "t" : NumberLong(1), "h" : NumberLong("-1803933551760394551"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515585, 1), "t" : NumberLong(1), "h" : NumberLong("6378500156512798666"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515595, 1), "t" : NumberLong(1), "h" : NumberLong("-6697095153073154522"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515605, 1), "t" : NumberLong(1), "h" : NumberLong("-7417759811377033092"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
{ "ts" : Timestamp(1523515615, 1), "t" : NumberLong(1), "h" : NumberLong("9185851310830042742"), "v" : 2, "op" : "n", "ns" : "", "o" : { "msg" : "periodic noop" } }
Type "it" for more

按要求查看日志设置状态:

sbc:SECONDARY> db.oplog.rs.stats()
{
        "ns" : "local.oplog.rs",
        "size" : 15946,
        "count" : 166,
        "avgObjSize" : 96,
        "storageSize" : 36864,
        "capped" : true,
        "max" : -1,
        "maxSize" : 52428800,
        "sleepCount" : 0,
        "sleepMS" : 0,
        "wiredTiger" : {
                "metadata" : {
                        "formatVersion" : 1,
                        "oplogKeyExtractionVersion" : 1
                },
                "creationString" : "access_pattern_hint=none,allocation_size=4KB,app_metadata=(formatVersion=1,oplogKeyExtractionVersion=1),block_allocation=best,block_compressor=snappy,cache_resident=false,checksum=on,colgroups=,collator=,columns=,dictionary=0,encryption=(keyid=,name=),exclusive=false,extractor=,format=btree,huffman_key=,huffman_value=,ignore_in_memory_cache_size=false,immutable=false,internal_item_max=0,internal_key_max=0,internal_key_truncate=true,internal_page_max=4KB,key_format=q,key_gap=10,leaf_item_max=0,leaf_key_max=0,leaf_page_max=32KB,leaf_value_max=64MB,log=(enabled=true),lsm=(auto_throttle=true,bloom=true,bloom_bit_count=16,bloom_config=,bloom_hash_count=8,bloom_oldest=false,chunk_count_limit=0,chunk_max=5GB,chunk_size=10MB,merge_max=15,merge_min=0),memory_page_max=10m,os_cache_dirty_max=0,os_cache_max=0,prefix_compression=false,prefix_compression_min=4,source=,split_deepen_min_child=0,split_deepen_per_child=0,split_pct=90,type=file,value_format=u",
                "type" : "file",
                "uri" : "statistics:table:collection-12-6528043697247909312",
                "LSM" : {
                        "bloom filter false positives" : 0,
                        "bloom filter hits" : 0,
                        "bloom filter misses" : 0,
                        "bloom filter pages evicted from cache" : 0,
                        "bloom filter pages read into cache" : 0,
                        "bloom filters in the LSM tree" : 0,
                        "chunks in the LSM tree" : 0,
                        "highest merge generation in the LSM tree" : 0,
                        "queries that could have benefited from a Bloom filter that did not exist" : 0,
                        "sleep for LSM checkpoint throttle" : 0,
                        "sleep for LSM merge throttle" : 0,
                        "total size of bloom filters" : 0
                },
                "block-manager" : {
                        "allocations requiring file extension" : 8,
                        "blocks allocated" : 107,
                        "blocks freed" : 26,
                        "checkpoint size" : 4096,
                        "file allocation unit size" : 4096,
                        "file bytes available for reuse" : 16384,
                        "file magic number" : 120897,
                        "file major version number" : 1,
                        "file size in bytes" : 36864,
                        "minor version number" : 0
                },
                "btree" : {
                        "btree checkpoint generation" : 30,
                        "column-store fixed-size leaf pages" : 0,
                        "column-store internal pages" : 0,
                        "column-store variable-size RLE encoded values" : 0,
                        "column-store variable-size deleted values" : 0,
                        "column-store variable-size leaf pages" : 0,
                        "fixed-record size" : 0,
                        "maximum internal page key size" : 368,
                        "maximum internal page size" : 4096,
                        "maximum leaf page key size" : 2867,
                        "maximum leaf page size" : 32768,
                        "maximum leaf page value size" : 67108864,
                        "maximum tree depth" : 3,
                        "number of key/value pairs" : 0,
                        "overflow pages" : 0,
                        "pages rewritten by compaction" : 0,
                        "row-store internal pages" : 0,
                        "row-store leaf pages" : 0
                },
                "cache" : {
                        "bytes currently in the cache" : 30195,
                        "bytes read into cache" : 0,
                        "bytes written from cache" : 242917,
                        "checkpoint blocked page eviction" : 0,
                        "data source pages selected for eviction unable to be evicted" : 0,
                        "hazard pointer blocked page eviction" : 0,
                        "in-memory page passed criteria to be split" : 0,
                        "in-memory page splits" : 0,
                        "internal pages evicted" : 0,
                        "internal pages split during eviction" : 0,
                        "leaf pages split during eviction" : 0,
                        "modified pages evicted" : 0,
                        "overflow pages read into cache" : 0,
                        "overflow values cached in memory" : 0,
                        "page split during eviction deepened the tree" : 0,
                        "page written requiring lookaside records" : 0,
                        "pages read into cache" : 0,
                        "pages read into cache requiring lookaside entries" : 0,
                        "pages requested from the cache" : 393,
                        "pages written from cache" : 54,
                        "pages written requiring in-memory restoration" : 0,
                        "tracked dirty bytes in the cache" : 29849,
                        "unmodified pages evicted" : 0
                },
                "cache_walk" : {
                        "Average difference between current eviction generation when the page was last considered" : 0,
                        "Average on-disk page image size seen" : 0,
                        "Clean pages currently in cache" : 0,
                        "Current eviction generation" : 0,
                        "Dirty pages currently in cache" : 0,
                        "Entries in the root page" : 0,
                        "Internal pages currently in cache" : 0,
                        "Leaf pages currently in cache" : 0,
                        "Maximum difference between current eviction generation when the page was last considered" : 0,
                        "Maximum page size seen" : 0,
                        "Minimum on-disk page image size seen" : 0,
                        "On-disk page image sizes smaller than a single allocation unit" : 0,
                        "Pages created in memory and never written" : 0,
                        "Pages currently queued for eviction" : 0,
                        "Pages that could not be queued for eviction" : 0,
                        "Refs skipped during cache traversal" : 0,
                        "Size of the root page" : 0,
                        "Total number of pages currently in cache" : 0
                },
                "compression" : {
                        "compressed pages read" : 0,
                        "compressed pages written" : 21,
                        "page written failed to compress" : 0,
                        "page written was too small to compress" : 33,
                        "raw compression call failed, additional data available" : 0,
                        "raw compression call failed, no additional data available" : 0,
                        "raw compression call succeeded" : 0
                },
                "cursor" : {
                        "bulk-loaded cursor-insert calls" : 0,
                        "create calls" : 6,
                        "cursor-insert key and value bytes inserted" : 17440,
                        "cursor-remove key bytes removed" : 0,
                        "cursor-update value bytes updated" : 0,
                        "insert calls" : 166,
                        "next calls" : 387,
                        "prev calls" : 2,
                        "remove calls" : 0,
                        "reset calls" : 396,
                        "restarted searches" : 0,
                        "search calls" : 223,
                        "search near calls" : 1,
                        "truncate calls" : 0,
                        "update calls" : 0
                },
                "reconciliation" : {
                        "dictionary matches" : 0,
                        "fast-path pages deleted" : 0,
                        "internal page key bytes discarded using suffix compression" : 10,
                        "internal page multi-block writes" : 0,
                        "internal-page overflow keys" : 0,
                        "leaf page key bytes discarded using prefix compression" : 0,
                        "leaf page multi-block writes" : 0,
                        "leaf-page overflow keys" : 0,
                        "maximum blocks required for a page" : 0,
                        "overflow values written" : 0,
                        "page checksum matches" : 0,
                        "page reconciliation calls" : 54,
                        "page reconciliation calls for eviction" : 0,
                        "pages deleted" : 0
                },
                "session" : {
                        "object compaction" : 0,
                        "open cursor count" : 6
                },
                "transaction" : {
                        "update conflicts" : 0
                }
        },
        "nindexes" : 0,
        "totalIndexSize" : 0,
        "indexSizes" : {

        },
        "ok" : 1
}

执行所给出查询:

sbc:SECONDARY> db.oplog.rs.find( { } ).sort( { $natural : 1 } ).limit( 1 ).next( ).o.msg[0]
R

答案为R

你可能感兴趣的:(mongodb,Mongodb,University)