简体   繁体   English

Mongo ReplicaSet解释

[英]Mongo ReplicaSet explain

i'm new in mongo replication. 我是mongo复制的新手。

I've got two server and i set a primary on one server and secondary to the other. 我有两个服务器,在一个服务器上设置一个主服务器,在另一个服务器上设置辅助服务器。

I'm developing a web application using spring and spring-data and i use that xml configuration to access to mongo: 我正在使用springspring-data开发一个Web应用程序,并且使用该xml配置访问mongo:

<mongo:mongo id="mongoConnection" replica-set="IP-Primary:27017,IP-Secondary:27017">
        <mongo:options
            connections-per-host="100"
            threads-allowed-to-block-for-connection-multiplier="50"
            connect-timeout="10000"
            max-wait-time="50000"
            slave-ok="true"
            auto-connect-retry="true"
            write-number="1"
            write-timeout="0"
            write-fsync="false"/>
</mongo:mongo>
<beans:bean id="mongoWriteConcern" class="com.mongodb.WriteConcern">
    <beans:constructor-arg type="int" name="w" value="2"/></beans:bean> 
<beans:bean id="mongoCredentials"
    class="org.springframework.data.authentication.UserCredentials">
    <beans:constructor-arg name="username" value="xxx" />
    <beans:constructor-arg name="password" value="yyy" />
</beans:bean>
<beans:bean id="myTemplate"
    class="org.springframework.data.mongodb.core.MongoTemplate">
    <beans:constructor-arg ref="mongoConnection" />
    <beans:constructor-arg name="databaseName" value="dbName" />
    <beans:constructor-arg name="userCredentials" ref="mongoHotelCredentials" />
    <beans:property name="writeConcern" ref="mongoWriteConcern"/>
</beans:bean>

Can please someone explain me what is the flow on the replica set? 能否请人解释一下副本集上的流程是什么?

It will write first on primary and automatically the primary write to secondary? 它将首先在主节点上写入,然后自动将主节点写入辅助节点吗?

Does the framework will check for success connection to each database and "elect" a primary if the current primary is unavailable ( so elect secondary as primary)? 如果当前的主数据库不可用,框架是否会检查与每个数据库的成功连接并“选出”一个主数据库(因此选择辅助数据库作为主数据库)? If yes what happen when primary come back again? 如果是,当主节点再次返回时会发生什么?

Thanks! 谢谢!

Basic flow of replication: 复制的基本流程:

Your application (client) writes to primary of the replica set. 您的应用程序(客户端)写入副本集的主副本。 Write operations are stored in a log file (oplog), and oplog replicates to secondaries either thru 'chained replication' (default) or replicates from closest member, configurable, read here http://docs.mongodb.org/manual/tutorial/manage-chained-replication/ . 写操作存储在日志文件(oplog)中,并且oplog通过“链式复制”(默认)或从最近的成员复制到辅助副本(可配置),请参见此处http://docs.mongodb.org/manual/tutorial/管理链接复制/

"It will write first on primary and automatically the primary write to secondary?" “它将首先在主数据库上写入,然后自动将主数据库写入辅助数据库吗?”

Writes go to primary, store in oplog, oplog replicates to secondary, then writes apply to secondary. 写入到主服务器,存储在oplog中,oplog复制到辅助服务器,然后写入应用于辅助服务器。 It is automatic, but you have to take network latency into consideration. 它是自动的,但是您必须考虑网络延迟。 Latency factors include distance between primary node and secondary node, and also barriers like firewall. 延迟因素包括主节点和辅助节点之间的距离,以及防火墙等障碍。 There are many things you can toy with MongoDB's replication, eg higher writeConcern setting to increase consistency (at cost of throughput), configure delayed secondary to act as backup, election priority for each secondary (useful for multi-datacenter), etc. 您可以通过MongoDB的复制进行很多操作,例如,更高的writeConcern设置以提高一致性(以吞吐量为代价),将延迟的辅助节点配置为充当备份,为每个辅助节点选择选举优先级(对多数据中心有用)等。

"Does the framework will check for success connection to each database and "elect" a primary if the current primary is unavailable ( so elect secondary as primary)? If yes what happen when primary come back again?" “如果当前的主数据库不可用,框架是否会检查与每个数据库的成功连接,并“选择”一个主数据库(因此选择辅助数据库作为主数据库?如果是,当主数据库再次出现时会发生什么?)

There is 'heartbeat' between members of a replica set. 副本集的成员之间存在“心跳”。 If a member goes down, within 10-30 seconds other members will detect the loss heartbeat and mark that member as unavailable. 如果某个成员掉线,则其他成员将在10-30秒内检测到丢失心跳,并将该成员标记为不可用。 If the down member is a primary, election process will occur and a new primary gets elected. 如果下成员是主要成员,则将进行选举过程,并选举出新的主要成员。 It is important to keep the 'odd' number of members (3, 5, 7... up to 12 members with 7 voting members) http://docs.mongodb.org/manual/core/replica-set-architecture-four-members/ 重要的是要保持“奇数”的成员数量(3、5、7 ...最多12个成员,而有7个投票成员) http://docs.mongodb.org/manual/core/replica-set-architecture-四人/

When that downed primary comes back it will become a secondary, with unprocessed operations (operations not applied to secondaries) stored in 'rollback' folder that have to be manually applied back to the database, or discarded. 当关闭的主服务器恢复正常后,它将成为辅助服务器,其中未处理的操作(未应用到辅助服务器的操作)存储在“ rollback”文件夹中,这些操作必须手动应用回数据库或丢弃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM