作者:容易 2013-11-01 15:07:29

    

    问题描述,mongodb reset 同步后,所有节点都处于在线状态,然后直接将备库数据库目录下的全部数据文件删除,再将备库关闭,随后重启。
重启后,由于备库的oplog没有被删除,备库会根据oplog最后的操作,去同步主库的数据,但是历史数据将会丢失。

环境如下
mongodb rs 一共三个节点,其中192.168.4.165为主节点
192.168.4.164 192.168.4.166 同为备用节点
一、第一步直接将192.168.4.164 节点scp数据库目录下的全部数据删除
cd scp/
rm -rf *
然后观察rs状态,显示一切OK
tigerdb:PRIMARY> rs.status();
{
        "set" : "tigerdb",
        "date" : ISODate("2013-11-01T05:12:20Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 13,
                        "name" : "192.168.4.166:8010",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 52839,
                        "optime" : Timestamp(1383282738000, 7495),
                        "optimeDate" : ISODate("2013-11-01T05:12:18Z"),
                        "lastHeartbeat" : ISODate("2013-11-01T05:12:18Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 14,
                        "name" : "192.168.4.164:8010",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 52839,
                        "optime" : Timestamp(1383282738000, 7482),
                        "optimeDate" : ISODate("2013-11-01T05:12:18Z"),
                        "lastHeartbeat" : ISODate("2013-11-01T05:12:18Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 16,
                        "name" : "192.168.4.165:8010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 54410,
                        "optime" : Timestamp(1383282740000, 2927),
                        "optimeDate" : ISODate("2013-11-01T05:12:20Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}
二、关闭192.168.4.164节点,观察rs状态
tigerdb:PRIMARY> rs.status();
{
        "set" : "tigerdb",
        "date" : ISODate("2013-11-01T05:15:37Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 13,
                        "name" : "192.168.4.166:8010",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 53036,
                        "optime" : Timestamp(1383282936000, 9153),
                        "optimeDate" : ISODate("2013-11-01T05:15:36Z"),
                        "lastHeartbeat" : ISODate("2013-11-01T05:15:36Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 14,
                        "name" : "192.168.4.164:8010",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : Timestamp(1383282894000, 5949),
                        "optimeDate" : ISODate("2013-11-01T05:14:54Z"),
                        "lastHeartbeat" : ISODate("2013-11-01T05:14:54Z"),
                        "pingMs" : 0,
                        "errmsg" : "socket exception [CONNECT_ERROR] for 192.168.4.164:8010"
                },
                {
                        "_id" : 16,
                        "name" : "192.168.4.165:8010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 54607,
                        "optime" : Timestamp(1383282937000, 3539),
                        "optimeDate" : ISODate("2013-11-01T05:15:37Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}

三、重启192.168.4.164节点,观察状态,一开始直接显示"stateStr" : "SECONDARY",
tigerdb:PRIMARY> rs.status();
{
        "set" : "tigerdb",
        "date" : ISODate("2013-11-01T05:15:56Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 13,
                        "name" : "192.168.4.166:8010",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 53055,
                        "optime" : Timestamp(1383282954000, 9182),
                        "optimeDate" : ISODate("2013-11-01T05:15:54Z"),
                        "lastHeartbeat" : ISODate("2013-11-01T05:15:54Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 14,
                        "name" : "192.168.4.164:8010",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 2,
                        "optime" : Timestamp(1383282895000, 6896),
                        "optimeDate" : ISODate("2013-11-01T05:14:55Z"),
                        "lastHeartbeat" : ISODate("2013-11-01T05:15:54Z"),
                        "pingMs" : 2
                },
                {
                        "_id" : 16,
                        "name" : "192.168.4.165:8010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 54626,
                        "optime" : Timestamp(1383282956000, 2049),
                        "optimeDate" : ISODate("2013-11-01T05:15:56Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}

过了会 开始重新同步数据,从测试结果看这里同步的数据只是数据文件删除后,oplog中还未写入的数据。
tigerdb:PRIMARY> rs.status();
{
        "set" : "tigerdb",
        "date" : ISODate("2013-11-01T05:15:59Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 13,
                        "name" : "192.168.4.166:8010",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 53058,
                        "optime" : Timestamp(1383282958000, 4298),
                        "optimeDate" : ISODate("2013-11-01T05:15:58Z"),
                        "lastHeartbeat" : ISODate("2013-11-01T05:15:58Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 14,
                        "name" : "192.168.4.164:8010",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 5,
                        "optime" : Timestamp(1383282895000, 6996),
                        "optimeDate" : ISODate("2013-11-01T05:14:55Z"),
                        "lastHeartbeat" : ISODate("2013-11-01T05:15:58Z"),
                        "pingMs" : 0,
                        "errmsg" : "syncing to: 192.168.4.166:8010"
                },
                {
                        "_id" : 16,
                        "name" : "192.168.4.165:8010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 54629,
                        "optime" : Timestamp(1383282959000, 5316),
                        "optimeDate" : ISODate("2013-11-01T05:15:59Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}

四、同步完成,比较主备库节点的数据
tigerdb:PRIMARY> rs.status();
{
        "set" : "tigerdb",
        "date" : ISODate("2013-11-01T05:20:30Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 13,
                        "name" : "192.168.4.166:8010",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 53329,
                        "optime" : Timestamp(1383283228000, 9972),
                        "optimeDate" : ISODate("2013-11-01T05:20:28Z"),
                        "lastHeartbeat" : ISODate("2013-11-01T05:20:28Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 14,
                        "name" : "192.168.4.164:8010",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 276,
                        "optime" : Timestamp(1383283227000, 9336),
                        "optimeDate" : ISODate("2013-11-01T05:20:27Z"),
                        "lastHeartbeat" : ISODate("2013-11-01T05:20:28Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 16,
                        "name" : "192.168.4.165:8010",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 54900,
                        "optime" : Timestamp(1383283230000, 1566),
                        "optimeDate" : ISODate("2013-11-01T05:20:30Z"),
                        "self" : true
                }
        ],
        "ok" : 1
}
tigerdb:SECONDARY> db.nagios.count();
5448909
tigerdb:PRIMARY> db.nagios.count();
68768294
tigerdb:PRIMARY> db.printSlaveReplicationInfo();
source:   192.168.4.166:8010
         syncedTo: Fri Nov 01 2013 13:21:20 GMT+0800 (CST)
                 = 2 secs ago (0hrs)
source:   192.168.4.164:8010
         syncedTo: Fri Nov 01 2013 13:21:20 GMT+0800 (CST)
                 = 2 secs ago (0hrs)
     
五、比较主备库数据文件的数量
备库
[mdb@mdb-164 scp]$ ls -lh
total 4.0G
-rw------- 1 mdb mdb  64M Nov  1 13:20 scp.0
-rw------- 1 mdb mdb 128M Nov  1 13:18 scp.1
-rw------- 1 mdb mdb 256M Nov  1 13:19 scp.2
-rw------- 1 mdb mdb 512M Nov  1 13:20 scp.3
-rw------- 1 mdb mdb 1.0G Nov  1 13:20 scp.4
-rw------- 1 mdb mdb 2.0G Nov  1 13:19 scp.5
-rw------- 1 mdb mdb  16M Nov  1 13:20 scp.ns

主库上的数据库文件 
[mdb@mdb-165 db]$ ls -l |grep scp
-rw------- 1 mdb mdb   67108864 Oct 31 23:24 scp.0
-rw------- 1 mdb mdb  134217728 Oct 31 22:38 scp.1
-rw------- 1 mdb mdb 2146435072 Nov  1 11:41 scp.10
-rw------- 1 mdb mdb 2146435072 Oct 31 22:53 scp.11
-rw------- 1 mdb mdb 2146435072 Nov  1 13:25 scp.12
-rw------- 1 mdb mdb 2146435072 Oct 29 15:15 scp.13
-rw------- 1 mdb mdb 2146435072 Nov  1 11:38 scp.14
-rw------- 1 mdb mdb 2146435072 Nov  1 11:49 scp.15
-rw------- 1 mdb mdb 2146435072 Nov  1 13:25 scp.16
-rw------- 1 mdb mdb 2146435072 Nov  1 12:01 scp.17
-rw------- 1 mdb mdb 2146435072 Nov  1 12:12 scp.18
-rw------- 1 mdb mdb 2146435072 Nov  1 12:24 scp.19
-rw------- 1 mdb mdb  268435456 Oct 31 22:38 scp.2
-rw------- 1 mdb mdb 2146435072 Nov  1 13:21 scp.20
-rw------- 1 mdb mdb 2146435072 Nov  1 13:25 scp.21
-rw------- 1 mdb mdb 2146435072 Nov  1 13:25 scp.22
-rw------- 1 mdb mdb 2146435072 Nov  1 13:22 scp.23
-rw------- 1 mdb mdb  536870912 Oct 31 22:40 scp.3
-rw------- 1 mdb mdb 1073741824 Oct 31 22:45 scp.4
-rw------- 1 mdb mdb 2146435072 Oct 31 22:52 scp.5
-rw------- 1 mdb mdb 2146435072 Oct 31 22:57 scp.6
-rw------- 1 mdb mdb 2146435072 Nov  1 13:19 scp.7
-rw------- 1 mdb mdb 2146435072 Nov  1 11:26 scp.8
-rw------- 1 mdb mdb 2146435072 Nov  1 11:48 scp.9
-rw------- 1 mdb mdb   16777216 Nov  1 13:25 scp.ns

测试过程中主库我只做了插入操作,备库没有做过其他操作。

 

 

One Response


    还没有评论!
1  

Leave your comment

请留下您的姓名(*)

请输入正确的邮箱地址(*)

请输入你的评论(*)


感谢开源 © 2016. All rights reserved.&3Q Open Source&^_^赣ICP备15012863号-1^_^
乐于分享共同进步 KreativeThemes