【MONGODB】驗證MONGODB 主從複製

來源:互聯網
上載者:User
1 實驗主從複製,並驗證複製成功
2 實驗複本集,並驗證自動切換primary成功.

=======================================================================
1.1.啟動master節點:
mongod -dbpath=D:\Program_file\mongo\db1 -logpath=D:\Program_file\mongo\log\mongodb1.log --port 18001 --master  --rest  --nojournal  


1.2.啟動slave1節點:
mongod -dbpath=D:\Program_file\mongo\db2 -logpath=D:\Program_file\mongo\log\mongodb2.log  --port 18002 --slave    --rest  --nojournal   --source localhost:18001

1.3.啟動slave2節點:
mongod -dbpath=D:\Program_file\mongo\db3 -logpath=D:\Program_file\mongo\log\mongodb2.log  --port 18003 --slave    --rest  --nojournal   --source localhost:18001



1.4.開啟主庫,建立立一個庫及表:


D:\Program_file\mongo\mongodb\bin>mongo -port 18001
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:18001/test
Server has startup warnings:
Thu Mar 13 11:36:06.084 [initandlisten]
Thu Mar 13 11:36:06.084 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Thu Mar 13 11:36:06.084 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Thu Mar 13 11:36:06.084 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Thu Mar 13 11:36:06.084 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Thu Mar 13 11:36:06.084 [initandlisten]
Thu Mar 13 11:36:06.192 [initandlisten]
Thu Mar 13 11:36:06.192 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.replset
Thu Mar 13 11:36:06.192 [initandlisten] **          Restart with --replSet unless you are doing maintenance and no other clients are connected.
Thu Mar 13 11:36:06.192 [initandlisten] **          The TTL collection monitor will not start because of this.
Thu Mar 13 11:36:06.192 [initandlisten] **          For more info see http://dochub.mongodb.org/core/ttlcollections
Thu Mar 13 11:36:06.192 [initandlisten]
> show dbs;
admin   0.0625GB
local   0.15625GB
maketion        0.0625GB
mydb    0.0625GB
> use test
switched to db test
> db.test.insert({_id:1,name:"name_test"});
> db.test.find()
{ "_id" : 1, "name" : "name_test" }



1.5.開啟從庫,查看建立立的資料庫及表有沒有同步:
D:\Program_file\mongo\mongodb\bin>mongo -port 18002
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:18002/test
Server has startup warnings:
Thu Mar 13 11:44:17.236 [initandlisten]
Thu Mar 13 11:44:17.236 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Thu Mar 13 11:44:17.236 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Thu Mar 13 11:44:17.236 [initandlisten] **       Note that journaling defaults to off for 32 bit and is currently off.
Thu Mar 13 11:44:17.236 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Thu Mar 13 11:44:17.236 [initandlisten]
Thu Mar 13 11:44:17.263 [initandlisten]
Thu Mar 13 11:44:17.263 [initandlisten] ** WARNING: mongod started without --replSet yet 1 documents are present in local.system.replset
Thu Mar 13 11:44:17.263 [initandlisten] **          Restart with --replSet unless you are doing maintenance and no other clients are connected
Thu Mar 13 11:44:17.263 [initandlisten] **          The TTL collection monitor will not start because of this.
Thu Mar 13 11:44:17.263 [initandlisten] **          For more info see http://dochub.mongodb.org/core/ttlcollections
Thu Mar 13 11:44:17.263 [initandlisten]
> show dbs;
admin   0.0625GB
local   0.09375GB
maketion        0.0625GB
mydb    0.0625GB
test    0.0625GB
> use test;
switched to db test
> show collections;
system.indexes
test
> db.test.find();
{ "_id" : 1, "name" : "name_test" }
>

可以看到,建立立一個庫TEST,表:TEST 及資料都已同步過來了。

-----------------------------------------------------------------------------------
2 實驗複本集,並驗證自動切換primary成功,抓圖實驗過程

下面配置為前段時間配置的一個 replset 測試環境:

2.1 建立相關目錄:
mkdir D:\Program_file\mongo\key
mkdir D:\Program_file\mongo\db1
mkdir D:\Program_file\mongo\db2
mkdir D:\Program_file\mongo\db3
mkdir D:\Program_file\mongo\db4



D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r0 -dbpath=D:\Program_file\mongo\db1 -logpath=D:\Program_file\mongo\log\mongodb.log --logappend  --journal --port 28010


D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r1 -dbpath=D:\Program_file\mongo\db1 -logpath=D:\Program_file\mongo\log\mongodb1.log --logappend  --journal --port 28011

D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r2 -dbpath=D:\Program_file\mongo\db2 -logpath=D:\Program_file\mongo\log\mongodb2.log --logappend  --journal --port 28012

D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r3 -dbpath=D:\Program_file\mongo\db3 -logpath=D:\Program_file\mongo\log\mongodb3.log --logappend  --journal --port 28013

--檔案拷貝後添加--fastsync 參數啟動(拷貝的如果不是primary,啟動後拷貝前的節點會變為primary)
D:\Program_file\mongo\mongodb\bin\mongod.exe --replSet rs1 --keyFile D:\Program_file\mongo\key\r4 -dbpath=D:\Program_file\mongo\db4 -logpath=D:\Program_file\mongo\log\mongodb4.log --logappend  --journal --port 28014 --fastsync


#share:

D:\Program_file\mongo\mongodb\bin\mongod.exe --shardsvr --port 20000 -dbpath=D:\Program_file\mongo\shard\shard0\db -logpath=D:\Program_file\mongo\shard\shard0\log\mongo.log --logappend --journal --directoryperdb

D:\Program_file\mongo\mongodb\bin\mongod.exe --shardsvr --port 20001 -dbpath=D:\Program_file\mongo\shard\shard1\db -logpath=D:\Program_file\mongo\shard\shard1\log\mongo.log --logappend --journal --directoryperdb

#config
D:\Program_file\mongo\mongodb\bin\mongod.exe --configsvr --port 30000 -dbpath=D:\Program_file\mongo\shard\config -logpath=D:\Program_file\mongo\shard\config\mongo.log --logappend --journal --directoryperdb

#route
D:\Program_file\mongo\mongodb\bin\mongos.exe  --port 40000 --configdb localhost:30000 --logpath=D:\Program_file\mongo\shard\config\route.log --logappend  --chunkSize 1

2.2 配置rs

config_rs1={_id:'rs1',members:[
{_id:0,host:'localhost:28010',priority:1},
{_id:0,host:'localhost:28011'},
{_id:0,host:'localhost:28012'},
{_id:0,host:'localhost:28013'},
{_id:0,host:'localhost:28014'}]};
rs.initiat(config_rs1);

2.3 關閉primary,

rs1:PRIMARY> use admin
rs1:PRIMARY> runCommand("shutdown");


2.4 #登入另一節點:

D:\Program_file\mongo\mongodb\bin>mongo -port 28011
MongoDB shell version: 2.4.8
connecting to: 127.0.0.1:28011/test
Server has startup warnings:
Thu Mar 13 13:55:23.541 [initandlisten]
Thu Mar 13 13:55:23.541 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
Thu Mar 13 13:55:23.541 [initandlisten] **       32 bit builds are limited to less than 2GB of data (or less with --journal).
Thu Mar 13 13:55:23.541 [initandlisten] **       See http://dochub.mongodb.org/core/32bit
Thu Mar 13 13:55:23.541 [initandlisten]
rs1:PRIMARY> rs.status()
{
        "set" : "rs1",
        "date" : ISODate("2014-03-13T05:58:52Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:28010",
                        "health" : 0,
                        "state" : 8,
                        "stateStr" : "(not reachable/healthy)",
                        "uptime" : 0,
                        "optime" : Timestamp(0, 0),
                        "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
                        "lastHeartbeat" : ISODate("2014-03-13T05:58:50Z"),
                        "lastHeartbeatRecv" : ISODate("1970-01-01T00:00:00Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 1,
                        "name" : "localhost:28011",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 209,
                        "optime" : Timestamp(1389939638, 1),
                        "optimeDate" : ISODate("2014-01-17T06:20:38Z"),
                        "self" : true
                },
                {
                        "_id" : 2,
                        "name" : "localhost:28012",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 197,
                        "optime" : Timestamp(1389939638, 1),
                        "optimeDate" : ISODate("2014-01-17T06:20:38Z"),
                        "lastHeartbeat" : ISODate("2014-03-13T05:58:51Z"),
                        "lastHeartbeatRecv" : ISODate("2014-03-13T05:58:51Z"),
                        "pingMs" : 0,
                        "syncingTo" : "localhost:28011"
                },
                {
                        "_id" : 3,
                        "name" : "localhost:28013",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 193,
                        "optime" : Timestamp(1389939638, 1),
                        "optimeDate" : ISODate("2014-01-17T06:20:38Z"),
                        "lastHeartbeat" : ISODate("2014-03-13T05:58:51Z"),
                        "lastHeartbeatRecv" : ISODate("2014-03-13T05:58:51Z"),
                        "pingMs" : 0,
                        "syncingTo" : "localhost:28011"
                }
        ],
        "ok" : 1
}
rs1:PRIMARY>
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.