Skip to Content
MongoDB权威指南(第3版)
book

MongoDB权威指南(第3版)

by Shannon Bradshaw, Eoin Brazil, Kristina Chodorow
November 2021
Intermediate to advanced
410 pages
12h 1m
Chinese
Posts & Telecom Press
Content preview from MongoDB权威指南(第3版)
216
12
从应用程序连接副本集
本章介绍如何在应用程序中与副本集进行交互,包括:
如何连接到副本集以及故障转移的工作机制;
在进行写操作时等待复制;
将读请求路由到正确的成员。
12.1
 客户端到副本集的连接行为
MongoDB
的客户端开发库(也叫“驱动程序”)用于管理与
MongoDB
服务器端的通信,
无论服务器端是单机的
MongoDB
实例还是副本集
。对于副本集,默认情况下,驱动程序
会连接到主节点,并将所有流量都路由到此节点。应用程序可以像与单机服务器通信一样
执行读写操作,同时副本集会在后台悄悄地处理热备份。
连接副本集与连接单机服务器非常类似。在驱动程序中使用
MongoClient
类(或等价类),
并提供一个种子列表供驱动程序连接。种子列表就是服务器地址列表。
种子
是应用程序将
读取和写入数据的副本集成员。你不需要列出种子列表中的所有成员(尽管这样做也可
以)。当驱动程序连接到种子服务器时,它可以从其中发现其他成员。一个连接字符串通
常看起来像下面这样:
"mongodb://server-1:27017,server-2:27017,server-3:27017"
详情请参阅相关的驱动程序文档。
如果想提供更强的容错能力,那么也可以使用
DNS
子列表连接格式来指定应用程序连
接到副本集的方式。使用
DNS
的优点
是可以轮流更改
MongoDB
副本集成员所在的服务
器,而无须重新配置客户端(特指连接字符串)。
从应用程序连接副本集
217
所有
MongoDB
驱动程序都遵守服务器发现和监控(
SDAM ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

大数据项目管理:从规划到实现

大数据项目管理:从规划到实现

Ted Malaska, Jonathan Seidman
Go程序设计语言

Go程序设计语言

艾伦A. A.多诺万, 布莱恩W. 柯尼汉
Spark快速大数据分析(第2版)

Spark快速大数据分析(第2版)

Jules S. Damji, Brooke Wenig, Tathagata Das, Denny Lee
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty

Publisher Resources

ISBN: 9787115576538