Documentation Home
MySQL 5.5 Reference Manual
Related Documentation Download this Manual Excerpts from this Manual

B.5.2.2 Can't connect to [local] MySQL server

A MySQL client on Unix can connect to the mysqld server in two different ways: By using a Unix socket file to connect through a file in the file system (default /tmp/mysql.sock), 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 localhost.

If the MySQL server is running on Windows, you can connect using TCP/IP. If the server is started with the --enable-named-pipe option, 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 MySQL by default. If you do not give a host name when connecting to mysqld, 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 . as the host name.

The error (2002) Can't connect to ... 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.

The error (2003) Can't connect to MySQL server on 'server' (10061) 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.

Start by checking whether there is a process named mysqld running on your server host. (Use ps xa | grep mysqld on Unix or the Task Manager on Windows.) If there is no such process, you should start the server. See Section 2.10.2, “Starting the Server”.

If a mysqld 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. host_ip represents the IP address of the machine where the server is running.

shell> mysqladmin version
shell> mysqladmin variables
shell> mysqladmin -h `hostname` version variables
shell> mysqladmin -h `hostname` --port=3306 version
shell> mysqladmin -h host_ip version
shell> mysqladmin --protocol=SOCKET --socket=/tmp/mysql.sock version

Note the use of backticks rather than forward quotation marks with the hostname command; these cause the output of hostname (that is, the current host name) to be substituted into the mysqladmin command. If you have no hostname command or are running on Windows, you can manually type the host name of your machine (without backticks) following the -h option. You can also try -h to connect with TCP/IP to the local host.

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 --skip-networking, it will not accept TCP/IP connections at all. If the server was started with --bind-address=, it will listen for TCP/IP connections only locally on the loopback interface and will not accept remote connections.

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 the Windows XP personal firewall may need to be configured not to block the MySQL port.

Here are some reasons the Can't connect to local MySQL server error might occur:

  • mysqld is not running on the local host. Check your operating system's process list to ensure the mysqld process is present.

  • 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: Section B., “Connection to MySQL Server Failing on Windows”.

  • Someone has removed the Unix socket file that mysqld uses (/tmp/mysql.sock by default). For example, you might have a cron job that removes old files from the /tmp directory. You can always run mysqladmin version to check whether the Unix socket file that mysqladmin is trying to use really exists. The fix in this case is to change the cron job to not remove mysql.sock or to place the socket file somewhere else. See Section B.5.4.5, “How to Protect or Change the MySQL Unix Socket File”.

  • You have started the mysqld server with the --socket=/path/to/socket 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 --socket option when you run client programs. You also need to ensure that clients have permission to access the mysql.sock file. To find out where the socket file is, you can do:

    shell> netstat -ln | grep mysql

    See Section B.5.4.5, “How to Protect or Change the MySQL Unix Socket File”.

  • You are using Linux and one server thread has died (dumped core). In this case, you must kill the other mysqld threads (for example, with kill or with the mysql_zap script) before you can restart the MySQL server. See Section B.5.4.2, “What to Do If MySQL Keeps Crashing”.

  • 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 mysqld with a --socket option that specifies a socket file name in a directory where the server can create it and where client programs can access it.

If you get the error message Can't connect to MySQL server on some_host, you can try the following things to find out what the problem is:

  • Check whether the server is running on that host by executing telnet some_host 3306 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 telnet: Unable to connect to remote host: Connection refused, then there is no server running on the given port.

  • If the server is running on the local host, try using mysqladmin -h localhost variables 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 port variable.)

  • If you are running under Linux and Security-Enhanced Linux (SELinux) is enabled, make sure you have disabled SELinux protection for the mysqld process.

B. Connection to MySQL Server Failing on Windows

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 Can't connect to MySQL server error, the reason might be that Windows does not allow for enough ephemeral (short-lived) ports to serve those connections.

The purpose of TIME_WAIT 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 TIME_WAIT delay prevents this by ensuring that the port cannot be reused until after some time has been permitted for those delayed packets to arrive.

It is safe to reduce TIME_WAIT 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.

Windows permits ephemeral (short-lived) TCP ports to the user. After any port is closed it will remain in a TIME_WAIT status for 120 seconds. The port will not be 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:

  • Windows through Server 2003: Ports in range 1025–5000

  • Windows Vista, Server 2008, and newer: Ports in range 49152–65535

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 TIME_WAIT status you have a good chance for running out of ports. There are two ways to address this problem:

  • Reduce the number of TCP ports consumed quickly by investigating connection pooling or persistent connections where possible

  • Tune some settings in the Windows registry (see below)


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:

  1. Start Registry Editor (Regedt32.exe).

  2. Locate the following key in the registry:

  3. On the Edit menu, click Add Value, and then add the following registry value:

    Value Name: MaxUserPort
    Data Type: REG_DWORD
    Value: 65534

    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).

  4. On the Edit menu, click Add Value, and then add the following registry value:

    Value Name: TcpTimedWaitDelay
    Data Type: REG_DWORD
    Value: 30

    This sets the number of seconds to hold a TCP port connection in TIME_WAIT 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).

  5. Quit Registry Editor.

  6. Reboot the machine.

Note: Undoing the above should be as simple as deleting the registry entries you've created.

Download this Manual
User Comments
  Posted by Rick Measham on November 17, 2004
I was getting this error because the daemon couldn't create a pid file in the requested place. It never told me that it couldn't, it just never wrote it.

Once I made the pid file's location writable by the mysql user, everything started working.
  Posted by Greg Rundlett on August 20, 2005
Using /usr/sbin/mysqld --verbose allowed me to see that there was an unrecognized variable 'old_passwords=1', which I later found out was being set by Debian in /etc/mysql/my.cnf for some backward compatibility with *Debian* software. Removing this entry from the configuration file was all that was needed to get my server working again. This bug has been addressed by the Debian package maintainers:
  Posted by Lennart Zuur on January 28, 2006
This worked for me on SuSe 10.
Disabling IPv6 for MySQLd wil fix the socket creation problem for now.
  Posted by Sam Mehta on April 10, 2006
I faced a similar problem where mysql 5.0.19 (upgraded from 4.1.13) would just not connect from neither perl/java nor php. Command line connections were OK for root but not for other users even they had same privileges as the root user. Problem resolved by correcting the hosts file. Apparently if localhost isnt the first alias for, then you're in for trouble. Same situation with a postgres 8.1 installation. Good luck.
  Posted by Einar Coutin on June 20, 2006
Once you have edited the my.conf under /etc/mysql and removed the bind-host line you must also do the following:

mysql> grant all prvileges on *.* to <userName>@<hostName> identified by 'passwrd'

where the user will own the database acces and the host is the machine you're connecting from.

Thanks to Spyros for this post, which u can find:,34957,56380#msg-56380
  Posted by Michael Mitterer on August 22, 2006
/etc/host.allow or /etc/host.deny

I was not successful with all the above tips (but anyhow THANK YOU). After searching around on my server
I found that I had an entry in /etc/host.allow like this:
mysqld : : allow
mysqld : ALL : deny

All other host except the were disabled on the server!!!
After changing the IP-Address to LOCAL everything works fine.
(Don't forget to restart the portmapper after changing this file)

  Posted by zhang tiefeng on November 4, 2006
I have a case to solve this problem.If compile apache --with-mpm=worker,then mysql_connect() also show this message:
Can't connect to MySQL server on "remote_mysqld"

#cd httpd-2.0.55
#./configure --prefix=/usr/local/apache --enable-so --enable-rewrite --enable-deflate --with-mpm=worker

So if you want connect remote mysql server,pls not use --with-mpm=worker
notice:I use php-4.4.1 and mysql4.0.26

  Posted by Kathryn Wilson on December 1, 2006
Also this may be helpful. Try checking your iptables configuration. First I would stop your iptables services by typing.

/etc/init.c/iptables stop

If when you try to get your MySQL Admin to connect to your host server and it connects then you know that you will need to update them within your system. For a UNIX system this would be in ipf configurations. But you can at least stop them and then restart them until your configuration file is updated.
  Posted by Paul Thao on January 24, 2007
  Posted by Eric Schorvitz on January 10, 2008
As a continuation of David Jung's comments:

To turn off SELinux temporarily: setenforce 0
To turn it back on: setenforce 1

To disable permanently, edit: /etc/selinux/config

To edit the SELinux settings (e.g. allow HTTP traffic - default is NO on installation):
system-config-securitylevel-tui (run this from the command line)
  Posted by Noah Abrahams on May 30, 2008
To follow up on the SELinux issue, the only setting that needs to be changed is as follows:

setsebool -P httpd_can_network_connect_db=1
  Posted by Cristemir Albuquerque on October 6, 2008
Tive o mesmo problema com um servidor Windows 2000 e após a inclusão das chaves do registro o problema foi resolvido.

  Posted by Matt Keenan on April 23, 2009
Ensure version of daemon matches that of the driver.
e.g. Daemon 4.0 and driver 5.1 do not work together the
JDBC driver just hangs trying to get a connection.

So make sure mysqladmin version matches that of your driver.
  Posted by vasu s on July 3, 2010
Check whether your mysql instance is running or not.

1. To start your MySQL instance, go to MySQL/MySQL System Tray Monitor.
2. Tray monitor icon will be displayed in the lower most right corner of your window.
3. Right click the Tray Monitor.
4. Click Start Instance.
5. Now open MySQL/MySQL Query Browser and you will see it working fine now :)

  Posted by Shane Bester on November 30, 2010
if you're still seeing this on windows even after the registry changes, check if applying this KB solves it:

  Posted by leigh tippett on February 24, 2011
This problem for me was to do with my laptop stopping the running process of "mySQL". Just go to task manager, view services running, check if its running which it wasnt then click on the services button at bottom, then standard, find mysql and click start.

That should solve the problem.
  Posted by Prakhar Srivastava on April 25, 2011
I faced a similar problem.
The exact problem that my machine received a new IP through DHCP and my mysql server was binded to the previous IP. This was causing the problem and was resolved as soon as I changed the bind address in my.conf and restarted the server.

  Posted by Andreas Mähr on July 6, 2011
By default MySQL (5.5 version on Windows XP) binds to "localhost" (somehow is not same as localhost) and it will accept connections only from localhost.
Since the OS would convert localhost to automatically, other programs are unable to connect.
(MySQL command line client might be functioning differently Because it can connect using localhost as host name.)

If you want MySQL port to listen on all interfaces add this setting to your my.ini file

Found on
  Posted by Ilguiz Latypov on July 11, 2011
Thanks, Andreas. I think the root cause is Windows installing its IPv6 stack which makes its TCP library resolve the host name "localhost" to what appears to be an IPv6 address "[::]", according to the netstat output,

$ netstat -ano | grep 3306
TCP [::]:3306 [::]:0 LISTENING 5160

The parameter bind-address= appears to force the MySQL server to listen on all IPv4 interfaces.

  Posted by Jukka Petrinen on September 8, 2011
If you have this error message using phpMyAdmin web application, have a look
Found the solution and it made the error go away :)
  Posted by Jon Spencer on August 24, 2012
This relates to running MySQL on Windows 7. I too had the problem of not being able to connect to the MySQL server with an app I wrote. It turns out that the host file in \Windows\System32\etc\hosts did not have a definition for "localhosts". I simply removed the comment "#" for localhost and everything started to work fine.

I am not (yet) a Windows geek, but am a Linux geek, where all of this kind of thing is so much easier to deal with. :-)
  Posted by Ramesh Thuravil on October 23, 2012
The following change worked for me:

Find your MySQL config file (my.cnf is usually in /etc/mysql) and comment out the following line by putting a hash character in front of it as shown:

# bind-address =

Restart the mysql server.
  Posted by Sandesh Mhatre on December 3, 2012
just check if you have assigned access from various(or that particular) host.
while creating user be sure that you've mentioned host.

or in host field put % sign , known as wildcard. to allow user of same name to access database from any host in your network.
  Posted by on December 4, 2012
The Fix suggested by John Spencer worked for me.

Windows 7 (64 Bit)
mysql Ver 14.14 Distrib 5.5.28, for Win64 (x86)

small point - the hosts file is in %windows%\system32\drivers\etc

Sign Up Login You must be logged in to post a comment.