You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从本地主机连接云数据库时出现HY000/2002错误求助

Troubleshooting MySQL 2002 Connection Error to Cloud Database

Hey there, let's figure out why your cloud database connection is failing with that timeout error. I've run into this exact issue plenty of times, so here are the most common things to check step by step:

1. Verify Cloud Database Firewall/IP Whitelist

Cloud databases are locked down by default to prevent unauthorized access. First, head to your cloud provider's database console (like Alibaba Cloud, Tencent Cloud, AWS RDS, etc.) and check the security group or IP whitelist settings. You need to add your local machine's public IP address (not your private 192.168.x.x or 127.0.0.1) to this list. Without this, the database will silently reject your connection attempts, leading to that timeout message.

2. Check Database Port Settings

Your code doesn't specify a port, so it's using the default MySQL port 3306. But many cloud providers let you customize the port for security reasons.

  • Go to your database console to confirm the actual port number.
  • Update your connection code to include the port:
    $servername = "27.111.132.11";
    $username = "root";
    $password = "123";
    $dbname = "test";
    $port = 3306; // Replace with your cloud DB's actual port
    
    // Create connection with port
    $conn = new mysqli($servername, $username, $password, $dbname, $port);
    

Also, make sure your local firewall or antivirus isn't blocking outgoing traffic on that port. You can test connectivity with a command like:

telnet 27.111.132.11 3306

Or if you have nc installed:

nc -zv 27.111.132.11 3306

If these commands fail, the port is blocked either locally or on the cloud side.

3. Confirm Credentials and User Permissions

Cloud databases often restrict the root user to local access only for security. Double-check:

  • Is your username allowed to connect from external IPs? In MySQL, user permissions look like username@% (where % means any remote IP) instead of username@localhost.
  • Did you enter the correct password? Typos, case sensitivity, or special characters can easily break connections.
  • Does the test database actually exist on the cloud instance? You can verify this via the cloud console or a local MySQL client that has access.

4. Check Cloud Database Instance Status

Sometimes the issue isn't on your end at all. Log into your cloud provider's console and make sure the database instance is running normally—not in maintenance mode, stopped, or experiencing an outage. A restarting or failed instance will also cause connection timeouts.

5. Test Basic Network Connectivity

First, ping the cloud database IP to see if your machine can reach it:

ping 27.111.132.11

If ping fails, there might be a network issue between your local machine and the cloud server. Also, confirm that the cloud database's public IP hasn't changed (most are static, but it's worth checking).

Bonus: Add Detailed Error Debugging

To get more specific info, update your code to print the full MySQL error details:

$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error . " | Error Code: " . $conn->connect_errno);
}

This can give you a more precise error code to narrow down the problem.


内容的提问来源于stack exchange,提问作者Md.shahidul islam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:12:44