<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="replication-howto-additionalslaves">
      </a>
      19.1.2.8 Adding Replicas to a Replication Environment
     </h4>
    </div>
   </div>
  </div>
  <p>
   You can add another replica to an existing replication
        configuration without stopping the source server. To do this,
        you can set up the new replica by copying the data directory of
        an existing replica, and giving the new replica a different
        server ID (which is user-specified) and server UUID (which is
        generated at startup).
  </p>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    If the replication source server or existing replica that you
          are copying to create the new replica has any scheduled
          events, ensure that these are disabled on the new replica
          before you start it. If an event runs on the new replica that
          has already run on the source, the duplicated operation causes
          an error. The Event Scheduler is controlled by the
    <a class="link" href="server-system-variables.html#sysvar_event_scheduler">
     <code class="literal">
      event_scheduler
     </code>
    </a>
    system
          variable, which defaults to
    <code class="literal">
     ON
    </code>
    , so events
          that are active on the original server run by default when the
          new replica starts up. To stop all events from running on the
          new replica, set the
    <a class="link" href="server-system-variables.html#sysvar_event_scheduler">
     <code class="literal">
      event_scheduler
     </code>
    </a>
    system
          variable to
    <code class="literal">
     OFF
    </code>
    or
    <code class="literal">
     DISABLED
    </code>
    on the new replica. Alternatively,
          you can use the
    <a class="link" href="alter-event.html" title="15.1.3 ALTER EVENT Statement">
     <code class="literal">
      ALTER EVENT
     </code>
    </a>
    statement to set individual events to
    <code class="literal">
     DISABLE
    </code>
    or
    <code class="literal">
     DISABLE ON
          REPLICA
    </code>
    to prevent them from running on the new
          replica. You can list the events on a server using the
    <a class="link" href="show.html" title="15.7.7 SHOW Statements">
     <code class="literal">
      SHOW
     </code>
    </a>
    statement or the
          Information Schema
    <a class="link" href="information-schema-events-table.html" title="28.3.14 The INFORMATION_SCHEMA EVENTS Table">
     <code class="literal">
      EVENTS
     </code>
    </a>
    table.
          For more information, see
    <a class="xref" href="replication-features-invoked.html" title="19.5.1.16 Replication of Invoked Features">
     Section 19.5.1.16, “Replication of Invoked Features”
    </a>
    .
   </p>
  </div>
  <p>
   As an alternative to creating a new replica in this way, MySQL
        Server's clone plugin can be used to transfer all the data and
        replication settings from an existing replica to a clone. For
        instructions to use this method, see
   <a class="xref" href="clone-plugin-replication.html" title="7.6.7.7 Cloning for Replication">
    Section 7.6.7.7, “Cloning for Replication”
   </a>
   .
  </p>
  <p>
   To duplicate an existing replica without cloning, follow these
        steps:
  </p>
  <div class="orderedlist">
   <ol class="orderedlist" type="1">
    <li class="listitem">
     <p>
      Stop the existing replica and record the replica status
            information, particularly the source binary log file and
            relay log file positions. You can view the replica status
            either in the Performance Schema replication tables (see
      <a class="xref" href="performance-schema-replication-tables.html" title="29.12.11 Performance Schema Replication Tables">
       Section 29.12.11, “Performance Schema Replication Tables”
      </a>
      ), or
            by issuing
      <a class="link" href="show-replica-status.html" title="15.7.7.35 SHOW REPLICA STATUS Statement">
       <code class="literal">
        SHOW REPLICA
            STATUS
       </code>
      </a>
      as follows:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa5314456"><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">STOP</span> <span class="token keyword">REPLICA</span><span class="token punctuation">;</span>
<span class="token prompt">mysql&gt;</span> <span class="token keyword">SHOW</span> <span class="token keyword">REPLICA</span> <span class="token keyword">STATUS</span>\G</code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      Shut down the existing replica:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa46558841"><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-terminal"><span class="token prompt">$&gt; </span><span class="token command">mysqladmin</span> shutdown</code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      Copy the data directory from the existing replica to the new
            replica, including the log files and relay log files. You
            can do this by creating an archive using
      <span class="command">
       <strong>
        tar
       </strong>
      </span>
      or
      <code class="literal">
       WinZip
      </code>
      , or by
            performing a direct copy using a tool such as
      <span class="command">
       <strong>
        cp
       </strong>
      </span>
      or
      <span class="command">
       <strong>
        rsync
       </strong>
      </span>
      .
     </p>
     <div class="important" style="margin-left: 0.5in; margin-right: 0.5in;">
      <div class="admon-title">
       Important
      </div>
      <div class="itemizedlist">
       <ul class="itemizedlist" style="list-style-type: disc; ">
        <li class="listitem">
         <p>
          Before copying, verify that all the files relating to
                  the existing replica actually are stored in the data
                  directory. For example, the
          <code class="literal">
           InnoDB
          </code>
          system tablespace, undo tablespace, and redo log might
                  be stored in an alternative location.
          <code class="literal">
           InnoDB
          </code>
          tablespace files and
                  file-per-table tablespaces might have been created in
                  other directories. The binary logs and relay logs for
                  the replica might be in their own directories outside
                  the data directory. Check through the system variables
                  that are set for the existing replica and look for any
                  alternative paths that have been specified. If you
                  find any, copy these directories over as well.
         </p>
        </li>
        <li class="listitem">
         <p>
          During copying, if files have been used for the
                  replication metadata repositories (see
          <a class="xref" href="replica-logs.html" title="19.2.4 Relay Log and Replication Metadata Repositories">
           Section 19.2.4, “Relay Log and Replication Metadata Repositories”
          </a>
          ), ensure that you also
                  copy these files from the existing replica to the new
                  replica. If tables have been used for the repositories
                  (the default, the tables are in the data directory.
         </p>
        </li>
        <li class="listitem">
         <p>
          After copying, delete the
          <code class="filename">
           auto.cnf
          </code>
          file from the copy of
                  the data directory on the new replica, so that the new
                  replica is started with a different generated server
                  UUID. The server UUID must be unique.
         </p>
        </li>
       </ul>
      </div>
     </div>
     <p>
      A common problem that is encountered when adding new
            replicas is that the new replica fails with a series of
            warning and error messages like these:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-none"><div class="docs-select-all right" id="sa91986509"><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-none">071118 16:44:10 [Warning] Neither --relay-log nor --relay-log-index were used; so
replication may break when this MySQL server acts as a replica and has his hostname
changed!! Please use '--relay-log=<em class="replaceable">new_replica_hostname</em>-relay-bin' to avoid this problem.
071118 16:44:10 [ERROR] Failed to open the relay log './<em class="replaceable">old_replica_hostname</em>-relay-bin.003525'
(relay_log_pos 22940879)
071118 16:44:10 [ERROR] Could not find target log during relay log initialization
071118 16:44:10 [ERROR] Failed to initialize the master info structure</code></pre>
     </div>
     <p>
      This situation can occur if the
      <a class="link" href="replication-options-replica.html#sysvar_relay_log">
       <code class="literal">
        relay_log
       </code>
      </a>
      system variable
            is not specified, as the relay log files contain the host
            name as part of their file names. This is also true of the
            relay log index file if the
      <a class="link" href="replication-options-replica.html#sysvar_relay_log_index">
       <code class="literal">
        relay_log_index
       </code>
      </a>
      system
            variable is not used. For more information about these
            variables, see
      <a class="xref" href="replication-options.html" title="19.1.6 Replication and Binary Logging Options and Variables">
       Section 19.1.6, “Replication and Binary Logging Options and Variables”
      </a>
      .
     </p>
     <p>
      To avoid this problem, use the same value for
      <a class="link" href="replication-options-replica.html#sysvar_relay_log">
       <code class="literal">
        relay_log
       </code>
      </a>
      on the new
            replica that was used on the existing replica. If this
            option was not set explicitly on the existing replica, use
      <code class="literal">
       <em class="replaceable">
        <code>
         existing_replica_hostname
        </code>
       </em>
       -relay-bin
      </code>
      .
            If this is not possible, copy the existing replica's relay
            log index file to the new replica and set the
      <a class="link" href="replication-options-replica.html#sysvar_relay_log_index">
       <code class="literal">
        relay_log_index
       </code>
      </a>
      system
            variable on the new replica to match what was used on the
            existing replica. If this option was not set explicitly on
            the existing replica, use
      <code class="literal">
       <em class="replaceable">
        <code>
         existing_replica_hostname
        </code>
       </em>
       -relay-bin.index
      </code>
      .
            Alternatively, if you have already tried to start the new
            replica after following the remaining steps in this section
            and have encountered errors like those described previously,
            then perform the following steps:
     </p>
     <div class="orderedlist">
      <ol class="orderedlist" type="a">
       <li class="listitem">
        <p>
         If you have not already done so, issue
         <a class="link" href="stop-replica.html" title="15.4.2.5 STOP REPLICA Statement">
          <code class="literal">
           STOP REPLICA
          </code>
         </a>
         on the new
                replica.
        </p>
        <p>
         If you have already started the existing replica again,
                issue
         <a class="link" href="stop-replica.html" title="15.4.2.5 STOP REPLICA Statement">
          <code class="literal">
           STOP REPLICA
          </code>
         </a>
         on the
                existing replica as well.
        </p>
       </li>
       <li class="listitem">
        <p>
         Copy the contents of the existing replica's relay log
                index file into the new replica's relay log index file,
                making sure to overwrite any content already in the
                file.
        </p>
       </li>
       <li class="listitem">
        <p>
         Proceed with the remaining steps in this section.
        </p>
       </li>
      </ol>
     </div>
    </li>
    <li class="listitem">
     <p>
      When copying is complete, restart the existing replica.
     </p>
    </li>
    <li class="listitem">
     <p>
      On the new replica, edit the configuration and give the new
            replica a unique server ID (using the
      <a class="link" href="replication-options.html#sysvar_server_id">
       <code class="literal">
        server_id
       </code>
      </a>
      system variable)
            that is not used by the source or any of the existing
            replicas.
     </p>
    </li>
    <li class="listitem">
     <p>
      Start the new replica server, ensuring that replication does
            not start yet by specifying
      <a class="link" href="replication-options-replica.html#option_mysqld_skip-replica-start">
       <code class="option">
        --skip-replica-start
       </code>
      </a>
      . Use the
            Performance Schema replication tables or issue
      <a class="link" href="show-replica-status.html" title="15.7.7.35 SHOW REPLICA STATUS Statement">
       <code class="literal">
        SHOW REPLICA STATUS
       </code>
      </a>
      to
            confirm that the new replica has the correct settings when
            compared with the existing replica. Also display the server
            ID and server UUID and verify that these are correct and
            unique for the new replica.
     </p>
    </li>
    <li class="listitem">
     <p>
      Start the replica threads by issuing a
      <a class="link" href="start-replica.html" title="15.4.2.4 START REPLICA Statement">
       <code class="literal">
        START REPLICA
       </code>
      </a>
      statement. The
            new replica now uses the information in its connection
            metadata repository to start the replication process.
     </p>
    </li>
   </ol>
  </div>
 </div>
 <br/>
</div>
