Laravel项目无法连接XAMPP数据库问题求助
Let's work through fixing this SQLSTATE[HY000] [1045] Access denied error you're hitting when running php artisan migrate—I've been in this exact spot before, so let's break it down step by step.
1. First, Confirm MySQL is Running in XAMPP
Start with the basics: fire up your XAMPP control panel and make sure the MySQL module shows a green "Running" status. It's easy to overlook that the service stopped unexpectedly, so if it's not running, click "Start" to kick it off.
2. Verify Your MySQL Root Credentials & Database Exists
Even though you set an empty password in your configs, let's double-check if MySQL actually allows that:
- Open the XAMPP Shell (click the "Shell" button in the control panel)
- Run this command to try logging into MySQL:
(Skip themysql -u root-pflag since you're using an empty password)- If this fails, your
rootuser might have a password set (some XAMPP versions sneak in a default password during installation). If that's the case, update your.envandconfig/database.phpwith the correct password. - If you log in successfully, run this SQL command to check if your target database exists:
IfSHOW DATABASES;styde_cursoisn't listed, create it manually with:CREATE DATABASE styde_curso;
- If this fails, your
3. Hunt for Typos in Configuration Files
Typos are the silent killer here. Double-check these details in both .env and config/database.php:
- Ensure
DB_HOSTis set tolocalhostor127.0.0.1(sometimes127.0.0.1works more reliably thanlocalhostfor XAMPP MySQL) - Confirm
DB_DATABASEis exactlystyde_curso—no extra spaces, and watch for case sensitivity (it matters on some operating systems) - Make sure
DB_PASSWORDis truly empty (no hidden spaces after the equals sign—.envfiles are super sensitive to whitespace) - In
config/database.php, under themysqlconnection array, verify the values pull correctly from your.env(they should look likeenv('DB_DATABASE', 'default'))
4. Clear All Caches (Not Just Config)
Sometimes clearing only the config cache isn't enough. Run these commands in order to wipe out any stale cached settings:
php artisan config:clear php artisan cache:clear php artisan route:clear php artisan view:clear
Pro tip: Avoid using php artisan config:cache in development—it compiles your configs into a single file, so changes to .env won't take effect until you clear the cache again.
5. Test the Connection Outside Laravel
To rule out Laravel-specific issues, create a simple PHP script in your XAMPP htdocs (or Laravel public) folder to test the MySQL connection directly:
<?php $servername = "localhost"; $username = "root"; $password = ""; $dbname = "styde_curso"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; $conn->close(); ?>
Run this in your browser. If it fails, the problem is with your XAMPP MySQL setup, not Laravel. If it works, then the issue is somewhere in Laravel's environment loading.
6. Confirm Laravel is Loading the Right .env File
Make sure your .env file is in the root of your Laravel project (not a subfolder). Also, check that APP_ENV in .env is set to local (the default for development)—if you have environment-specific files like .env.local, this ensures Laravel uses the right one.
内容的提问来源于stack exchange,提问作者Larry Betancourt

