总结一下让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)

你可能感兴趣的:(总结一下让pymongo支持事务)