<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="replication-setup-replicas">
      </a>
      19.1.2.6 Setting Up Replicas
     </h4>
    </div>
   </div>
  </div>
  <p>
   The following sections describe how to set up replicas. Before
        you proceed, ensure that you have:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Configured the source with the necessary configuration
            properties. See
      <a class="xref" href="replication-howto-masterbaseconfig.html" title="19.1.2.1 Setting the Replication Source Configuration">
       Section 19.1.2.1, “Setting the Replication Source Configuration”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      Obtained the source status information, or a copy of the
            source's binary log index file made during a shutdown for
            the data snapshot. See
      <a class="xref" href="replication-howto-masterstatus.html" title="19.1.2.4 Obtaining the Replication Source Binary Log Coordinates">
       Section 19.1.2.4, “Obtaining the Replication Source Binary Log Coordinates”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      On the source, released the read lock:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa84430847"><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">UNLOCK</span> <span class="token keyword">TABLES</span><span class="token punctuation">;</span></code></pre>
     </div>
    </li>
    <li class="listitem">
     <p>
      On the replica, edited the MySQL configuration. See
      <a class="xref" href="replication-howto-slavebaseconfig.html" title="19.1.2.2 Setting the Replica Configuration">
       Section 19.1.2.2, “Setting the Replica Configuration”
      </a>
      .
     </p>
    </li>
   </ul>
  </div>
  <p>
   The next steps depend on whether you have existing data to
        import to the replica or not. See
   <a class="xref" href="replication-snapshot-method.html" title="19.1.2.5 Choosing a Method for Data Snapshots">
    Section 19.1.2.5, “Choosing a Method for Data Snapshots”
   </a>
   for more
        information. Choose one of the following:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      If you do not have a snapshot of a database to import, see
      <a class="xref" href="replication-setup-replicas.html#replication-howto-newservers" title="19.1.2.6.1 Setting Up Replication with New Source and Replicas">
       Section 19.1.2.6.1, “Setting Up Replication with New Source and Replicas”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      If you have a snapshot of a database to import, see
      <a class="xref" href="replication-setup-replicas.html#replication-howto-existingdata" title="19.1.2.6.2 Setting Up Replication with Existing Data">
       Section 19.1.2.6.2, “Setting Up Replication with Existing Data”
      </a>
      .
     </p>
    </li>
   </ul>
  </div>
  <div class="section">
   <div class="titlepage">
    <div>
     <div>
      <h5 class="title">
       <a name="replication-howto-newservers">
       </a>
       19.1.2.6.1 Setting Up Replication with New Source and Replicas
      </h5>
     </div>
    </div>
   </div>
   <p>
    When there is no snapshot of a previous database to import,
          configure the replica to start replication from the new
          source.
   </p>
   <p>
    To set up replication between a source and a new replica:
   </p>
   <div class="orderedlist">
    <ol class="orderedlist" type="1">
     <li class="listitem">
      <p>
       Start up the replica.
      </p>
     </li>
     <li class="listitem">
      <p>
       Execute a
       <a class="link" href="change-replication-source-to.html" title="15.4.2.2 CHANGE REPLICATION SOURCE TO Statement">
        <code class="literal">
         CHANGE REPLICATION SOURCE
              TO
        </code>
       </a>
       statement on the replica to set the source
              configuration. See
       <a class="xref" href="replication-howto-slaveinit.html" title="19.1.2.7 Setting the Source Configuration on the Replica">
        Section 19.1.2.7, “Setting the Source Configuration on the Replica”
       </a>
       .
      </p>
     </li>
    </ol>
   </div>
   <p>
    Perform these replica setup steps on each replica.
   </p>
   <p>
    This method can also be used if you are setting up new servers
          but have an existing dump of the databases from a different
          server that you want to load into your replication
          configuration. By loading the data into a new source, the data
          is automatically replicated to the replicas.
   </p>
   <p>
    If you are setting up a new replication environment using the
          data from a different existing database server to create a new
          source, run the dump file generated from that server on the
          new source. The database updates are automatically propagated
          to the replicas:
   </p>
   <div class="copytoclipboard-wrapper">
    <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa90382026"><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">mysql</span> <span class="token property">-h</span> source &lt; fulldb<span class="token punctuation">.</span>dump</code></pre>
   </div>
  </div>
  <div class="section">
   <div class="titlepage">
    <div>
     <div>
      <h5 class="title">
       <a name="replication-howto-existingdata">
       </a>
       19.1.2.6.2 Setting Up Replication with Existing Data
      </h5>
     </div>
    </div>
   </div>
   <p>
    When setting up replication with existing data, transfer the
          snapshot from the source to the replica before starting
          replication. The process for importing data to the replica
          depends on how you created the snapshot of data on the source.
   </p>
   <div class="tip" style="margin-left: 0.5in; margin-right: 0.5in;">
    <div class="admon-title">
     Tip
    </div>
    <p>
     To deploy multiple instances of MySQL, you can use
     <a class="ulink" href="/doc/mysql-shell/8.4/en/mysql-innodb-cluster.html" target="_top">
      InnoDB Cluster
     </a>
     which enables you to easily administer a group of MySQL server instances in
     <a class="ulink" href="/doc/mysql-shell/8.4/en/" target="_top">
      MySQL Shell
     </a>
     . InnoDB Cluster wraps MySQL Group Replication in a programmatic environment that enables you easily deploy a cluster of MySQL instances to achieve high availability. In addition, InnoDB Cluster interfaces seamlessly with
     <a class="ulink" href="/doc/mysql-router/8.4/en/" target="_top">
      MySQL Router
     </a>
     , which enables your applications to connect to the cluster without writing your own failover process. For similar use cases that do not require high availability, however, you can use
     <a class="ulink" href="/doc/mysql-shell/8.4/en/mysql-innodb-replicaset.html" target="_top">
      InnoDB ReplicaSet
     </a>
     . Installation instructions for MySQL Shell can be found
     <a class="ulink" href="/doc/mysql-shell/8.4/en/mysql-shell-install.html" target="_top">
      here
     </a>
     .
    </p>
   </div>
   <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 (default
     <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>
    Follow this procedure to set up replication with existing
          data:
   </p>
   <div class="orderedlist">
    <ol class="orderedlist" type="1">
     <li class="listitem">
      <p>
       If you used MySQL Server's clone plugin to create a clone
              from an existing replica (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>
       ), the data is
              already transferred. Otherwise, import the data to the
              replica using one of the following methods.
      </p>
      <div class="orderedlist">
       <ol class="orderedlist" type="a">
        <li class="listitem">
         <p>
          If you used
          <a class="link" href="mysqldump.html" title="6.5.4 mysqldump — A Database Backup Program">
           <span class="command">
            <strong>
             mysqldump
            </strong>
           </span>
          </a>
          , start the
                  replica server, ensuring that replication does not
                  start by starting the server with
          <a class="link" href="replication-options-replica.html#option_mysqld_skip-replica-start">
           <code class="option">
            --skip-replica-start
           </code>
          </a>
          .
                  Then import the dump file:
         </p>
         <div class="copytoclipboard-wrapper">
          <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa87554537"><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">mysql</span> &lt; fulldb<span class="token punctuation">.</span>dump</code></pre>
         </div>
        </li>
        <li class="listitem">
         <p>
          If you created a snapshot using the raw data files,
                  extract the data files into your replica's data
                  directory. For example:
         </p>
         <div class="copytoclipboard-wrapper">
          <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa21303907"><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">tar</span> xvf dbdump<span class="token punctuation">.</span>tar</code></pre>
         </div>
         <p>
          You may need to set permissions and ownership on the
                  files so that the replica server can access and modify
                  them. Then start the replica server, ensuring that
                  replication does not start by using
          <a class="link" href="replication-options-replica.html#option_mysqld_skip-replica-start">
           <code class="option">
            --skip-replica-start
           </code>
          </a>
          .
         </p>
        </li>
       </ol>
      </div>
     </li>
     <li class="listitem">
      <p>
       Configure the replica with the replication coordinates
              from the source. This tells the replica the binary log
              file and position within the file where replication needs
              to start. Also, configure the replica with the login
              credentials and host name of the source. For more
              information on the
       <a class="link" href="change-replication-source-to.html" title="15.4.2.2 CHANGE REPLICATION SOURCE TO Statement">
        <code class="literal">
         CHANGE REPLICATION
              SOURCE TO
        </code>
       </a>
       statement required, see
       <a class="xref" href="replication-howto-slaveinit.html" title="19.1.2.7 Setting the Source Configuration on the Replica">
        Section 19.1.2.7, “Setting the Source Configuration on the Replica”
       </a>
       .
      </p>
     </li>
     <li class="listitem">
      <p>
       Start the replication 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.
      </p>
     </li>
    </ol>
   </div>
   <p>
    After you have performed this procedure, the replica connects
          to the source and replicates any updates that have occurred on
          the source since the snapshot was taken. Error messages are
          issued to the replica's error log if it is not able to
          replicate for any reason.
   </p>
   <p>
    The replica uses information logged in its connection metadata
          repository and applier metadata repository to keep track of
          how much of the source's binary log it has processed. By
          default, these repositories are tables named
    <code class="literal">
     slave_master_info
    </code>
    and
    <code class="literal">
     slave_relay_log_info
    </code>
    in the
    <code class="literal">
     mysql
    </code>
    database. Do
    <span class="emphasis">
     <em>
      not
     </em>
    </span>
    remove or edit these tables unless you know exactly what you
          are doing and fully understand the implications. Even in that
          case, it is preferred that you use the
    <a class="link" href="change-replication-source-to.html" title="15.4.2.2 CHANGE REPLICATION SOURCE TO Statement">
     <code class="literal">
      CHANGE REPLICATION SOURCE TO
     </code>
    </a>
    statement to change replication parameters. The replica uses
          the values specified in the statement to update the
          replication metadata repositories automatically. 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>
    , for more information.
   </p>
   <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
    <div class="admon-title">
     Note
    </div>
    <p>
     The contents of the replica's connection metadata repository
            override some of the server options specified on the command
            line or in
     <code class="filename">
      my.cnf
     </code>
     . 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>
     , for more details.
    </p>
   </div>
   <p>
    A single snapshot of the source suffices for multiple
          replicas. To set up additional replicas, use the same source
          snapshot and follow the replica portion of the procedure just
          described.
   </p>
  </div>
 </div>
 <br/>
</div>
