我试图了解mongodb读取语义。根据文档,除非您更改readpreferences,否则它始终是副本集中的主服务器。
我的设置: Linux VM:4GB RAM,1个COre,在同一个VM上运行3个mongo实例(我知道这是一个糟糕的设置!但这就是我现在从操作系统获得的方式)
当我从Java Driver运行以下mongo查询时,我只是监视内存使用情况,日志等。
这就是我所看到的:
ReadPreference.PrimaryPreferred:
顶
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
17874(primary) mongod 20 0 43.0g 1.3g 1.1g S 14.3 36.0 109:06.71 mongod
18048(secondary-1) mongod 20 0 42.7g 690m 88m S 0.3 18.0 28:45.09 mongod
18207(secondary-2) mongod 20 0 42.7g 641m 60m S 0.3 16.7 28:53.13 mongod
日志
主要-1
Fri Aug 2 21:35:39.416 [initandlisten] connection accepted from 10.10.1.57:54306 #11927 (40 connections now open)
Fri Aug 2 21:44:16.332 [initandlisten] connection accepted from 10.10.1.57:54376 #11963 (41 connections now open)
Fri Aug 2 21:44:16.348 [initandlisten] connection accepted from 10.10.1.57:54377 #11964 (42 connections now open)
Fri Aug 2 21:44:16.414 [initandlisten] connection accepted from 10.10.1.57:54380 #11965 (43 connections now open)
二次-1
Fri Aug 2 20:03:43.066 [conn11270] end connection 10.10.1.57:53547 (20 connections now open)
Fri Aug 2 20:03:43.066 [conn11271] end connection 10.10.1.57:53551 (19 connections now open)
Fri Aug 2 21:44:16.331 [initandlisten] connection accepted from 10.10.1.57:54374 #11776 (19 connections now open)
Fri Aug 2 21:44:16.350 [initandlisten] connection accepted from 10.10.1.57:54378 #11777 (20 connections now open)
二次-2
Fri Aug 2 20:03:43.065 [conn11271] end connection 10.10.1.57:53552 (20 connections now open)
Fri Aug 2 20:03:43.066 [conn11270] end connection 10.10.1.57:53549 (19 connections now open)
Fri Aug 2 21:44:16.331 [initandlisten] connection accepted from 10.10.1.57:54375 #11776 (19 connections now open)
Fri Aug 2 21:44:16.352 [initandlisten] connection accepted from 10.10.1.57:54379 #11777 (20 connections now open)
ReadPreference.Secondary:
顶
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
18048(secondary-1) mongod 20 0 42.7g 1.1g 517m S 7.3 29.2 29:28.00 mongod
17874(primary) mongod 20 0 43.0g 1.0g 777m S 0.3 26.7 112:57.05 mongod
18207(secondary-2) mongod 20 0 42.7g 617m 37m S 0.3 16.1 28:59.08 mongod
日志
==> mongod_rs0-0.log< ==
Fri Aug 2 22:19:01.056 [conn12118] end connection 10.10.10.201:52558 (34 connections now open)
Fri Aug 2 22:19:01.057 [initandlisten] connection accepted from 10.10.10.201:52564 #12120 (35 connections now open)
==> mongod_rs0-1.log< ==
Fri Aug 2 22:19:04.038 [conn11925] end connection 10.10.10.201:46443 (15 connections now open)
Fri Aug 2 22:19:04.039 [initandlisten] connection accepted from 10.10.10.201:46449 #11927 (16 connections now open)
==> mongod_rs0-2.log< ==
Fri Aug 2 22:19:04.050 [conn11925] end connection 10.10.10.201:37641 (14 connections now open)
Fri Aug 2 22:19:04.050 [initandlisten] connection accepted from 10.10.10.201:37647 #11927 (15 connections now open)
我的问题是:
答案 0 :(得分:1)
与主服务器和辅助服务器的连接可能甚至不是您的客户端连接和查询服务器。主服务器和辅助服务器需要保持彼此连接,以便a)主服务器上的写入可以在辅助服务器上复制,并且b)所有服务器都可以确保主服务器仍然可用。
例如,我现在已经在我的笔记本电脑上设置并运行了三个服务器副本,但没有连接到它或查询它的进程,并且主服务器上的日志显示:
[conn15939] end connection 127.0.0.1:50584 (6 connections now open)
[initandlisten] connection accepted from 127.0.0.1:50597 #15941 (7 connections now open)
在辅助设备上,我有一半的连接数。这是因为辅助服务器只需要连接到其主服务器,但主服务器将具有来自所有辅助服务器的连接。