Laravel 5生产环境远程MySQL连接异常问题求助
Let’s break down why this is happening and how to fix it—there are a few common culprits here:
1. Double-Check Your Production Environment Variables
First, confirm your production .env file has the correct values for the secondary database. It’s easy to accidentally copy local values or mistype the remote host. Open your production .env and verify these lines:
DB_HOST_2=cherokee.websitewelcome.com DB_PORT_2=3306 DB_DATABASE_2=dbname DB_USERNAME_2=dbusr DB_PASSWORD_2=dbpass
If any of these are missing or set to your local server’s host (eu01.server.plus), that’s exactly why you’re seeing the error. Fix them and save the file.
2. Clear Laravel’s Configuration Cache
Laravel caches config files in production for performance. If you updated your .env or config files after caching, the old (wrong) settings are still being used. Run these commands on your production server:
php artisan config:clear php artisan config:cache
This wipes the old cache and rebuilds it with your current settings.
3. Check for Environment-Specific Config Overrides
Some teams use environment-specific config folders (like config/production/) that override the base config/database.php. Look for a database.php file in a production-specific config directory—if it exists, ensure the mysql_remote connection there matches your intended remote settings, not the local server.
4. Verify Remote MySQL User Permissions
Let’s clarify the error message: Access denied for user 'dbusr'@'eu01.server.plus' means the remote MySQL server (cherokee.websitewelcome.com) is rejecting your connection because it doesn’t recognize the user dbusr connecting from your production server’s IP (eu01.server.plus).
Locally, your connection works because your local IP is allowed by the remote MySQL user. On production, you need to grant access to your production server’s IP:
- Log into the remote MySQL server using an admin user.
- Run this SQL command to allow your production server to connect:
GRANT ALL PRIVILEGES ON dbname.* TO 'dbusr'@'eu01.server.plus' IDENTIFIED BY 'dbpass'; FLUSH PRIVILEGES;
If you need to allow connections from any host (less secure but sometimes necessary):
GRANT ALL PRIVILEGES ON dbname.* TO 'dbusr'@'%' IDENTIFIED BY 'dbpass'; FLUSH PRIVILEGES;
Replace dbname, dbusr, and dbpass with your actual values.
5. Test the Connection Directly
To rule out Laravel-specific issues, test the connection from your production server using the MySQL command line:
mysql -h cherokee.websitewelcome.com -u dbusr -p dbname
Enter your password when prompted. If this fails with the same access denied error, the problem is definitely with remote MySQL permissions, not your Laravel config. If it works, the issue lies in Laravel’s config or caching.
Start with steps 1 and 2 first—those are the most common fixes. If those don’t resolve the issue, move on to checking permissions and direct connection testing.
内容的提问来源于stack exchange,提问作者Daniel P.

