溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何搭建mongodb架構(gòu)Replica Set&Sharding—ttlsa

發(fā)布時(shí)間:2021-09-26 14:43:57 來源:億速云 閱讀:158 作者:柒染 欄目:MongoDB數(shù)據(jù)庫

本篇文章給大家分享的是有關(guān)如何搭建mongodb架構(gòu)Replica Set&Sharding—ttlsa,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

一分鐘搭建mongodb架構(gòu)Replica Set&Sharding—ttlsa

在測試試驗(yàn)階段,我們需要有一個(gè)模擬的測試環(huán)境來測試應(yīng)用程序和系統(tǒng)架構(gòu)各個(gè)方面的功能,是否符合需求。在我公司,我常常使用下面的方法來為開發(fā)人員搭建mongodb的復(fù)制集和分片架構(gòu)進(jìn)行測試。我也常用這個(gè)方法來模擬線上架構(gòu),測試相關(guān)內(nèi)容。
開啟一個(gè)MongoDB shell,不連接任何mongod

?

1

# ./mongo --nodb

創(chuàng)建復(fù)制集,一個(gè)primary兩個(gè)secondary

?

1

> replicaSet = newReplSetTest({"nodes": 3})

啟動(dòng)三個(gè)mongod實(shí)例

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

> replicaSet.startSet()

ReplSetTest Starting Set

ReplSetTest n is: 0

ReplSetTest n: 0ports: [ 31000, 31001, 31002] 31000number

{

"useHostName": true,

"oplogSize": 40,

"keyFile": undefined,

"port": 31000,

"noprealloc": "",

"smallfiles": "",

"rest": "",

"replSet": "testReplSet",

"dbpath": "$set-$node",

"restart": undefined,

"pathOpts": {

"node": 0,

"set": "testReplSet"

}

}

ReplSetTest Starting....

Resetting db path '/data/db/testReplSet-0'

ReplSetTest n is: 1

ReplSetTest n: 1ports: [ 31000, 31001, 31002] 31001number

{

"useHostName": true,

"oplogSize": 40,

"keyFile": undefined,

"port": 31001,

"noprealloc": "",

"smallfiles": "",

"rest": "",

"replSet": "testReplSet",

"dbpath": "$set-$node",

"restart": undefined,

"pathOpts": {

"node": 1,

"set": "testReplSet"

}

}

ReplSetTest Starting....

Resetting db path '/data/db/testReplSet-1'

ReplSetTest n is: 2

ReplSetTest n: 2ports: [ 31000, 31001, 31002] 31002number

{

"useHostName": true,

"oplogSize": 40,

"keyFile": undefined,

"port": 31002,

"noprealloc": "",

"smallfiles": "",

"rest": "",

"replSet": "testReplSet",

"dbpath": "$set-$node",

"restart": undefined,

"pathOpts": {

"node": 2,

"set": "testReplSet"

}

}

ReplSetTest Starting....

Resetting db path '/data/db/testReplSet-2'

復(fù)制集初始化

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

> replicaSet.initiate()

{

"replSetInitiate": {

"_id": "testReplSet",

"members": [

{

"_id": 0,

"host": "nd0302012029:31000"

},

{

"_id": 1,

"host": "nd0302012029:31001"

},

{

"_id": 2,

"host": "nd0302012029:31002"

}

]

}

}

啟動(dòng)了三個(gè)實(shí)例,分別監(jiān)聽在31000,31001,31002端口上

當(dāng)前MongoDB shell窗口會(huì)有大量的日志信息輸出,影響操作,另開啟一個(gè)MongoDB shell

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

# ./mongo --nodb

> conn1 = newMongo("localhost:31000")

> primaryDB = conn1.getDB("test")  //testReplSet是默認(rèn)的復(fù)制集測試名

test

> primaryDB.isMaster()

{

"setName": "testReplSet",

"ismaster": true,

"secondary": false,

"hosts": [

"nd0302012029:31000",

"nd0302012029:31002",

"nd0302012029:31001"

],

"primary": "nd0302012029:31000",

"me": "nd0302012029:31000",

"maxBsonObjectSize": 16777216,

"maxMessageSizeBytes": 48000000,

"localTime": ISODate("2013-07-28T04:23:49.866Z"),

"ok": 1

}

插入1000條文檔

?

1

2

3

>for(i=0; i<<>1000; i++) { primaryDB.coll.insert({count: i}) }

> primaryDB.coll.count()

1000

創(chuàng)建第二個(gè)連接,連接到secondary

?

1

2

3

4

5

6

> conn2 = newMongo("localhost:31001")

connection to localhost:31001

> secondaryDB = conn2.getDB("test")

test

> secondaryDB.coll.find()  //默認(rèn)情況下secondary不可讀不可寫

error: { "$err": "not master and slaveOk=false", "code": 13435}

允許secondary可讀

?

嘗試想secondary寫數(shù)據(jù)

?

1

2

3

4

5

6

7

8

9

10

> secondaryDB.coll.insert({"count": 1001})

> secondaryDB.runCommand({"getLastError": 1})

{

"err": "not master",

"code": 10058,

"n": 0,

"lastOp": Timestamp(0, 0),

"connectionId": 75,

"ok": 1

}

可看到secondary不接收客戶端寫操作

測試復(fù)制集的automatic failover功能:
shutdown 31000實(shí)例

查看哪個(gè)實(shí)例變成primary

可見31002實(shí)例變成新的master

關(guān)閉replica set

sharding簡易搭建方法參見: http://www.ttlsa.com/html/1787.html

以上就是如何搭建mongodb架構(gòu)Replica Set&Sharding—ttlsa,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見到或用到的。希望你能通過這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI