如何搭建mongodb架构Replica Set&Sharding—ttlsa
admin
2023-04-13 21:41:49
0

一分钟搭建mongodb架构Replica Set&Sharding—ttlsa

在测试试验阶段,我们需要有一个模拟的测试环境来测试应用程序和系统架构各个方面的功能,是否符合需求。在我公司,我常常使用下面的方法来为开发人员搭建mongodb的复制集和分片架构进行测试。我也常用这个方法来模拟线上架构,测试相关内容。
开启一个MongoDB shell,不连接任何mongod

?

1

# ./mongo --nodb

创建复制集,一个primary两个secondary

?

1

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

启动三个mongod实例

?

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'

复制集初始化

?

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"

}

]

}

}

启动了三个实例,分别监听在31000,31001,31002端口上

当前MongoDB shell窗口会有大量的日志信息输出,影响操作,另开启一个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是默认的复制集测试名

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

创建第二个连接,连接到secondary

?

1

2

3

4

5

6

> conn2 = newMongo("localhost:31001")

connection to localhost:31001

> secondaryDB = conn2.getDB("test")

test

> secondaryDB.coll.find()  //默认情况下secondary不可读不可写

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

允许secondary可读

?

尝试想secondary写数据

?

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不接收客户端写操作

测试复制集的automatic failover功能:
shutdown 31000实例

查看哪个实例变成primary

可见31002实例变成新的master

关闭replica set

sharding简易搭建方法参见: http://www.ttlsa.com/html/1787.html

相关内容

热门资讯

徐巧芯剖析赖清德:最怕自己丢掉... 海峡导报综合报道 大罢免投票一周年纪念活动26日举行,台北市长蒋万安出席并怒轰,民进党一年过后仍没有...
中国红十字会总会紧急向广东调拨... 记者从中国红十字会总会了解到,针对台风“红霞”给广东造成的灾情,中国红十字会总会启动四级应急响应,紧...
这些少年,正被“毒蛋”围猎 李梦抽了几口电子烟,吐出了白色的烟雾,有一股浓烈的水果香味。随后,她看着自己握电子烟的手,跟身边人说...
蒋万安号召“倒阁”,卢秀燕认为... 海峡导报综合报道 台北市长蒋万安27日抛出震撼议题,表示多名民代向他提议推动“倒阁”,要求台行政机构...
特朗普将在白宫会见两位重要客人 俄乌战争仍未结束,美伊冲突又有升级风险。在此背景下,美国总统特朗普将在白宫接见乌克兰和以色列领导人。...
新租客打扫卧室柜子上掉落4万元... 近日,莲前西路某小区内,张倩签下一套房子的租赁合同。当天上午,她带着12岁的大女儿周馨怡和11岁的小...
六部门通告:禁止涉军队退役报废... 关于禁止涉军队退役报废装备销售活动的通告近年来,部分经营者在线上线下公开销售涉及我军退役报废装备(指...
九寨沟景区发生泥石流,部分道路... 7月26日,九寨沟风景名胜区管理局发布泥石流灾害通报:2026年7月26日15时50分,九寨沟景区局...
空调26度不凉是什么原因 从空调内部元器件来说,不凉的原因有可能是温度传感器出现故障了,也有可能是空调压缩机或者铜管出现问题,...
鲸鸿动能携手香港国际机场、鸿蒙...   2026 年 7 月 25 日,十余位鸿蒙车主组成的车队从珠海出发,经港珠澳大桥通关抵达香港国际...