热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

总结一下让pymongo支持事务

一.升级mongo到4.0以上安装mongodb4.0参考了这篇文章步骤1:在终端输入GPK码sudoapt-keyadv--keyserverhkp:keyserver.ubun

一. 升级 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,"#########="),
"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)

推荐阅读
  • 本文详细记录了在基于Debian的Deepin 20操作系统上安装MySQL 5.7的具体步骤,包括软件包的选择、依赖项的处理及远程访问权限的配置。 ... [详细]
  • 本文详细介绍如何使用arm-eabi-gdb调试Android平台上的C/C++程序。通过具体步骤和实用技巧,帮助开发者更高效地进行调试工作。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 本文详细解析了Python中的os和sys模块,介绍了它们的功能、常用方法及其在实际编程中的应用。 ... [详细]
  • 掌握远程执行Linux脚本和命令的技巧
    本文将详细介绍如何利用Python的Paramiko库实现远程执行Linux脚本和命令,帮助读者快速掌握这一实用技能。通过具体的示例和详尽的解释,让初学者也能轻松上手。 ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • 本文探讨如何设计一个安全的加密和验证算法,确保生成的密码具有高随机性和低重复率,并提供相应的验证机制。 ... [详细]
  • 网络攻防实战:从HTTP到HTTPS的演变
    本文通过一系列日记记录了从发现漏洞到逐步加强安全措施的过程,探讨了如何应对网络攻击并最终实现全面的安全防护。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • MongoDB集群配置:副本集与分片详解
    本文详细介绍了如何在MongoDB中配置副本集(Replica Sets)和分片(Sharding),并提供了具体的步骤和命令,帮助读者理解并实现高可用性和水平扩展的MongoDB集群。 ... [详细]
author-avatar
莪鈈稀罕rn
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有