You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel项目无法连接XAMPP数据库问题求助

Troubleshooting Laravel-XAMPP Database Connection Issues

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:
    mysql -u root
    
    (Skip the -p flag since you're using an empty password)
    • If this fails, your root user might have a password set (some XAMPP versions sneak in a default password during installation). If that's the case, update your .env and config/database.php with the correct password.
    • If you log in successfully, run this SQL command to check if your target database exists:
      SHOW DATABASES;
      
      If styde_curso isn't listed, create it manually with:
      CREATE DATABASE styde_curso;
      

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_HOST is set to localhost or 127.0.0.1 (sometimes 127.0.0.1 works more reliably than localhost for XAMPP MySQL)
  • Confirm DB_DATABASE is exactly styde_curso—no extra spaces, and watch for case sensitivity (it matters on some operating systems)
  • Make sure DB_PASSWORD is truly empty (no hidden spaces after the equals sign—.env files are super sensitive to whitespace)
  • In config/database.php, under the mysql connection array, verify the values pull correctly from your .env (they should look like env('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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:54:46