总结一下让pymongo支持事务

一. 升级 mongo 到 4.0 以上

安装mongodb 4.0 参考了这篇文章

步骤1:在终端输入GPK码

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 9DA31620334BD75D9DCB49F368818C72E52529D4

步骤2:添加mongoDB源
  • Ubuntu 18.04 LTS:
    echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list
  • Ubuntu 16.04 LTS:
    echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb.list
步骤3:安装 mongodb server

sudo apt update
sudo apt install mongodb-org (这种方法我没有安装成功,用的是下面的指定版本,成功了)
也可以直接安装指定版本的mongoDB
sudo apt install mongodb-org=4.0.1 mongodb-org-server=4.0.1 mongodb-org-shell=4.0.1 mongodb-org-mongos=4.0.1 mongodb-org-tools=4.0.1

步骤4: 开启服务

service mongod start
兼容性检查,进入mongo终端,输入
db.adminCommand( { getParameter: 1, featureCompatibilityVersion: 1 } )
如果版本号小于4.0, 如下:

"featureCompatibilityVersion" : {
        "version" : "3.6"
    }

则运行下面命令提高兼容性:
db.adminCommand( { setFeatureCompatibilityVersion: "4.0" } )
在此运行检查兼容性命令,会出现如下,表示成功。

"featureCompatibilityVersion" : {
        "version" : "4.0"
    }
步骤5:验证 mongoDB 版本

mongod --version
结果 :

db version v4.0.1 git version: 54f1582fc6eb01de4d4c42f26fc133e623f065fb
OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1604
    distarch: x86_64
    target_arch: x86_64

二. 创建副本集

数据库必须工作在副本集或者分片模式.单机模式是不支持事务的.

  • 如果之前开启了 mongo 服务,先关闭 service mongod stop
  • 如果27017端口还是被占用,那就杀死该进程 1. 查找进程号 lsof -i:27017 2. sudo kill -9 pid
  • 以下面这种方式启动 mongo 服务:mongod --port 27017 --dbpath /srv/mongodb/db0 --replSet rs0 --bind_ip 0.0.0.0 注意 /srv/mongodb/db0/ 文件夹要先手动创建
    运行上述命令后会出现以下:
root@XXX:~# mongod --port 27017 --dbpath /srv/mongodb/db0 --replSet rs0
2019-01-15T15:33:03.536+0800 I CONTROL  [main] Automatically disabling TLS 1.0, to force-enable TLS 1.0 specify --sslDisabledProtocols 'none'
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten] MongoDB starting : pid=22484 port=27017 dbpath=/srv/mongodb/db0 64-bit host=JXC
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten] db version v4.0.1
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten] git version: 54f1582fc6eb01de4d4c42f26fc133e623f065fb
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten] allocator: tcmalloc
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten] modules: none
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten] build environment:
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten]     distmod: ubuntu1604
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten]     distarch: x86_64
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten]     target_arch: x86_64
2019-01-15T15:33:03.542+0800 I CONTROL  [initandlisten] options: { net: { bindIp: "0.0.0.0", port: 27017 }, replication: { replSet: "rs0" }, storage: { dbPath: "/srv/mongodb/db0" } }
2019-01-15T15:33:03.543+0800 I STORAGE  [initandlisten] Detected data files in /srv/mongodb/db0 created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2019-01-15T15:33:03.543+0800 I STORAGE  [initandlisten] 
2019-01-15T15:33:03.543+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-01-15T15:33:03.543+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-01-15T15:33:03.543+0800 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=488M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),statistics_log=(wait=0),verbose=(recovery_progress),
2019-01-15T15:33:04.320+0800 I STORAGE  [initandlisten] WiredTiger message [1547537584:320685][22484:0x7f18251bfa00], txn-recover: Main recovery loop: starting at 9/5888
2019-01-15T15:33:04.429+0800 I STORAGE  [initandlisten] WiredTiger message [1547537584:429313][22484:0x7f18251bfa00], txn-recover: Recovering log 9 through 10
2019-01-15T15:33:04.497+0800 I STORAGE  [initandlisten] WiredTiger message [1547537584:497288][22484:0x7f18251bfa00], txn-recover: Recovering log 10 through 10
2019-01-15T15:33:04.556+0800 I STORAGE  [initandlisten] WiredTiger message [1547537584:556247][22484:0x7f18251bfa00], txn-recover: Set global recovery timestamp: 0
2019-01-15T15:33:04.587+0800 I RECOVERY [initandlisten] WiredTiger recoveryTimestamp. Ts: Timestamp(0, 0)
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] 
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] 
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] 
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] 
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 7856 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] 
2019-01-15T15:33:04.611+0800 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/srv/mongodb/db0/diagnostic.data'
2019-01-15T15:33:04.612+0800 I REPL     [initandlisten] Did not find local voted for document at startup.
2019-01-15T15:33:04.613+0800 I REPL     [initandlisten] Rollback ID is 1
2019-01-15T15:33:04.613+0800 I REPL     [initandlisten] Did not find local replica set configuration document at startup;  NoMatchingDocument: Did not find replica set configuration document in local.system.replset
2019-01-15T15:33:04.613+0800 I NETWORK  [initandlisten] waiting for connections on port 27017
2019-01-15T15:33:04.614+0800 I CONTROL  [LogicalSessionCacheRefresh] Sessions collection is not set up; waiting until next sessions refresh interval: Replication has not yet been configured
2019-01-15T15:33:04.614+0800 I CONTROL  [LogicalSessionCacheReap] Sessions collection is not set up; waiting until next sessions reap interval: Replication has not yet been configured

此时工作还没有完成,还不能使用 mongo 事务,需要进入 mongo 终端
mongo命令进入

root@XXX:~# mongo
MongoDB shell version v4.0.1
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 4.0.1
Server has startup warnings: 
2019-01-15T15:33:03.543+0800 I STORAGE  [initandlisten] 
2019-01-15T15:33:03.543+0800 I STORAGE  [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2019-01-15T15:33:03.543+0800 I STORAGE  [initandlisten] **          See http://dochub.mongodb.org/core/prodnotes-filesystem
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] 
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] ** WARNING: Access control is not enabled for the database.
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] **          Read and write access to data and configuration is unrestricted.
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] 
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] 
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] ** WARNING: /sys/kernel/mm/transparent_hugepage/defrag is 'always'.
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] **        We suggest setting it to 'never'
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] 
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] ** WARNING: soft rlimits too low. rlimits set to 7856 processes, 65535 files. Number of processes should be at least 32767.5 : 0.5 times number of files.
2019-01-15T15:33:04.599+0800 I CONTROL  [initandlisten] 
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).

The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.

To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
>

运行如下命令:
rs.initiate({ _id: "rs0", version: 1, members: [ { _id: 0, host: "mongo所在主机ip:27017" } ] })
结果:

{
    "ok" : 1,
    "operationTime" : Timestamp(1547539555, 1),
    "$clusterTime" : {
        "clusterTime" : Timestamp(1547539555, 1),
        "signature" : {
            "hash" : BinData(0,"AAAAAAAAAAAAAAAAAAAAAAAAAAA="),
            "keyId" : NumberLong(0)
        }
    }
}

如过以上所有过程都没问题,则可以用 pymongo 尝试事务操作

注意:事务操作不会自己建立 database 和 collection,这两个需要自己手动创建
且 pymongo 版本 3.7 以上
操作

  • 没有错误, t1 和 t2 都会插入成功
conns = pymongo.MongoClient("mongo服务ip", 27017)
t1 = conns["test"]['t1']
t2 = conns["test"]['t2']
with conns.start_session(causal_consistency=True) as session:
    """事物必须在session下执行,with保证了session的正常关闭"""
    with session.start_transaction():
        """一旦出现异常会自动调用session.abort_transaction()"""
        t1.insert_one(document={"city": "beijing"}, session=session)  # 注意多了session这个参数
        t2.insert_one(document={"city": "tianjin"}, session=session)
  • 出现错误,t1 和 t2 都不会插入成功
conns = pymongo.MongoClient("mongo服务ip", 27017)
t1 = conns["test"]['t1']
t2 = conns["test"]['t2']
with conns.start_session(causal_consistency=True) as session:
    """事物必须在session下执行,with保证了session的正常关闭"""
    with session.start_transaction():
        """一旦出现异常会自动调用session.abort_transaction()"""
        t1.insert_one(document={"city": "beijing"}, session=session)  # 注意多了session这个参数
        raise Exception("haha")  # 制造一个错误, t1和t2的插入都不会成功.
        t2.insert_one(document={"city": "tianjin"}, session=session)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 212,686评论 6 492
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 90,668评论 3 385
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 158,160评论 0 348
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 56,736评论 1 284
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 65,847评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,043评论 1 291
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,129评论 3 410
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 37,872评论 0 268
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,318评论 1 303
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 36,645评论 2 327
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,777评论 1 341
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,470评论 4 333
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,126评论 3 317
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,861评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,095评论 1 267
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 46,589评论 2 362
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 43,687评论 2 351