mongo在进行事务操作的时候必须开启副本集。同时只有在master才能进行正常的写入操作。因此,我们不仅仅需要开启副本集,同时要让当前节点作为master运行。
记录下单机mongo开启副本集的操作:
配置文件:
# mongod.conf
# for documentation of all options, see:
# http://docs.mongodb.org/manual/reference/configuration-options/
# Where and how to store data.
storage:
dbPath: /data/db
journal:
enabled: true
# engine:
# mmapv1:
# wiredTiger:
# where to write logging data.
systemLog:
destination: file
logAppend: true
path: /var/log/mongodb/mongod.log
# network interfaces
net:
port: 27017
# bindIp: 0.0.0.0
# how the process runs
processManagement:
timeZoneInfo: /usr/share/zoneinfo
#security:
#operationProfiling:
replication:
replSetName: rs0
#sharding:
## Enterprise-Only Options:
#auditLog:
#snmp:
这儿副本集为 rs0。然后进入mongo shell当中,初始化副本集,并且作为master运行。
===============
rs.initiate({_id:"rs0",members:[{_id:0,host:"127.0.0.1:27017",priority:1}]})
===============
注意: 事务处理不能新建集合,对于动态的生成的集合,可通过建立索引的方式避免因集合不存在导致事务报错的故障。