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

Laravel 7数据库连接失败报SQLSTATE[HY000][1698]错误,如何解决?

Fixing SQLSTATE[HY000] [1698] Access Denied for 'root'@'localhost' in Laravel 7

Hey there, let's work through this database connection issue you're hitting in Laravel 7. That error is super common with newer MySQL versions, and I bet I know exactly what's going on—especially since your Laravel 5.4 setup works fine.

What's Causing This?

Starting with MySQL 5.7, the default root user uses the auth_socket authentication plugin instead of password-based authentication. This means MySQL checks if your system user matches the database user (so you can only log in as root via sudo mysql), rather than accepting a password.

Your Laravel 5.4 project is likely running against an older MySQL instance where root was configured with password authentication, which is why it connects without issues.

Solution 1: Update the root User's Authentication Method

If you want to keep using the root user for your Laravel 7 project, follow these steps:

  1. Log into MySQL using system root privileges:
    sudo mysql -u root
    
  2. Switch to the mysql database (where user accounts are stored):
    USE mysql;
    
  3. Update the root user to use password authentication (we'll use an empty password to match your .env):
    ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '';
    
    Note: If you'd prefer to set a secure password instead of leaving it blank, replace the empty quotes with your desired password and update your .env file accordingly.
  4. Flush MySQL privileges to apply the changes:
    FLUSH PRIVILEGES;
    
  5. Exit MySQL, then clear Laravel's config cache again (just to be safe) and restart your server:
    php artisan config:clear
    php artisan serve
    

Using root for your application isn't the most secure practice. Instead, create a separate user for Laravel:

  1. Log into MySQL with sudo mysql -u root as before.
  2. Create a new user (replace your_username and your_password with your preferred credentials):
    CREATE USER 'your_username'@'localhost' IDENTIFIED BY 'your_password';
    
  3. Grant full access to your amazon_services database:
    GRANT ALL PRIVILEGES ON amazon_services.* TO 'your_username'@'localhost';
    
  4. Flush privileges:
    FLUSH PRIVILEGES;
    
  5. Update your .env file to use this new user:
    DB_USERNAME=your_username
    DB_PASSWORD=your_password
    
  6. Clear config cache and restart the server:
    php artisan config:clear
    php artisan serve
    

Either of these methods should fix the access denied error. The second option is better for security, but both will get your Laravel 7 project connected to the database properly.

内容的提问来源于stack exchange,提问作者manikandan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.08 14:07:50