<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="query-log">
      </a>
      7.4.3 The General Query Log
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045258419968">
  </a>
  <a class="indexterm" name="idm46045258418896">
  </a>
  <p>
   The general query log is a general record of what
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   is doing. The server writes information
      to this log when clients connect or disconnect, and it logs each
      SQL statement received from clients. The general query log can be
      very useful when you suspect an error in a client and want to know
      exactly what the client sent to
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   .
  </p>
  <p>
   Each line that shows when a client connects also includes
   <code class="literal">
    using
    <em class="replaceable">
     <code>
      connection_type
     </code>
    </em>
   </code>
   to indicate
      the protocol used to establish the connection.
   <em class="replaceable">
    <code>
     connection_type
    </code>
   </em>
   is one of
   <code class="literal">
    TCP/IP
   </code>
   (TCP/IP connection established without
      SSL),
   <code class="literal">
    SSL/TLS
   </code>
   (TCP/IP connection established
      with SSL),
   <code class="literal">
    Socket
   </code>
   (Unix socket file
      connection),
   <code class="literal">
    Named Pipe
   </code>
   (Windows named pipe
      connection), or
   <code class="literal">
    Shared Memory
   </code>
   (Windows shared
      memory connection).
  </p>
  <p>
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   writes statements to the query log in
      the order that it receives them, which might differ from the order
      in which they are executed. This logging order is in contrast with
      that of the binary log, for which statements are written after
      they are executed but before any locks are released. In addition,
      the query log may contain statements that only select data while
      such statements are never written to the binary log.
  </p>
  <p>
   When using statement-based binary logging on a replication source
      server, statements received by its replicas are written to the
      query log of each replica. Statements are written to the query log
      of the source if a client reads events with the
   <a class="link" href="mysqlbinlog.html" title="6.6.9 mysqlbinlog — Utility for Processing Binary Log Files">
    <span class="command">
     <strong>
      mysqlbinlog
     </strong>
    </span>
   </a>
   utility and passes them to the
      server.
  </p>
  <p>
   However, when using row-based binary logging, updates are sent as
      row changes rather than SQL statements, and thus these statements
      are never written to the query log when
   <a class="link" href="replication-options-binary-log.html#sysvar_binlog_format">
    <code class="literal">
     binlog_format
    </code>
   </a>
   is
   <code class="literal">
    ROW
   </code>
   . A given update also might not be written
      to the query log when this variable is set to
   <code class="literal">
    MIXED
   </code>
   , depending on the statement used. See
   <a class="xref" href="replication-sbr-rbr.html" title="19.2.1.1 Advantages and Disadvantages of Statement-Based and Row-Based Replication">
    Section 19.2.1.1, “Advantages and Disadvantages of Statement-Based and Row-Based
        Replication”
   </a>
   , for more information.
  </p>
  <p>
   By default, the general query log is disabled. To specify the
      initial general query log state explicitly, use
   <a class="link" href="server-system-variables.html#sysvar_general_log">
    <code class="option">
     --general_log[={0|1}]
    </code>
   </a>
   . With no
      argument or an argument of 1,
   <a class="link" href="server-system-variables.html#sysvar_general_log">
    <code class="option">
     --general_log
    </code>
   </a>
   enables the log. With
      an argument of 0, this option disables the log. To specify a log
      file name, use
   <a class="link" href="server-system-variables.html#sysvar_general_log_file">
    <code class="option">
     --general_log_file=
     <em class="replaceable">
      <code>
       file_name
      </code>
     </em>
    </code>
   </a>
   .
      To specify the log destination, use the
   <a class="link" href="server-system-variables.html#sysvar_log_output">
    <code class="literal">
     log_output
    </code>
   </a>
   system variable (as
      described in
   <a class="xref" href="log-destinations.html" title="7.4.1 Selecting General Query Log and Slow Query Log Output Destinations">
    Section 7.4.1, “Selecting General Query Log and Slow Query Log Output Destinations”
   </a>
   ).
  </p>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    If you specify the
    <code class="literal">
     TABLE
    </code>
    log destination, see
    <a class="xref" href="log-destinations.html#log-destinations-tables-open-files" title="Log Tables and “Too many open files” Errors">
     Log Tables and
     <span class="quote">
      “
      <span class="quote">
       Too many open files
      </span>
      ”
     </span>
     Errors
    </a>
    .
   </p>
  </div>
  <p>
   If you specify no name for the general query log file, the default
      name is
   <code class="filename">
    <em class="replaceable">
     <code>
      host_name
     </code>
    </em>
    .log
   </code>
   . The
      server creates the file in the data directory unless an absolute
      path name is given to specify a different directory.
  </p>
  <p>
   To disable or enable the general query log or change the log file
      name at runtime, use the global
   <a class="link" href="server-system-variables.html#sysvar_general_log">
    <code class="literal">
     general_log
    </code>
   </a>
   and
   <a class="link" href="server-system-variables.html#sysvar_general_log_file">
    <code class="literal">
     general_log_file
    </code>
   </a>
   system
      variables. Set
   <a class="link" href="server-system-variables.html#sysvar_general_log">
    <code class="literal">
     general_log
    </code>
   </a>
   to 0
      (or
   <code class="literal">
    OFF
   </code>
   ) to disable the log or to 1 (or
   <code class="literal">
    ON
   </code>
   ) to enable it. Set
   <a class="link" href="server-system-variables.html#sysvar_general_log_file">
    <code class="literal">
     general_log_file
    </code>
   </a>
   to specify the
      name of the log file. If a log file already is open, it is closed
      and the new file is opened.
  </p>
  <p>
   When the general query log is enabled, the server writes output to
      any destinations specified by the
   <a class="link" href="server-system-variables.html#sysvar_log_output">
    <code class="literal">
     log_output
    </code>
   </a>
   system variable. If
      you enable the log, the server opens the log file and writes
      startup messages to it. However, further logging of queries to the
      file does not occur unless the
   <code class="literal">
    FILE
   </code>
   log
      destination is selected. If the destination is
   <code class="literal">
    NONE
   </code>
   , the server writes no queries even if the
      general log is enabled. Setting the log file name has no effect on
      logging if the log destination value does not contain
   <code class="literal">
    FILE
   </code>
   .
  </p>
  <p>
   Server restarts and log flushing do not cause a new general query
      log file to be generated (although flushing closes and reopens
      it). To rename the file and create a new one, use the following
      commands:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa62949868"><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">mv</span> <em class="replaceable">host_name</em><span class="token punctuation">.</span>log <em class="replaceable">host_name</em>-old<span class="token punctuation">.</span>log
<span class="token prompt">$&gt; </span><span class="token command">mysqladmin</span> flush-logs general
<span class="token prompt">$&gt; </span><span class="token command">mv</span> <em class="replaceable">host_name</em>-old<span class="token punctuation">.</span>log <em class="replaceable">backup-directory</em></code></pre>
  </div>
  <p>
   On Windows, use
   <span class="command">
    <strong>
     rename
    </strong>
   </span>
   rather than
   <span class="command">
    <strong>
     mv
    </strong>
   </span>
   .
  </p>
  <p>
   You can also rename the general query log file at runtime by
      disabling the log:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa26413783"><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">SET</span> <span class="token keyword">GLOBAL</span> general_log <span class="token operator">=</span> <span class="token string">'OFF'</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   With the log disabled, rename the log file externally (for
      example, from the command line). Then enable the log again:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-sql"><div class="docs-select-all right" id="sa92307903"><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">SET</span> <span class="token keyword">GLOBAL</span> general_log <span class="token operator">=</span> <span class="token string">'ON'</span><span class="token punctuation">;</span></code></pre>
  </div>
  <p>
   This method works on any platform and does not require a server
      restart.
  </p>
  <p>
   To disable or enable general query logging for the current
      session, set the session
   <a class="link" href="server-system-variables.html#sysvar_sql_log_off">
    <code class="literal">
     sql_log_off
    </code>
   </a>
   variable to
   <code class="literal">
    ON
   </code>
   or
   <code class="literal">
    OFF
   </code>
   . (This assumes
      that the general query log itself is enabled.)
  </p>
  <p>
   Passwords in statements written to the general query log are
      rewritten by the server not to occur literally in plain text.
      Password rewriting can be suppressed for the general query log by
      starting the server with the
   <a class="link" href="server-options.html#option_mysqld_log-raw">
    <code class="option">
     --log-raw
    </code>
   </a>
   option. This option may
      be useful for diagnostic purposes, to see the exact text of
      statements as received by the server, but for security reasons is
      not recommended for production use. See also
   <a class="xref" href="password-logging.html" title="8.1.2.3 Passwords and Logging">
    Section 8.1.2.3, “Passwords and Logging”
   </a>
   .
  </p>
  <p>
   An implication of password rewriting is that statements that
      cannot be parsed (due, for example, to syntax errors) are not
      written to the general query log because they cannot be known to
      be password free. Use cases that require logging of all statements
      including those with errors should use the
   <a class="link" href="server-options.html#option_mysqld_log-raw">
    <code class="option">
     --log-raw
    </code>
   </a>
   option, bearing in mind
      that this also bypasses password rewriting.
  </p>
  <p>
   Password rewriting occurs only when plain text passwords are
      expected. For statements with syntax that expect a password hash
      value, no rewriting occurs. If a plain text password is supplied
      erroneously for such syntax, the password is logged as given,
      without rewriting.
  </p>
  <p>
   The
   <a class="link" href="server-system-variables.html#sysvar_log_timestamps">
    <code class="literal">
     log_timestamps
    </code>
   </a>
   system
      variable controls the time zone of timestamps in messages written
      to the general query log file (as well as to the slow query log
      file and the error log). It does not affect the time zone of
      general query log and slow query log messages written to log
      tables, but rows retrieved from those tables can be converted from
      the local system time zone to any desired time zone with
   <a class="link" href="date-and-time-functions.html#function_convert-tz">
    <code class="literal">
     CONVERT_TZ()
    </code>
   </a>
   or by setting the
      session
   <a class="link" href="server-system-variables.html#sysvar_time_zone">
    <code class="literal">
     time_zone
    </code>
   </a>
   system
      variable.
  </p>
 </div>
 <br/>
</div>
