执行php artisan migrate:install失败,MySQL权限拒绝问题求助
Hey there! I see you're hitting a database access error with Laravel 6 even though you can connect to MySQL just fine via mysqlshow. Let's walk through some common fixes that usually resolve this:
1. Clear Laravel's Configuration Cache
Laravel caches config files to boost performance, which means changes to your .env file might not take effect immediately. Run these commands in your project root to refresh the cache:
php artisan config:clear php artisan cache:clear
This forces Laravel to read the latest values from your .env instead of relying on stale cached settings.
2. Double-Check Your .env File Formatting
Even tiny formatting issues can break your database connection:
- Avoid extra spaces around credentials. For example, skip
DB_PASSWORD= "your-password"(space after equals sign) — useDB_PASSWORD="your-password"orDB_PASSWORD=your-passwordinstead. - Confirm you're using the correct Laravel 6 environment variables, and don't forget to set your target database name:
DB_CONNECTION=mysql DB_HOST=localhost DB_PORT=3306 DB_DATABASE=rm1 # Match this to the database you want Laravel to use DB_USERNAME=root DB_PASSWORD=your_env_password_here
3. Switch DB_HOST from localhost to 127.0.0.1
MySQL handles localhost and 127.0.0.1 differently on Windows: localhost uses a named pipe, while 127.0.0.1 uses TCP/IP. Sometimes Laravel has trouble with the pipe connection. Update your .env to:
DB_HOST=127.0.0.1
4. Update MySQL User Authentication Plugin
MySQL 8.0 defaults to caching_sha2_password, but older PHP setups (including some Laravel 6 environments) struggle with this. Switch your root user to the more compatible mysql_native_password plugin:
- Connect to MySQL via Workbench.
- Run these SQL commands:
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your_env_password'; FLUSH PRIVILEGES; - Restart your MySQL service to apply the change.
5. Verify config/database.php Isn't Overriding .env Settings
Check your config/database.php file to ensure the MySQL configuration pulls values from your .env instead of hardcoding them. 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', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), // ... other default settings ],
If you see hardcoded values here (like 'username' => 'root' without the env() wrapper), that could be overriding your .env settings.
Give these steps a try, and let me know if any of them get your artisan commands working again!
内容的提问来源于stack exchange,提问作者R Mac




