<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="external-locking">
      </a>
      10.11.5 External Locking
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045222584576">
  </a>
  <a class="indexterm" name="idm46045222583504">
  </a>
  <p>
   External locking is the use of file system locking to manage
        contention for
   <a class="link" href="myisam-storage-engine.html" title="18.2 The MyISAM Storage Engine">
    <code class="literal">
     MyISAM
    </code>
   </a>
   database
        tables by multiple processes. External locking is used in
        situations where a single process such as the MySQL server
        cannot be assumed to be the only process that requires access to
        tables. Here are some examples:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      If you run multiple servers that use the same database
            directory (not recommended), each server must have external
            locking enabled.
     </p>
    </li>
    <li class="listitem">
     <p>
      If you use
      <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
       <span class="command">
        <strong>
         myisamchk
        </strong>
       </span>
      </a>
      to perform table
            maintenance operations on
      <a class="link" href="myisam-storage-engine.html" title="18.2 The MyISAM Storage Engine">
       <code class="literal">
        MyISAM
       </code>
      </a>
      tables, you must either
            ensure that the server is not running, or that the server
            has external locking enabled so that it locks table files as
            necessary to coordinate with
      <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
       <span class="command">
        <strong>
         myisamchk
        </strong>
       </span>
      </a>
      for access to the tables. The same is true for use of
      <a class="link" href="myisampack.html" title="6.6.6 myisampack — Generate Compressed, Read-Only MyISAM Tables">
       <span class="command">
        <strong>
         myisampack
        </strong>
       </span>
      </a>
      to pack
      <a class="link" href="myisam-storage-engine.html" title="18.2 The MyISAM Storage Engine">
       <code class="literal">
        MyISAM
       </code>
      </a>
      tables.
     </p>
     <p>
      If the server is run with external locking enabled, you can
            use
      <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
       <span class="command">
        <strong>
         myisamchk
        </strong>
       </span>
      </a>
      at any time for read
            operations such a checking tables. In this case, if the
            server tries to update a table that
      <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
       <span class="command">
        <strong>
         myisamchk
        </strong>
       </span>
      </a>
      is using, the server waits for
      <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
       <span class="command">
        <strong>
         myisamchk
        </strong>
       </span>
      </a>
      to finish before it continues.
     </p>
     <p>
      If you use
      <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
       <span class="command">
        <strong>
         myisamchk
        </strong>
       </span>
      </a>
      for write operations
            such as repairing or optimizing tables, or if you use
      <a class="link" href="myisampack.html" title="6.6.6 myisampack — Generate Compressed, Read-Only MyISAM Tables">
       <span class="command">
        <strong>
         myisampack
        </strong>
       </span>
      </a>
      to pack tables, you
      <span class="emphasis">
       <em>
        must
       </em>
      </span>
      always ensure that the
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      server is not using the table. If
            you do not stop
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      , at least do a
      <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
       <span class="command">
        <strong>
         mysqladmin flush-tables
        </strong>
       </span>
      </a>
      before you run
      <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
       <span class="command">
        <strong>
         myisamchk
        </strong>
       </span>
      </a>
      . Your tables
      <span class="emphasis">
       <em>
        may
            become corrupted
       </em>
      </span>
      if the server and
      <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
       <span class="command">
        <strong>
         myisamchk
        </strong>
       </span>
      </a>
      access the tables
            simultaneously.
     </p>
    </li>
   </ul>
  </div>
  <p>
   With external locking in effect, each process that requires
        access to a table acquires a file system lock for the table
        files before proceeding to access the table. If all necessary
        locks cannot be acquired, the process is blocked from accessing
        the table until the locks can be obtained (after the process
        that currently holds the locks releases them).
  </p>
  <p>
   External locking affects server performance because the server
        must sometimes wait for other processes before it can access
        tables.
  </p>
  <p>
   External locking is unnecessary if you run a single server to
        access a given data directory (which is the usual case) and if
        no other programs such as
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   need to
        modify tables while the server is running. If you only
   <span class="emphasis">
    <em>
     read
    </em>
   </span>
   tables with other programs, external
        locking is not required, although
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   might report warnings if the server changes tables while
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   is reading them.
  </p>
  <p>
   With external locking disabled, to use
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   , you must either stop the server
        while
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   executes or else lock and
        flush the tables before running
   <a class="link" href="myisamchk.html" title="6.6.4 myisamchk — MyISAM Table-Maintenance Utility">
    <span class="command">
     <strong>
      myisamchk
     </strong>
    </span>
   </a>
   . To
        avoid this requirement, use the
   <a class="link" href="check-table.html" title="15.7.3.2 CHECK TABLE Statement">
    <code class="literal">
     CHECK
        TABLE
    </code>
   </a>
   and
   <a class="link" href="repair-table.html" title="15.7.3.5 REPAIR TABLE Statement">
    <code class="literal">
     REPAIR TABLE
    </code>
   </a>
   statements to check and repair
   <a class="link" href="myisam-storage-engine.html" title="18.2 The MyISAM Storage Engine">
    <code class="literal">
     MyISAM
    </code>
   </a>
   tables.
  </p>
  <p>
   For
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   , external locking is controlled by
        the value of the
   <a class="link" href="server-system-variables.html#sysvar_skip_external_locking">
    <code class="literal">
     skip_external_locking
    </code>
   </a>
   system
        variable. When this variable is enabled, external locking is
        disabled, and vice versa. External locking is disabled by
        default.
  </p>
  <p>
   Use of external locking can be controlled at server startup by
        using the
   <a class="link" href="server-options.html#option_mysqld_external-locking">
    <code class="option">
     --external-locking
    </code>
   </a>
   or
   <a class="link" href="server-options.html#option_mysqld_external-locking">
    <code class="option">
     --skip-external-locking
    </code>
   </a>
   option.
  </p>
  <p>
   If you do use external locking option to enable updates to
   <a class="link" href="myisam-storage-engine.html" title="18.2 The MyISAM Storage Engine">
    <code class="literal">
     MyISAM
    </code>
   </a>
   tables from many MySQL
        processes, do not start the server with the
   <a class="link" href="server-system-variables.html#sysvar_delay_key_write">
    <code class="literal">
     delay_key_write
    </code>
   </a>
   system variable
        set to
   <code class="literal">
    ALL
   </code>
   or use the
   <code class="literal">
    DELAY_KEY_WRITE=1
   </code>
   table option for any shared
        tables. Otherwise, index corruption can occur.
  </p>
  <p>
   The easiest way to satisfy this condition is to always use
   <a class="link" href="server-options.html#option_mysqld_external-locking">
    <code class="option">
     --external-locking
    </code>
   </a>
   together with
   <a class="link" href="server-system-variables.html#sysvar_delay_key_write">
    <code class="option">
     --delay-key-write=OFF
    </code>
   </a>
   . (This is
        not done by default because in many setups it is useful to have
        a mixture of the preceding options.)
  </p>
 </div>
 <br/>
</div>
