PyMongo事务错误:事务编号仅在副本集成员或mongos上被允许 [英] PyMongo transaction error:Transaction numbers are only allowed on a replica set member or mongos

查看:291
本文介绍了PyMongo事务错误:事务编号仅在副本集成员或mongos上被允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用pymongo 3.7事务功能连接到mongo服务器4.0时,发生此错误仅允许在副本集成员或mongos上使用交易号",而我找不到任何解决此问题的答案. 我的代码是:

When I am using pymongo 3.7 transaction function to connect to mongo server 4.0, this error "Transaction numbers are only allowed on a replica set member or mongos" occurred and I cannot find any answer to solve this problem. My code is:

from pymongo import MongoClient
conn = MongoClient(host, port)
tb = conn.collector_gateway.try_table
with conn.start_session() as session:
    with session.start_transaction():
        tb.insert_one({"sku": "abc123", "qty": 100}, session=session)

错误是:

Traceback (most recent call last):
  File "/Users/yuzgu/PycharmProjects/seller_loss_alert/try_mongo.py", line 22, in <module>
    tb.insert_one({"sku": "abc123", "qty": 100}, session=session)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/collection.py", line 693, in insert_one
    session=session),
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/collection.py", line 607, in _insert
    bypass_doc_val, session)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/collection.py", line 595, in _insert_one
    acknowledged, _insert_command, session)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1243, in _retryable_write
    return self._retry_with_session(retryable, func, s, None)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/mongo_client.py", line 1196, in _retry_with_session
    return func(session, sock_info, retryable)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/collection.py", line 590, in _insert_command
    retryable_write=retryable_write)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/pool.py", line 579, in command
    unacknowledged=unacknowledged)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/network.py", line 150, in command
    parse_write_concern_error=parse_write_concern_error)
  File "/Users/yuzgu/anaconda3/lib/python3.6/site-packages/pymongo/helpers.py", line 155, in _check_command_response
    raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Transaction numbers are only allowed on a replica set member or mongos

自从mongo交易功能于今年发布以来,我找不到解决方案,有人可以帮助我解决这个问题吗?

Since mongo transaction function was published this year, I couldn't find the solution, can anyone help me with this problem?

推荐答案

事务仅在副本集设置中可用(

Transactions are only available in a replica set setup (https://docs.mongodb.com/master/core/transactions/#transactions-and-replica-sets):

多文档交易仅适用于副本集.

Multi-document transactions are available for replica sets only.

当您尝试在独立的mongod实例上执行事务操作时,会显示此错误消息.

This error message is shown when you are attempting to do a transactional operation on a standalone mongod instance.

有关更多详细信息,请参见交易页.要求.

Please see Transactions page for more details & requirements.

这篇关于PyMongo事务错误:事务编号仅在副本集成员或mongos上被允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆