pymongo connect to replica set(HA)

stackoverflow question
mongo doc read preference
Connecting to a Replica Set
The initial connection as made above is a special case for an uninitialized replica set. Normally we’ll want to connect differently. A connection to a replica set can be made using the MongoClient()
constructor, specifying one or more members of the set, along with the replica set name. Any of the following connects to the replica set we just created:

>>> MongoClient('localhost', replicaset='foo')
MongoClient('localhost', 27017)
>>> MongoClient('localhost:27018', replicaset='foo')
MongoClient('localhost', 27018)
>>> MongoClient('localhost', 27019, replicaset='foo')
MongoClient('localhost', 27019)
>>> MongoClient('mongodb://localhost:27017,localhost:27018/?replicaSet=foo')
MongoClient(['localhost:27017', 'localhost:27018'])

The addresses passed to MongoClient()
are called the seeds. As long as at least one of the seeds is online, MongoClient discovers all the members in the replica set, and determines which is the current primary and which are secondaries or arbiters.

Secondary Reads
By default an instance of MongoClient sends queries to the primary member of the replica set. To use secondaries for queries we have to change the read preference:

>>> client = MongoClient(...  'localhost:27017',...  replicaSet='foo',...  readPreference='secondaryPreferred')
>>> client.read_preferenceSecondaryPreferred(tag_sets=None)

Now all queries will be sent to the secondary members of the set. If there are no secondary members the primary will be used as a fallback. If you have queries you would prefer to never send to the primary you can specify that using the secondary
read preference.
By default the read preference of a Database
is inherited from its MongoClient, and the read preference of a Collection
is inherited from its Database. To use a different read preference use the get_database()
method, or the get_collection()
method:

>>> from pymongo import ReadPreference
>>> client.read_preferenceSecondaryPreferred(tag_sets=None)
>>> db =client.get_database('test',read_preference=ReadPreference.SECONDARY)
>>> db.read_preferenceSecondary(tag_sets=None)
>>> coll = db.get_collection('test', read_preference=ReadPreference.PRIMARY)
>>> coll.read_preferencePrimary()

You can also change the read preference of an existing Collection
with the with_options()
method:

>>> coll2 = coll.with_options(read_preference=ReadPreference.NEAREST)
>>> coll.read_preferencePrimary()
>>> coll2.read_preferenceNearest(tag_sets=None)

Note that since most database commands can only be sent to the primary of a replica set, the command()
method does not obey the Database’s read_preference
, but you can pass an explicit read preference to the method:

>>> db.command('dbstats', read_preference=ReadPreference.NEAREST){...}

Reads are configured using three options: read preference, tag sets, and local threshold.
Read preference:
Read preference is configured using one of the classes from read_preferences
(Primary
, PrimaryPreferred
, Secondary
, SecondaryPreferred
, or Nearest
). For convenience, we also provide ReadPreference
with the following attributes:
PRIMARY
: Read from the primary. This is the default read preference, and provides the strongest consistency. If no primary is available, raise AutoReconnect
.
PRIMARY_PREFERRED
: Read from the primary if available, otherwise read from a secondary.
SECONDARY
: Read from a secondary. If no matching secondary is available, raise AutoReconnect
.
SECONDARY_PREFERRED
: Read from a secondary if available, otherwise from the primary.
NEAREST
: Read from any available member.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末招刨,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子涯雅,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評(píng)論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件浦旱,死亡現(xiàn)場(chǎng)離奇詭異张症,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)讶请,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門祷嘶,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人夺溢,你說我怎么就攤上這事论巍。” “怎么了风响?”我有些...
    開封第一講書人閱讀 163,359評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵嘉汰,是天一觀的道長。 經(jīng)常有香客問我状勤,道長鞋怀,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,309評(píng)論 1 292
  • 正文 為了忘掉前任荧降,我火速辦了婚禮接箫,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘朵诫。我一直安慰自己辛友,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,346評(píng)論 6 390
  • 文/花漫 我一把揭開白布剪返。 她就那樣靜靜地躺著废累,像睡著了一般。 火紅的嫁衣襯著肌膚如雪脱盲。 梳的紋絲不亂的頭發(fā)上邑滨,一...
    開封第一講書人閱讀 51,258評(píng)論 1 300
  • 那天,我揣著相機(jī)與錄音钱反,去河邊找鬼掖看。 笑死匣距,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的哎壳。 我是一名探鬼主播毅待,決...
    沈念sama閱讀 40,122評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼归榕!你這毒婦竟也來了尸红?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,970評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤刹泄,失蹤者是張志新(化名)和其女友劉穎外里,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體特石,經(jīng)...
    沈念sama閱讀 45,403評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡盅蝗,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,596評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了县匠。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片风科。...
    茶點(diǎn)故事閱讀 39,769評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖乞旦,靈堂內(nèi)的尸體忽然破棺而出贼穆,到底是詐尸還是另有隱情,我是刑警寧澤兰粉,帶...
    沈念sama閱讀 35,464評(píng)論 5 344
  • 正文 年R本政府宣布故痊,位于F島的核電站,受9級(jí)特大地震影響玖姑,放射性物質(zhì)發(fā)生泄漏愕秫。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,075評(píng)論 3 327
  • 文/蒙蒙 一焰络、第九天 我趴在偏房一處隱蔽的房頂上張望戴甩。 院中可真熱鬧,春花似錦闪彼、人聲如沸甜孤。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽缴川。三九已至,卻和暖如春描馅,著一層夾襖步出監(jiān)牢的瞬間把夸,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評(píng)論 1 269
  • 我被黑心中介騙來泰國打工铭污, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留恋日,地道東北人膀篮。 一個(gè)月前我還...
    沈念sama閱讀 47,831評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像谚鄙,于是被迫代替她去往敵國和親各拷。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,678評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容