Can't connect to [local] MySQL server

本文介绍了当遇到无法连接到MySQL服务器的问题时可能的原因及解决办法,包括检查服务器状态、配置选项、防火墙设置等。
B.1.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 don't specify a host name or if you specify the special host name localhost.

If the MySQL server is running on Windows, you can connect via 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 don't give a host name when connecting to mysqld, a MySQL client first tries to connect to the named pipe. If that doesn't 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.11.2.3, “Starting and Troubleshooting the MySQL 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 number 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 quotes 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 127.0.0.1 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=127.0.0.1, 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.1.2.2.1, “Connection to MySQL Server Failing on Windows.

  • You are running on a system that uses MIT-pthreads. If you are running on a system that doesn't have native threads, mysqld uses the MIT-pthreads package. See Section 2.1.1, “Operating Systems Supported by MySQL Community Server”. However, not all MIT-pthreads versions support Unix socket files. On a system without socket file support, you must always specify the host name explicitly when connecting to the server. Try using this command to check the connection to the server:

    shell> mysqladmin -h `hostname` version
  • 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.1.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.1.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.1.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.1.2.2.1. 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 doesn't allow for enough ephemeral (short-lived) ports to serve those connections.

By default, Windows allows 5000 ephemeral (short-lived) TCP ports to the user. After any port is closed it will remain in a TIME_WAIT status for 120 seconds. This status allows the connection to be reused at a much lower cost than reinitializing a brand new connection. However, the port will not be available again until this time expires.

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)

IMPORTANT: 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 the registry 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: http://support.microsoft.com/kb/256986/EN-US/.

  1. Start Registry Editor (Regedt32.exe).

  2. Locate the following key in the registry:

    HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Tcpip/Parameters
  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 0 (zero) and 300 (decimal). 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.

 

原文:http://dev.mysql.com/doc/refman/5.1/en/can-not-connect-to-server.html

<think>好的,我现在需要解决用户遇到的无法连接到本地MySQL服务器的问题,错误提示是“Can’t connect to MySQL server on ‘localhost’ (10055)”。首先,我得先理解这个错误的原因。用户提到服务器运行一段时间后出现这个问题,重启后暂时解决,但之后又复发。这说明问题可能和资源耗尽或者配置有关。 根据引用[1]中的情况,用户检查了端口占用情况,但没发现占用,重启MySQL服务失败,最终重启服务器才恢复。这可能意味着问题不是端口被占用,而是其他资源问题,比如文件描述符耗尽或者内存不足。另外,引用[2]提到端口配置的问题,用户可能检查了默认端口3306,但实际MySQL可能运行在另一个端口,比如3307,导致连接失败。所以需要确认MySQL实际监听的端口。 接下来,我需要整理解决步骤。首先检查MySQL服务是否正在运行,如果服务停止,自然无法连接。然后确认端口是否正确,使用SHOW GLOBAL VARIABLES LIKE 'PORT'命令查看实际端口。如果端口被其他程序占用,需要使用netstat或类似工具检查。另外,检查防火墙设置,确保没有阻止MySQL的端口。还需要查看MySQL的错误日志,寻找更详细的错误信息,这可能帮助找到根本原因,比如内存不足或文件描述符限制。 用户的环境是Windows 2003,可能有些过时,系统资源限制较低,容易出现资源耗尽的情况。需要检查系统的最大连接数、打开文件数等参数是否过低,适当调整MySQL的配置,比如max_connections、table_open_cache等。同时,检查是否有长时间运行的查询或未释放的连接,导致资源泄漏。 此外,引用[3]中的连接字符串显示尝试使用TCP协议连接到localhost,可能需要检查MySQL是否配置为监听TCP端口,而不是仅通过套接字文件连接。在my.ini或my.cnf配置文件中,确保有bind-address=0.0.0.0或127.0.0.1,并正确设置端口。 最后,如果问题周期性出现,可能需要设置监控,跟踪系统资源使用情况,如内存、CPU、文件句柄等,及时发现资源瓶颈。定期重启MySQL服务可能作为临时解决方案,但长期需要优化配置和查询,避免资源耗尽。</think>### 逐步解决方案:无法连接本地MySQL服务器(错误10055) #### 1. **确认MySQL服务状态** - **步骤**:检查MySQL服务是否正常运行。 - Windows:打开“服务”管理器(services.msc),找到MySQL服务,确保状态为“已启动”。 - 命令行:执行`net start mysql`(服务名可能不同,需替换为实际名称)。 - **问题**:若服务未启动,可能因端口冲突或资源不足导致启动失败[^1]。 #### 2. **验证MySQL监听端口** - **步骤**:登录MySQL查看实际使用的端口: ```sql SHOW GLOBAL VARIABLES LIKE 'PORT'; -- 返回示例:3306或3307[^2] ``` - **问题**:若配置文件中端口被修改(如改为3307),客户端需同步调整连接参数。 #### 3. **检查端口占用与防火墙** - **步骤**: 1. 使用命令检查端口占用(以3306为例): ```bash netstat -ano | findstr :3306 ``` 2. 确认防火墙是否放行该端口(Windows防火墙或第三方安全软件)。 #### 4. **分析MySQL错误日志** - **步骤**:在MySQL配置文件(如`my.ini`)中查找日志路径: ``` log-error="C:/mysql_logs/error.log" ``` - **关键信息**:日志可能提示“Too many connections”“内存不足”或“文件描述符耗尽”[^1]。 #### 5. **调整系统与MySQL配置** - **优化方向**: - **最大连接数**:修改`my.ini`中`max_connections`(默认151),例如: ``` max_connections=500 ``` - **文件描述符限制**:在Windows中通过注册表调整`HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\GDIProcessHandleQuota`。 - **内存分配**:检查`innodb_buffer_pool_size`等参数是否合理。 #### 6. **排查资源泄漏** - **步骤**: 1. 执行`SHOW PROCESSLIST;`查看当前连接与查询状态。 2. 检查应用程序是否未正确关闭数据库连接(如PHP中使用`mysqli_close()`)。 #### 7. **临时措施与长期监控** - **临时方案**:定时重启MySQL服务或服务器(不推荐长期使用)。 - **长期方案**:部署监控工具(如Zabbix),跟踪内存、连接数、文件句柄等指标。 --- ### 相关问题 1. **如何通过修改注册表解决Windows系统的文件描述符限制?** 2. **MySQL错误日志中常见的资源类错误有哪些?如何针对性优化?** 3. **在PHP中如何确保数据库连接正确释放以避免泄漏?** --- [^1]: 环境:Windows 2003 + IIS + PHP + MYSQL,服务器运行一段时间后提示端口未占用但无法连接,需重启恢复。 : 通过`SHOW GLOBAL VARIABLES`可查询MySQL实际监听端口,避免客户端配置不符。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值