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

Laravel 5.1连接RDS MySQL报错SQLSTATE[HY000] [2002]连接拒绝求助

排查Laravel 5.1连接RDS MySQL出现SQLSTATE[HY000] [2002] Connection refused的步骤

Hey there, let's walk through this step by step—since you mentioned the RDS works fine on another EC2 instance, the issue is almost certainly tied to network/security group configurations or small Laravel setup details. Let's break it down:

  • 先跳过Laravel,验证基础网络连通性
    Log into your problematic EC2 instance and run these commands to test if you can reach the RDS port directly:

    # 使用telnet测试
    telnet your-rds-full-endpoint.rds.amazonaws.com 3306
    # 或者用nc(如果telnet没装)
    nc -zv your-rds-full-endpoint.rds.amazonaws.com 3306
    

    If this fails, the problem has nothing to do with Laravel—it's strictly a network/security group block. If it works, move on to check your app config.

  • 重点检查RDS的安全组入站规则(不是EC2的!)
    You said you opened port 3306, but make sure it's the RDS's security group that has the correct inbound rule. The rule should allow traffic from:

    • Your problematic EC2 instance's security group ID (this is the most flexible option, better than using an IP)
    • Or the private IP of your EC2 instance (if both are in the same VPC)
      Don't forget: EC2's outbound rules need to allow TCP 3306 traffic too, but since another EC2 works, this is less likely the issue—but double-check just in case.
  • Verify Laravel's database configuration details
    Open your .env file (or config/database.php if you're not using .env) and confirm these:

    • DB_HOST: Must be the full RDS endpoint (e.g., my-rds-instance.c1234567890.us-east-1.rds.amazonaws.com—don't use a shortened name or wrong IP)
    • DB_PORT: Ensure it's set to 3306 (RDS's default, unless you changed it during setup)
    • DB_USERNAME/DB_PASSWORD: Make sure these match exactly what you're using on the working EC2 instance—typos happen!
      Also, Laravel 5.1 caches config files by default. If you recently updated .env, run this to clear the cache:
    php artisan config:clear
    
  • Test with a MySQL client directly on the EC2
    Install the MySQL client on your EC2 (e.g., sudo yum install mysql for Amazon Linux, sudo apt-get install mysql-client for Ubuntu) then run:

    mysql -h your-rds-endpoint -u your-db-username -p
    

    If this connects successfully, the network is good, and the issue is definitely in Laravel's config or cache. Recheck your .env values and clear the config cache again.

  • Check VPC/Subnet routing (if applicable)
    If your EC2 and RDS are in different VPCs, ensure RDS has public access enabled and your EC2 can reach the public internet. If they're in the same VPC, confirm the subnet route tables allow communication between the EC2's subnet and RDS's subnets.


内容的提问来源于stack exchange,提问作者reshma

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 07:16:03