<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="gone-away">
      </a>
      B.3.2.7 MySQL server has gone away
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045054420608">
  </a>
  <a class="indexterm" name="idm46045054419568">
  </a>
  <p>
   This section also covers the related
   <code class="literal">
    Lost connection
          to server during query
   </code>
   error.
  </p>
  <p>
   The most common reason for the
   <code class="literal">
    MySQL server has gone
          away
   </code>
   error is that the server timed out and closed
          the connection. In this case, you normally get one of the
          following error codes (which one you get is operating
          system-dependent).
  </p>
  <div class="informaltable">
   <table summary="MySQL server has gone away error codes and a description of each code.">
    <colgroup>
     <col style="width: 35%"/>
     <col style="width: 65%"/>
    </colgroup>
    <thead>
     <tr>
      <th>
       Error Code
      </th>
      <th>
       Description
      </th>
     </tr>
    </thead>
    <tbody>
     <tr>
      <td>
       <a class="ulink" href="/doc/mysql-errors/8.4/en/client-error-reference.html#error_cr_server_gone_error" target="_top">
        <code class="literal">
         CR_SERVER_GONE_ERROR
        </code>
       </a>
      </td>
      <td>
       The client couldn't send a question to the server.
      </td>
     </tr>
     <tr>
      <td>
       <a class="ulink" href="/doc/mysql-errors/8.4/en/client-error-reference.html#error_cr_server_lost" target="_top">
        <code class="literal">
         CR_SERVER_LOST
        </code>
       </a>
      </td>
      <td>
       The client didn't get an error when writing to the server, but it didn't
                get a full answer (or any answer) to the question.
      </td>
     </tr>
    </tbody>
   </table>
  </div>
  <p>
   By default, the server closes the connection after eight hours
          if nothing has happened. You can change the time limit by
          setting the
   <a class="link" href="server-system-variables.html#sysvar_wait_timeout">
    <code class="literal">
     wait_timeout
    </code>
   </a>
   variable when you start
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   . See
   <a class="xref" href="server-system-variables.html" title="7.1.8 Server System Variables">
    Section 7.1.8, “Server System Variables”
   </a>
   .
  </p>
  <p>
   If you have a script, you just have to issue the query again
          for the client to do an automatic reconnection. This assumes
          that you have automatic reconnection in the client enabled
          (which is the default for the
   <code class="literal">
    mysql
   </code>
   command-line client).
  </p>
  <p>
   Some other common reasons for the
   <code class="literal">
    MySQL server has
          gone away
   </code>
   error are:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      You (or the db administrator) has killed the running
              thread with a
      <a class="link" href="kill.html" title="15.7.8.4 KILL Statement">
       <code class="literal">
        KILL
       </code>
      </a>
      statement or a
      <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
       <span class="command">
        <strong>
         mysqladmin kill
        </strong>
       </span>
      </a>
      command.
     </p>
    </li>
    <li class="listitem">
     <p>
      You tried to run a query after closing the connection to
              the server. This indicates a logic error in the
              application that should be corrected.
     </p>
    </li>
    <li class="listitem">
     <p>
      A client application running on a different host does not
              have the necessary privileges to connect to the MySQL
              server from that host.
     </p>
    </li>
    <li class="listitem">
     <p>
      You got a timeout from the TCP/IP connection on the client
              side. This may happen if you have been using the commands:
      <a class="ulink" href="/doc/c-api/8.4/en/mysql-options.html" target="_top">
       <code class="literal">
        mysql_options(...,
              MYSQL_OPT_READ_TIMEOUT,...)
       </code>
      </a>
      or
      <a class="ulink" href="/doc/c-api/8.4/en/mysql-options.html" target="_top">
       <code class="literal">
        mysql_options(...,
              MYSQL_OPT_WRITE_TIMEOUT,...)
       </code>
      </a>
      . In this case
              increasing the timeout may help solve the problem.
     </p>
    </li>
    <li class="listitem">
     <p>
      You have encountered a timeout on the server side and the
              automatic reconnection in the client is disabled (the
      <code class="literal">
       reconnect
      </code>
      flag in the
      <code class="literal">
       MYSQL
      </code>
      structure is equal to 0).
     </p>
    </li>
    <li class="listitem">
     <p>
      You are using a Windows client and the server had dropped
              the connection (probably because
      <a class="link" href="server-system-variables.html#sysvar_wait_timeout">
       <code class="literal">
        wait_timeout
       </code>
      </a>
      expired)
              before the command was issued.
     </p>
     <p>
      The problem on Windows is that in some cases MySQL does
              not get an error from the OS when writing to the TCP/IP
              connection to the server, but instead gets the error when
              trying to read the answer from the connection.
     </p>
     <p>
      The solution to this is to either do a
      <a class="ulink" href="/doc/c-api/8.4/en/mysql-ping.html" target="_top">
       <code class="literal">
        mysql_ping()
       </code>
      </a>
      on the
              connection if there has been a long time since the last
              query (this is what Connector/ODBC does) or set
      <a class="link" href="server-system-variables.html#sysvar_wait_timeout">
       <code class="literal">
        wait_timeout
       </code>
      </a>
      on the
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      server so high that it in
              practice never times out.
     </p>
    </li>
    <li class="listitem">
     <p>
      You can also get these errors if you send a query to the
              server that is incorrect or too large. If
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      receives a packet that is too
              large or out of order, it assumes that something has gone
              wrong with the client and closes the connection. If you
              need big queries (for example, if you are working with big
      <a class="link" href="blob.html" title="13.3.4 The BLOB and TEXT Types">
       <code class="literal">
        BLOB
       </code>
      </a>
      columns), you can
              increase the query limit by setting the server's
      <a class="link" href="server-system-variables.html#sysvar_max_allowed_packet">
       <code class="literal">
        max_allowed_packet
       </code>
      </a>
      variable, which has a default value of 64MB. You may also
              need to increase the maximum packet size on the client
              end. More information on setting the packet size is given
              in
      <a class="xref" href="packet-too-large.html" title="B.3.2.8 Packet Too Large">
       Section B.3.2.8, “Packet Too Large”
      </a>
      .
     </p>
     <p>
      An
      <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
       <code class="literal">
        INSERT
       </code>
      </a>
      or
      <a class="link" href="replace.html" title="15.2.12 REPLACE Statement">
       <code class="literal">
        REPLACE
       </code>
      </a>
      statement that
              inserts a great many rows can also cause these sorts of
              errors. Either one of these statements sends a single
              request to the server irrespective of the number of rows
              to be inserted; thus, you can often avoid the error by
              reducing the number of rows sent per
      <a class="link" href="insert.html" title="15.2.7 INSERT Statement">
       <code class="literal">
        INSERT
       </code>
      </a>
      or
      <a class="link" href="replace.html" title="15.2.12 REPLACE Statement">
       <code class="literal">
        REPLACE
       </code>
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      It is also possible to see this error if host name lookups
              fail (for example, if the DNS server on which your server
              or network relies goes down). This is because MySQL is
              dependent on the host system for name resolution, but has
              no way of knowing whether it is working—from MySQL's
              point of view the problem is indistinguishable from any
              other network timeout.
     </p>
     <p>
      You may also see the
      <code class="literal">
       MySQL server has gone
              away
      </code>
      error if MySQL is started with the
      <a class="link" href="server-system-variables.html#sysvar_skip_networking">
       <code class="literal">
        skip_networking
       </code>
      </a>
      system
              variable enabled.
     </p>
     <p>
      Another networking issue that can cause this error occurs
              if the MySQL port (default 3306) is blocked by your
              firewall, thus preventing any connections at all to the
              MySQL server.
     </p>
    </li>
    <li class="listitem">
     <p>
      You can also encounter this error with applications that
              fork child processes, all of which try to use the same
              connection to the MySQL server. This can be avoided by
              using a separate connection for each child process.
     </p>
    </li>
    <li class="listitem">
     <p>
      You have encountered a bug where the server died while
              executing the query.
     </p>
    </li>
   </ul>
  </div>
  <p>
   You can check whether the MySQL server died and restarted by
          executing
   <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
    <span class="command">
     <strong>
      mysqladmin version
     </strong>
    </span>
   </a>
   and examining
          the server's uptime. If the client connection was broken
          because
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   crashed and restarted, you
          should concentrate on finding the reason for the crash. Start
          by checking whether issuing the query again kills the server
          again. See
   <a class="xref" href="crashing.html" title="B.3.3.3 What to Do If MySQL Keeps Crashing">
    Section B.3.3.3, “What to Do If MySQL Keeps Crashing”
   </a>
   .
  </p>
  <p>
   You can obtain more information about lost connections by
          starting
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   with the
   <a class="link" href="server-system-variables.html#sysvar_log_error_verbosity">
    <code class="literal">
     log_error_verbosity
    </code>
   </a>
   system
          variable set to 3. This logs some of the disconnection
          messages in the
   <code class="literal">
    hostname.err
   </code>
   file. See
   <a class="xref" href="error-log.html" title="7.4.2 The Error Log">
    Section 7.4.2, “The Error Log”
   </a>
   .
  </p>
  <p>
   If you want to create a bug report regarding this problem, be
          sure that you include the following information:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Indicate whether the MySQL server died. You can find
              information about this in the server error log. See
      <a class="xref" href="crashing.html" title="B.3.3.3 What to Do If MySQL Keeps Crashing">
       Section B.3.3.3, “What to Do If MySQL Keeps Crashing”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      If a specific query kills
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      and
              the tables involved were checked with
      <a class="link" href="check-table.html" title="15.7.3.2 CHECK TABLE Statement">
       <code class="literal">
        CHECK TABLE
       </code>
      </a>
      before you ran
              the query, can you provide a reproducible test case? See
      <a class="xref" href="debugging-mysql.html" title="7.9 Debugging MySQL">
       Section 7.9, “Debugging MySQL”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      What is the value of the
      <a class="link" href="server-system-variables.html#sysvar_wait_timeout">
       <code class="literal">
        wait_timeout
       </code>
      </a>
      system
              variable in the MySQL server? (
      <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
       <span class="command">
        <strong>
         mysqladmin
              variables
        </strong>
       </span>
      </a>
      gives you the value of this variable.)
     </p>
    </li>
    <li class="listitem">
     <p>
      Have you tried to run
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      with the
              general query log enabled to determine whether the problem
              query appears in the log? (See
      <a class="xref" href="query-log.html" title="7.4.3 The General Query Log">
       Section 7.4.3, “The General Query Log”
      </a>
      .)
     </p>
    </li>
   </ul>
  </div>
  <p>
   See also
   <a class="xref" href="communication-errors.html" title="B.3.2.9 Communication Errors and Aborted Connections">
    Section B.3.2.9, “Communication Errors and Aborted Connections”
   </a>
   , and
   <a class="xref" href="bug-reports.html" title="1.6 How to Report Bugs or Problems">
    Section 1.6, “How to Report Bugs or Problems”
   </a>
   .
  </p>
 </div>
 <br/>
</div>
