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

更换电脑后Laravel 8执行php artisan migrate报错SQLSTATE[HY000][2002]

Laravel 8迁移报错SQLSTATE[HY000] [2002]的解决办法

你遇到的错误信息如下:

SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from information_schema.tables where table_schema = grain and table_name = migrations and table_type = 'BASE TABLE')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:712
    708▕         // If an exception occurs when attempting to run a query, we'll format the error
    709▕         // message to include the bindings with SQL, which will make this exception a
    710▕         // lot more helpful to the developer instead of just the database's errors.
    711▕         catch (Exception $e) {
  ➜ 712▕             throw new QueryException(
    713▕                 $query, $this->prepareBindings($bindings), $e
    714▕             );
    715▕         }
    716▕     }

      +33 vendor frames 
  34  artisan:35
      Illuminate\Foundation\Console\Kernel::handle(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

你的.env数据库配置:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=grain
DB_USERNAME=root
DB_PASSWORD=

针对换电脑后出现的这个问题,试试下面这些解决步骤:

  • 检查MySQL服务是否启动
    换电脑后大概率没开启MySQL服务,按你的系统操作:

    • Windows:打开「服务」管理器,找到MySQL服务并启动
    • Mac:终端执行 brew services start mysql(用brew安装的情况)
    • Linux:终端执行 sudo systemctl start mysql
  • 配置MySQL Socket路径(Mac/Linux适用)
    很多时候是Laravel默认的socket路径和MySQL实际路径不匹配导致的。先找到MySQL的socket路径,比如Mac上通常是/tmp/mysql.sock,然后在.env里添加:

    DB_SOCKET=/tmp/mysql.sock
    
  • 清除Laravel配置缓存
    换电脑后可能残留了旧环境的配置缓存,执行命令:

    php artisan config:clear
    php artisan cache:clear
    
  • 确认目标数据库存在
    登录MySQL控制台,执行命令创建数据库(如果还没创建的话):

    CREATE DATABASE IF NOT EXISTS grain;
    
  • 检查MySQL权限与端口
    确认root用户有权限访问grain数据库,同时检查3306端口是否被其他程序占用,确保MySQL确实在这个端口运行。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 16:25:19