You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Laravel无法使用php artisan migrate连接Amazon RDS求助

Troubleshooting Laravel's php artisan migrate Failure with AWS RDS (While MySQL Workbench Connects)

Hey there, I’ve run into this exact scenario a few times—when Workbench connects fine but Laravel throws errors, it almost always boils down to configuration mismatches or server-specific network/permissions issues. Let’s walk through the most likely fixes step by step:

1. Double-Check Your .env File (And Clear Config Cache)

First, let’s rule out the simplest culprits:

  • Verify every database parameter matches your RDS setup exactly:
    • DB_HOST: Make sure this is your full RDS endpoint (e.g., mydb.abc123.us-east-1.rds.amazonaws.com—no typos, no extra spaces)
    • DB_PORT: Default is 3306 for MySQL; confirm RDS isn’t using a custom port
    • DB_DATABASE, DB_USERNAME, DB_PASSWORD: Match the values you set when creating the RDS instance (watch for accidental spaces around these values—.env is strict about that!)
  • Laravel caches config values, so if you’ve made changes recently, run:
    php artisan config:clear
    
    This ensures Laravel picks up your latest .env settings instead of using cached old values.

2. Verify config/database.php Isn’t Overriding .env Values

Check your config/database.php file’s mysql connection array. Make sure it’s pulling values from .env instead of hardcoding them. For example:

'mysql' => [
    'driver' => 'mysql',
    'host' => env('DB_HOST', '127.0.0.1'),
    'port' => env('DB_PORT', '3306'),
    'database' => env('DB_DATABASE', 'forge'),
    'username' => env('DB_USERNAME', 'forge'),
    'password' => env('DB_PASSWORD', ''),
    // ... other settings
],

If any of these fields are hardcoded (e.g., 'host' => 'localhost'), that’ll override your .env and break the connection.

3. Ensure Your Laravel Server Has Network Access to RDS

Even if Workbench connects, your Laravel server (e.g., EC2 instance) might not have the right network permissions:

  • RDS Security Group Inbound Rules: Confirm your server’s public IP (or its VPC subnet CIDR) is allowed to access RDS on port 3306. Workbench works because your local IP is in the rules, but the server’s IP/subnet might be missing.
  • Server Outbound Rules: Check the security group attached to your Laravel server—does it allow outbound traffic on port 3306?
  • VPC Routing: If your RDS is in a private subnet, make sure your server is in the same VPC and has a route table that allows communication with the RDS subnet.

A quick way to test this: SSH into your Laravel server and run the MySQL command-line client to connect directly to RDS:

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

If this fails, you know the issue is network-related (fix security groups/routing first). If it works, move on to the next steps.

4. Check RDS Database User Permissions

Your RDS user might be restricted to only allow connections from your local IP (where Workbench runs), not your server’s IP:

  • Log into RDS via Workbench, then run this query to check your user’s allowed hosts:
    SELECT user, host FROM mysql.user WHERE user = 'your-db-username';
    
  • If the host value is your local IP (e.g., 192.168.1.100), you need to update it to allow your server’s IP or all hosts (use % for all, though this is less secure—prefer your server’s subnet CIDR if possible):
    GRANT ALL PRIVILEGES ON your-db-name.* TO 'your-db-username'@'%' IDENTIFIED BY 'your-db-password';
    FLUSH PRIVILEGES;
    

5. Enable SSL Connection (If RDS Requires It)

AWS RDS often enforces SSL connections for MySQL. If your Laravel app isn’t configured to use SSL, it’ll fail to connect:

  • Add this line to your .env file:
    DB_SSL_MODE=required
    
  • Then update your config/database.php’s mysql array to include the SSL mode:
    'mysql' => [
        // ... existing settings
        'sslmode' => env('DB_SSL_MODE', 'prefer'),
    ],
    

For some RDS instances, you might also need to specify the AWS SSL certificate, but starting with DB_SSL_MODE=required usually fixes the issue.

6. Check for Laravel-Specific Database Driver Issues

If all else fails, make sure your Laravel server has the required MySQL extensions installed. For example, on Ubuntu/Debian:

sudo apt-get install php-mysql

Then restart your web server (e.g., Nginx or Apache) to load the extension.


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

火山引擎 最新活动