<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="group-replication-single-primary-mode">
      </a>
      20.1.3.1 Single-Primary Mode
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045134489696">
  </a>
  <a class="indexterm" name="idm46045134488208">
  </a>
  <p>
   In single-primary mode
        (
   <a class="link" href="group-replication-system-variables.html#sysvar_group_replication_single_primary_mode">
    <code class="literal">
     group_replication_single_primary_mode=ON
    </code>
   </a>
   )
        the group has a single primary server that is set to read/write
        mode. All the other members in the group are set to read-only
        mode (with
   <a class="link" href="server-system-variables.html#sysvar_super_read_only">
    <code class="literal">
     super_read_only=ON
    </code>
   </a>
   ).
        The primary typically bootstraps the entire group. All other
        servers that join the group learn about the primary server and
        are automatically set to read-only mode.
  </p>
  <p>
   In single-primary mode, Group Replication enforces that only a
        single server writes to the group, so compared to multi-primary
        mode, consistency checking can be less strict and DDL statements
        do not need to be handled with any extra care. The option
   <a class="link" href="group-replication-system-variables.html#sysvar_group_replication_enforce_update_everywhere_checks">
    <code class="literal">
     group_replication_enforce_update_everywhere_checks
    </code>
   </a>
   enables or disables strict consistency checks for a group. When
        deploying in single-primary mode, or changing the group to
        single-primary mode, this system variable must be set to
   <code class="literal">
    OFF
   </code>
   .
  </p>
  <p>
   The member that is designated as the primary server can change
        in the following ways:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      If the existing primary leaves the group, whether
              voluntarily or unexpectedly, a new primary is elected
              automatically.
     </p>
    </li>
    <li class="listitem">
     <p>
      You can appoint a specific member as the new primary using
              the
      <a class="link" href="group-replication-functions-for-new-primary.html#function_group-replication-set-as-primary">
       <code class="literal">
        group_replication_set_as_primary()
       </code>
      </a>
      function.
     </p>
    </li>
    <li class="listitem">
     <p>
      If you use the
      <a class="link" href="group-replication-functions-for-mode.html#function_group-replication-switch-to-single-primary-mode">
       <code class="literal">
        group_replication_switch_to_single_primary_mode()
       </code>
      </a>
      function to change a group that was running in
              multi-primary mode to run in single-primary mode, a new
              primary is elected automatically, or you can appoint the
              new primary by specifying it with the function.
     </p>
    </li>
   </ul>
  </div>
  <p>
   When a new primary server is elected (automatically or
        manually), it is automatically set to read/write, and the other
        group members remain as secondaries, and as such, read-only. The
        following diagram shows this process:
  </p>
  <div class="figure">
   <a name="group-replication-primary-election-diagram">
   </a>
   <p class="title">
    <b>
     Figure 20.4 New Primary Election
    </b>
   </p>
   <div class="figure-contents">
    <div class="mediaobject">
     <img alt="Five server instances, S1, S2, S3, S4, and S5, are deployed as an interconnected group. Server S1 is the primary. Write clients are communicating with server S1, and a read client is communicating with server S4. Server S1 then fails, breaking communication with the write clients. Server S2 then takes over as the new primary, and the write clients now communicate with server S2." src="images/single-primary-election.png" style="width: 100%; max-width: 899px;"/>
    </div>
   </div>
  </div>
  <br class="figure-break"/>
  <p>
   When a new primary is chosen, it might have a backlog of changes
        that had been applied on the old primary but have not yet been
        applied on the new one. In this case, until the new primary
        catches up with the old one, read/write transactions might
        result in conflicts and be rolled back, and read-only
        transactions might result in stale reads. The Group Replication
        flow control mechanism minimizes the difference between fast and
        slow members, and so reduces the chances of this happening if it
        is activated and properly tuned. For more information, see
   <a class="xref" href="group-replication-flow-control.html" title="20.7.2 Flow Control">
    Section 20.7.2, “Flow Control”
   </a>
   . You can also
        use the
   <a class="link" href="group-replication-system-variables.html#sysvar_group_replication_consistency">
    <code class="literal">
     group_replication_consistency
    </code>
   </a>
   system variable to set the group's level of transaction
        consistency to prevent this issue. Setting this variable to
   <code class="literal">
    BEFORE_ON_PRIMARY_FAILOVER
   </code>
   (the default) or
        any higher consistency level holds new transactions on a newly
        elected primary until the backlog has been applied.
  </p>
  <p>
   For more information on transaction consistency, see
   <a class="xref" href="group-replication-consistency-guarantees.html" title="20.5.3 Transaction Consistency Guarantees">
    Section 20.5.3, “Transaction Consistency Guarantees”
   </a>
   . If
        flow control and transaction consistency guarantees are not used
        for a group, it is a good practice to wait for the new primary
        to apply its replication-related relay log before re-routing
        client applications to it.
  </p>
  <div class="section">
   <div class="titlepage">
    <div>
     <div>
      <h5 class="title">
       <a name="group-replication-primary-election">
       </a>
       20.1.3.1.1 Primary Election Algorithm
      </h5>
     </div>
    </div>
   </div>
   <p>
    The automatic primary member election process involves each
          member looking at the new view of the group, ordering the
          potential new primary members, and choosing the member that
          qualifies as the most suitable. Each member makes its own
          decision locally, following the primary election algorithm in
          its MySQL Server release. Because all members must reach the
          same decision, members adapt their primary election algorithm
          if other group members are running lower MySQL Server
          versions, so that they have the same behavior as the member
          with the lowest MySQL Server version in the group.
   </p>
   <p>
    The factors considered by members when electing a primary, in
          order, are as follows:
   </p>
   <div class="orderedlist">
    <ol class="orderedlist" type="1">
     <li class="listitem">
      <p>
       The first factor considered is which member or members
                are running the lowest MySQL Server version. All group
                members are first ordered by the patch version of their
                release.
      </p>
     </li>
     <li class="listitem">
      <p>
       If more than one member is running the lowest MySQL
                Server version, the second factor considered is the
                member weight of each of those members, as specified by
                the
       <a class="link" href="group-replication-system-variables.html#sysvar_group_replication_member_weight">
        <code class="literal">
         group_replication_member_weight
        </code>
       </a>
       system variable on the member.
      </p>
      <p>
       The
       <a class="link" href="group-replication-system-variables.html#sysvar_group_replication_member_weight">
        <code class="literal">
         group_replication_member_weight
        </code>
       </a>
       system variable specifies a number in the range 0-100.
                All members default to a weight of 50, so set a weight
                below this to lower their ordering, and a weight above
                it to increase their ordering. You can use this
                weighting function to prioritize the use of better
                hardware or to ensure failover to a specific member
                during scheduled maintenance of the primary.
      </p>
     </li>
     <li class="listitem">
      <p>
       If more than one member is running the lowest MySQL
                Server version, and more than one of those members has
                the highest member weight (or member weighting is being
                ignored), the third factor considered is the
                lexicographical order of the generated server UUIDs of
                each member, as specified by the
       <a class="link" href="replication-options.html#sysvar_server_uuid">
        <code class="literal">
         server_uuid
        </code>
       </a>
       system
                variable. The member with the lowest server UUID is
                chosen as the primary. This factor acts as a guaranteed
                and predictable tie-breaker so that all group members
                reach the same decision if it cannot be determined by
                any important factors.
      </p>
     </li>
    </ol>
   </div>
   <p>
   </p>
  </div>
  <div class="section">
   <div class="titlepage">
    <div>
     <div>
      <h5 class="title">
       <a name="group-replication-find-primary">
       </a>
       20.1.3.1.2 Finding the Primary
      </h5>
     </div>
    </div>
   </div>
   <a class="indexterm" name="idm46045134447520">
   </a>
   <p>
    To find out which server is currently the primary when
          deployed in single-primary mode, use the
    <code class="literal">
     MEMBER_ROLE
    </code>
    column in the
    <a class="link" href="performance-schema-replication-group-members-table.html" title="29.12.11.16 The replication_group_members Table">
     <code class="literal">
      performance_schema.replication_group_members
     </code>
    </a>
    table. For example:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa42339057"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-sql"><span class="token prompt">mysql&gt;</span> <span class="token keyword">SELECT</span> MEMBER_HOST<span class="token punctuation">,</span> MEMBER_ROLE <span class="token keyword">FROM</span> performance_schema<span class="token punctuation">.</span>replication_group_members<span class="token punctuation">;</span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> MEMBER_HOST             <span class="token punctuation">|</span> MEMBER_ROLE <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span>
<span class="token output"><span class="token punctuation">|</span> remote1.example.com     <span class="token punctuation">|</span> PRIMARY     <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> remote2.example.com     <span class="token punctuation">|</span> SECONDARY   <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">|</span> remote3.example.com     <span class="token punctuation">|</span> SECONDARY   <span class="token punctuation">|</span></span>
<span class="token output"><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">-</span><span class="token punctuation">+</span></span></code></pre>
   </div>
  </div>
 </div>
 <br/>
</div>
