日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【MONGODB】验证MONGODB 主从复制

發布時間:2024/4/15 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【MONGODB】验证MONGODB 主从复制 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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>

總結

以上是生活随笔為你收集整理的【MONGODB】验证MONGODB 主从复制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。