Laravel 5.4中.env配置无效,执行php artisan migrate报错求助
Hey there, let's troubleshoot this 1045 Access Denied error you're hitting when running php artisan migrate. The error message clearly shows Laravel is trying to connect to MySQL with an empty username, which points to issues with your .env configuration not being applied correctly. Here's how to fix it step by step:
1. Fill in the Missing Database Name
Your .env has an empty DB_DATABASE= value, but the error references table_schema = atp_db—so that's likely the database you intended to use. Update the line in your .env file to:
DB_DATABASE=atp_db
2. Clear Config Cache (Critical Step!)
Laravel sometimes caches configuration values, which might be causing it to ignore your updated .env. Run this command to clear the cache:
php artisan config:clear
3. Verify Your Database Credentials & Connection
First, confirm the
rootuser can connect to MySQL without a password. Test this directly via the terminal:mysql -u rootIf this fails, you'll need to either set a password for the
rootuser (and updateDB_PASSWORDin.envto match) or adjust MySQL's user permissions to allowrootaccess without a password fromlocalhost.Also, make sure the
atp_dbdatabase actually exists in MySQL. If it doesn't, create it with this command:mysql -u root -e "CREATE DATABASE atp_db;"
4. Double-Check config/database.php
Ensure your MySQL connection in config/database.php is correctly pulling values from .env—it should look like this:
'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', ''), 'username' => env('DB_USERNAME', ''), 'password' => env('DB_PASSWORD', ''), // ... other default configs ],
If there are hardcoded values here instead of env() calls, they'll override your .env settings.
Once you've completed these steps, run php artisan migrate again—it should connect to your database successfully and run the migrations.
内容的提问来源于stack exchange,提问作者Najib jamshidi

