<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="mysql-batch-commands">
      </a>
      6.5.1.5 Executing SQL Statements from a Text File
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045317349712">
  </a>
  <a class="indexterm" name="idm46045317348640">
  </a>
  <a class="indexterm" name="idm46045317347152">
  </a>
  <a class="indexterm" name="idm46045317345664">
  </a>
  <a class="indexterm" name="idm46045317344176">
  </a>
  <a class="indexterm" name="idm46045317342688">
  </a>
  <a class="indexterm" name="idm46045317341600">
  </a>
  <a class="indexterm" name="idm46045317340512">
  </a>
  <a class="indexterm" name="idm46045317339408">
  </a>
  <p>
   The
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   client typically is used
        interactively, like this:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa75154790"><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">mysql <em class="replaceable">db_name</em></code></pre>
  </div>
  <p>
   However, it is also possible to put your SQL statements in a
        file and then tell
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   to read its input
        from that file. To do so, create a text file
   <em class="replaceable">
    <code>
     text_file
    </code>
   </em>
   that contains the
        statements you wish to execute. Then invoke
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   as shown here:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa88779498"><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">mysql <em class="replaceable">db_name</em> &lt; <em class="replaceable">text_file</em></code></pre>
  </div>
  <p>
   If you place a
   <code class="literal">
    USE
    <em class="replaceable">
     <code>
      db_name
     </code>
    </em>
   </code>
   statement as the
        first statement in the file, it is unnecessary to specify the
        database name on the command line:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa96425436"><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">mysql &lt; text_file</code></pre>
  </div>
  <p>
   If you are already running
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   , you can
        execute an SQL script file using the
   <code class="literal">
    source
   </code>
   command or
   <code class="literal">
    \.
   </code>
   command:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-sql"><div class="docs-select-all right" id="sa68181178"><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">source</span> <em class="replaceable">file_name</em>
<span class="token prompt">mysql&gt;</span> \<span class="token punctuation">.</span> <em class="replaceable">file_name</em></code></pre>
  </div>
  <p>
   Sometimes you may want your script to display progress
        information to the user. For this you can insert statements like
        this:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa4827551"><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 keyword">SELECT</span> <span class="token string">'&lt;info_to_display&gt;'</span> <span class="token keyword">AS</span> <span class="token string">' '</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   The statement shown outputs
   <code class="literal">
    &lt;info_to_display&gt;
   </code>
   .
  </p>
  <p>
   You can also invoke
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   with the
   <a class="link" href="mysql-command-options.html#option_mysql_verbose">
    <code class="option">
     --verbose
    </code>
   </a>
   option, which causes
        each statement to be displayed before the result that it
        produces.
  </p>
  <p>
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   ignores Unicode byte order mark (BOM)
        characters at the beginning of input files. Previously, it read
        them and sent them to the server, resulting in a syntax error.
        Presence of a BOM does not cause
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   to
        change its default character set. To do that, invoke
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   with an option such as
   <a class="link" href="mysql-command-options.html#option_mysql_default-character-set">
    <code class="option">
     --default-character-set=utf8mb4
    </code>
   </a>
   .
  </p>
  <p>
   For more information about batch mode, see
   <a class="xref" href="batch-mode.html" title="5.5 Using mysql in Batch Mode">
    Section 5.5, “Using mysql in Batch Mode”
   </a>
   .
  </p>
 </div>
 <br/>
</div>
