<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h4 class="title">
      <a name="can-not-connect-to-server">
      </a>
      B.3.2.2 Can't connect to [local] MySQL server
     </h4>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045054581712">
  </a>
  <a class="indexterm" name="idm46045054580640">
  </a>
  <p>
   A MySQL client on Unix can connect to the
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   server in two different ways: By
          using a Unix socket file to connect through a file in the file
          system (default
   <code class="filename">
    /tmp/mysql.sock
   </code>
   ), or by
          using TCP/IP, which connects through a port number. A Unix
          socket file connection is faster than TCP/IP, but can be used
          only when connecting to a server on the same computer. A Unix
          socket file is used if you do not specify a host name or if
          you specify the special host name
   <code class="literal">
    localhost
   </code>
   .
  </p>
  <p>
   If the MySQL server is running on Windows, you can connect
          using TCP/IP. If the server is started with the
   <a class="link" href="server-system-variables.html#sysvar_named_pipe">
    <code class="literal">
     named_pipe
    </code>
   </a>
   system variable
          enabled, you can also connect with named pipes if you run the
          client on the host where the server is running. The name of
          the named pipe is
   <code class="literal">
    MySQL
   </code>
   by default. If you
          do not give a host name when connecting to
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   , a MySQL client first tries to
          connect to the named pipe. If that does not work, it connects
          to the TCP/IP port. You can force the use of named pipes on
          Windows by using
   <code class="literal">
    .
   </code>
   as the host name.
  </p>
  <p>
   The error (2002)
   <code class="literal">
    Can't connect to ...
   </code>
   normally means that there is no MySQL server running on the
          system or that you are using an incorrect Unix socket file
          name or TCP/IP port number when trying to connect to the
          server. You should also check that the TCP/IP port you are
          using has not been blocked by a firewall or port blocking
          service.
  </p>
  <p>
   The error (2003)
   <code class="literal">
    Can't connect to MySQL server on
          '
    <em class="replaceable">
     <code>
      server
     </code>
    </em>
    ' (10061)
   </code>
   indicates that the network connection has been refused. You
          should check that there is a MySQL server running, that it has
          network connections enabled, and that the network port you
          specified is the one configured on the server.
  </p>
  <p>
   Start by checking whether there is a process named
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   running on your server host. (Use
   <span class="command">
    <strong>
     ps xa | grep mysqld
    </strong>
   </span>
   on Unix or the Task
          Manager on Windows.) If there is no such process, you should
          start the server. See
   <a class="xref" href="starting-server.html" title="2.9.2 Starting the Server">
    Section 2.9.2, “Starting the Server”
   </a>
   .
  </p>
  <p>
   If a
   <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
    <span class="command">
     <strong>
      mysqld
     </strong>
    </span>
   </a>
   process is running, you can
          check it by trying the following commands. The port number or
          Unix socket file name might be different in your setup.
   <code class="literal">
    host_ip
   </code>
   represents the IP address of the
          machine where the server is running.
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa39100841"><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">mysqladmin</span> version
<span class="token prompt">$&gt; </span><span class="token command">mysqladmin</span> variables
<span class="token prompt">$&gt; </span><span class="token command">mysqladmin</span> <span class="token property">-h</span> `hostname` version variables
<span class="token prompt">$&gt; </span><span class="token command">mysqladmin</span> <span class="token property">-h</span> `hostname` <span class="token constant">--port</span><span class="token attr-value"><span class="token punctuation">=</span>3306</span> version
<span class="token prompt">$&gt; </span><span class="token command">mysqladmin</span> <span class="token property">-h</span> host_ip version
<span class="token prompt">$&gt; </span><span class="token command">mysqladmin</span> <span class="token constant">--protocol</span><span class="token attr-value"><span class="token punctuation">=</span>SOCKET</span> <span class="token constant">--socket</span><span class="token attr-value"><span class="token punctuation">=</span>/tmp/mysql.sock</span> version</code></pre>
  </div>
  <p>
   Note the use of backticks rather than forward quotation marks
          with the
   <span class="command">
    <strong>
     hostname
    </strong>
   </span>
   command; these cause the
          output of
   <span class="command">
    <strong>
     hostname
    </strong>
   </span>
   (that is, the current
          host name) to be substituted into the
   <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
    <span class="command">
     <strong>
      mysqladmin
     </strong>
    </span>
   </a>
   command. If you have no
   <span class="command">
    <strong>
     hostname
    </strong>
   </span>
   command or are running on Windows,
          you can manually type the host name of your machine (without
          backticks) following the
   <code class="literal">
    -h
   </code>
   option. You can
          also try
   <code class="literal">
    -h 127.0.0.1
   </code>
   to connect with
          TCP/IP to the local host.
  </p>
  <p>
   Make sure that the server has not been configured to ignore
          network connections or (if you are attempting to connect
          remotely) that it has not been configured to listen only
          locally on its network interfaces. If the server was started
          with the
   <a class="link" href="server-system-variables.html#sysvar_skip_networking">
    <code class="literal">
     skip_networking
    </code>
   </a>
   system variable enabled, it cannot accept TCP/IP connections
          at all. If the server was started with the
   <a class="link" href="server-system-variables.html#sysvar_bind_address">
    <code class="literal">
     bind_address
    </code>
   </a>
   system variable
          set to
   <code class="literal">
    127.0.0.1
   </code>
   , it listens for TCP/IP
          connections only locally on the loopback interface and does
          not accept remote connections.
  </p>
  <p>
   Check to make sure that there is no firewall blocking access
          to MySQL. Your firewall may be configured on the basis of the
          application being executed, or the port number used by MySQL
          for communication (3306 by default). Under Linux or Unix,
          check your IP tables (or similar) configuration to ensure that
          the port has not been blocked. Under Windows, applications
          such as ZoneAlarm or Windows Firewall may need to be
          configured not to block the MySQL port.
  </p>
  <p>
   Here are some reasons the
   <code class="literal">
    Can't connect to local
          MySQL server
   </code>
   error might occur:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      is not running on the local
              host. Check your operating system's process list to ensure
              the
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      process is present.
     </p>
    </li>
    <li class="listitem">
     <p>
      You're running a MySQL server on Windows with many TCP/IP
              connections to it. If you're experiencing that quite often
              your clients get that error, you can find a workaround
              here:
      <a class="xref" href="can-not-connect-to-server.html#can-not-connect-to-server-on-windows" title="B.3.2.2.1 Connection to MySQL Server Failing on Windows">
       Section B.3.2.2.1, “Connection to MySQL Server Failing on Windows”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      Someone has removed the Unix socket file that
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      uses
              (
      <code class="filename">
       /tmp/mysql.sock
      </code>
      by default). For
              example, you might have a
      <span class="command">
       <strong>
        cron
       </strong>
      </span>
      job that
              removes old files from the
      <code class="filename">
       /tmp
      </code>
      directory. You can always run
      <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>
      to check whether the Unix socket file
              that
      <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
       <span class="command">
        <strong>
         mysqladmin
        </strong>
       </span>
      </a>
      is trying to use really
              exists. The fix in this case is to change the
      <span class="command">
       <strong>
        cron
       </strong>
      </span>
      job to not remove
      <code class="filename">
       mysql.sock
      </code>
      or to place the socket
              file somewhere else. See
      <a class="xref" href="problems-with-mysql-sock.html" title="B.3.3.6 How to Protect or Change the MySQL Unix Socket File">
       Section B.3.3.6, “How to Protect or Change the MySQL Unix Socket File”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      You have started the
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      server with
              the
      <a class="link" href="server-options.html#option_mysqld_socket">
       <code class="option">
        --socket=/path/to/socket
       </code>
      </a>
      option, but forgotten to tell client programs the new name
              of the socket file. If you change the socket path name for
              the server, you must also notify the MySQL clients. You
              can do this by providing the same
      <a class="link" href="connection-options.html#option_general_socket">
       <code class="option">
        --socket
       </code>
      </a>
      option when you
              run client programs. You also need to ensure that clients
              have permission to access the
      <code class="filename">
       mysql.sock
      </code>
      file. To find out where
              the socket file is, you can do:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa49975740"><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">netstat</span> <span class="token property">-ln</span> | grep mysql</code></pre>
     </div>
     <p>
      See
      <a class="xref" href="problems-with-mysql-sock.html" title="B.3.3.6 How to Protect or Change the MySQL Unix Socket File">
       Section B.3.3.6, “How to Protect or Change the MySQL Unix Socket File”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      You are using Linux and one server thread has died (dumped
              core). In this case, you must kill the other
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      threads (for example, with
      <a class="link" href="kill.html" title="15.7.8.4 KILL Statement">
       <span class="command">
        <strong>
         kill
        </strong>
       </span>
      </a>
      ) before you can restart the MySQL
              server. 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>
      The server or client program might not have the proper
              access privileges for the directory that holds the Unix
              socket file or the socket file itself. In this case, you
              must either change the access privileges for the directory
              or socket file so that the server and clients can access
              them, or restart
      <a class="link" href="mysqld.html" title="6.3.1 mysqld — The MySQL Server">
       <span class="command">
        <strong>
         mysqld
        </strong>
       </span>
      </a>
      with a
      <a class="link" href="server-options.html#option_mysqld_socket">
       <code class="option">
        --socket
       </code>
      </a>
      option that
              specifies a socket file name in a directory where the
              server can create it and where client programs can access
              it.
     </p>
    </li>
   </ul>
  </div>
  <p>
   If you get the error message
   <code class="literal">
    Can't connect to MySQL
          server on some_host
   </code>
   , you can try the following
          things to find out what the problem is:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Check whether the server is running on that host by
              executing
      <code class="literal">
       telnet some_host 3306
      </code>
      and
              pressing the Enter key a couple of times. (3306 is the
              default MySQL port number. Change the value if your server
              is listening to a different port.) If there is a MySQL
              server running and listening to the port, you should get a
              response that includes the server's version number. If you
              get an error such as
      <code class="literal">
       telnet: Unable to connect to
              remote host: Connection refused
      </code>
      , then there is
              no server running on the given port.
     </p>
    </li>
    <li class="listitem">
     <p>
      If the server is running on the local host, try using
      <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
       <span class="command">
        <strong>
         mysqladmin -h localhost variables
        </strong>
       </span>
      </a>
      to
              connect using the Unix socket file. Verify the TCP/IP port
              number that the server is configured to listen to (it is
              the value of the
      <a class="link" href="server-system-variables.html#sysvar_port">
       <code class="literal">
        port
       </code>
      </a>
      variable.)
     </p>
    </li>
    <li class="listitem">
     <p>
      If you are running under Linux and Security-Enhanced Linux
              (SELinux) is enabled, see
      <a class="xref" href="selinux.html" title="8.7 SELinux">
       Section 8.7, “SELinux”
      </a>
      .
     </p>
    </li>
   </ul>
  </div>
  <div class="section">
   <div class="titlepage">
    <div>
     <div>
      <h5 class="title">
       <a name="can-not-connect-to-server-on-windows">
       </a>
       B.3.2.2.1 Connection to MySQL Server Failing on Windows
      </h5>
     </div>
    </div>
   </div>
   <p>
    When you're running a MySQL server on Windows with many
            TCP/IP connections to it, and you're experiencing that quite
            often your clients get a
    <code class="literal">
     Can't connect to MySQL
            server
    </code>
    error, the reason might be that Windows
            does not allow for enough ephemeral (short-lived) ports to
            serve those connections.
   </p>
   <p>
    The purpose of
    <code class="literal">
     TIME_WAIT
    </code>
    is to keep a
            connection accepting packets even after the connection has
            been closed. This is because Internet routing can cause a
            packet to take a slow route to its destination and it may
            arrive after both sides have agreed to close. If the port is
            in use for a new connection, that packet from the old
            connection could break the protocol or compromise personal
            information from the original connection. The
    <code class="literal">
     TIME_WAIT
    </code>
    delay prevents this by ensuring
            that the port cannot be reused until after some time has
            been permitted for those delayed packets to arrive.
   </p>
   <p>
    It is safe to reduce
    <code class="literal">
     TIME_WAIT
    </code>
    greatly on
            LAN connections because there is little chance of packets
            arriving at very long delays, as they could through the
            Internet with its comparatively large distances and
            latencies.
   </p>
   <p>
    Windows permits ephemeral (short-lived) TCP ports to the
            user. After any port is closed, it remains in a
    <code class="literal">
     TIME_WAIT
    </code>
    status for 120 seconds. The
            port is not available again until this time expires. The
            default range of port numbers depends on the version of
            Windows, with a more limited number of ports in older
            versions:
   </p>
   <div class="itemizedlist">
    <ul class="itemizedlist" style="list-style-type: disc; ">
     <li class="listitem">
      <p>
       Windows through Server 2003: Ports in range
                1025–5000
      </p>
     </li>
     <li class="listitem">
      <p>
       Windows Vista, Server 2008, and newer: Ports in range
                49152–65535
      </p>
     </li>
    </ul>
   </div>
   <p>
    With a small stack of available TCP ports (5000) and a high
            number of TCP ports being open and closed over a short
            period of time along with the
    <code class="literal">
     TIME_WAIT
    </code>
    status you have a good chance for running out of ports.
            There are two ways to address this problem:
   </p>
   <div class="itemizedlist">
    <ul class="itemizedlist" style="list-style-type: disc; ">
     <li class="listitem">
      <p>
       Reduce the number of TCP ports consumed quickly by
                investigating connection pooling or persistent
                connections where possible
      </p>
     </li>
     <li class="listitem">
      <p>
       Tune some settings in the Windows registry (see below)
      </p>
     </li>
    </ul>
   </div>
   <div class="important" style="margin-left: 0.5in; margin-right: 0.5in;">
    <div class="admon-title">
     Important
    </div>
    <p>
     The following procedure involves modifying the Windows
              registry. Before you modify the registry, make sure to
              back it up and make sure that you understand how to
              restore it if a problem occurs. For information about how
              to back up, restore, and edit the registry, view the
              following article in the Microsoft Knowledge Base:
     <a class="ulink" href="http://support.microsoft.com/kb/256986/EN-US/" target="_blank">
      http://support.microsoft.com/kb/256986/EN-US/
     </a>
     .
    </p>
   </div>
   <div class="orderedlist">
    <ol class="orderedlist" type="1">
     <li class="listitem">
      <p>
       Start Registry Editor
                (
       <code class="filename">
        Regedt32.exe
       </code>
       ).
      </p>
     </li>
     <li class="listitem">
      <p>
       Locate the following key in the registry:
      </p>
      <div class="copytoclipboard-wrapper">
       <pre class="programlisting one-line language-none"><div class="docs-select-all right" id="sa66918406"><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-none">HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters</code></pre>
      </div>
     </li>
     <li class="listitem">
      <p>
       On the
       <code class="literal">
        Edit
       </code>
       menu, click
       <code class="literal">
        Add
                Value
       </code>
       , and then add the following registry
                value:
      </p>
      <div class="copytoclipboard-wrapper">
       <pre class="programlisting language-none"><div class="docs-select-all right" id="sa15797377"><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-none">Value Name: MaxUserPort
Data Type: REG_DWORD
Value: 65534</code></pre>
      </div>
      <p>
       This sets the number of ephemeral ports available to any
                user. The valid range is between 5000 and 65534
                (decimal). The default value is 0x1388 (5000 decimal).
      </p>
     </li>
     <li class="listitem">
      <p>
       On the
       <code class="literal">
        Edit
       </code>
       menu, click
       <code class="literal">
        Add
                Value
       </code>
       , and then add the following registry
                value:
      </p>
      <div class="copytoclipboard-wrapper">
       <pre class="programlisting language-none"><div class="docs-select-all right" id="sa82816689"><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-none">Value Name: TcpTimedWaitDelay
Data Type: REG_DWORD
Value: 30</code></pre>
      </div>
      <p>
       This sets the number of seconds to hold a TCP port
                connection in
       <code class="literal">
        TIME_WAIT
       </code>
       state before
                closing. The valid range is between 30 and 300 decimal,
                although you may wish to check with Microsoft for the
                latest permitted values. The default value is 0x78 (120
                decimal).
      </p>
     </li>
     <li class="listitem">
      <p>
       Quit Registry Editor.
      </p>
     </li>
     <li class="listitem">
      <p>
       Reboot the machine.
      </p>
     </li>
    </ol>
   </div>
   <p>
    Note: Undoing the above should be as simple as deleting the
            registry entries you've created.
   </p>
  </div>
 </div>
 <br/>
</div>
