一、什么Laravel任务调度
Laravel 的命令调度器提供了一种全新的方法来管理服务器上的定时任务。调度器允许您在 Laravel 应用程序本身中流畅且有表达力地定义命令调度。
二、遇到问题
写完任务调度代码之后,查看调度任务和它们下一次计划运行的概况,使用 schedule:list Artisan 命令:
php artisan schedule:list
报如下错误:
Illuminate\Database\QueryException
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel12.cache_locks' doesn't exist (Connection: mysql, SQL: update `cache_locks` set `owner` = SxRnJgzKqHTx6Z6n, `expiration` = 1743149223 where `key` = laravel_cache_framework\schedule-0dd41b6cf46707cbf5b912f0cf0cf936b5618f01 and (`owner` = SxRnJgzKqHTx6Z6n or `expiration` <= 1743062823))
at vendor\laravel\framework\src\Illuminate\Database\Connection.php:823
819▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
820▕ );
821▕ }
822▕
➜ 823▕ throw new QueryException(
824▕ $this->getName(), $query, $this->prepareBindings($bindings), $e
825▕ );
826▕ }
827▕ }
1 [internal]:0
Illuminate\Console\Scheduling\ScheduleListCommand::Illuminate\Console\Scheduling\{closure}(Object(Illuminate\Console\Scheduling\Event))
2 vendor\laravel\framework\src\Illuminate\Database\Connection.php:590
PDOException::("SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel12.cache_locks' doesn't exist")
三、问题原因
遇到的错误表示 Laravel 在查询 cache_locks 表时,发现该表不存在。通常这是因为使用了 Laravel 的调度程序并启用了缓存锁定机制,但缺少了该表。
四、解决方法
1、方法一
创建缺失的表:Laravel 会使用一个名为 cache_locks 的表来管理调度任务的锁定机制。当使用数据库缓存时,Laravel 期望该表存在。你可以通过运行以下 Artisan 命令来生成创建表的迁移文件:
php artisan cache:table
这会生成一个迁移文件,用于创建 cache_locks 表。
运行迁移:生成迁移文件后,你需要运行它来在数据库中创建该表:
php artisan migrate
2、方法二
修改env配置文件,找到
CACHE_STORE=database
将其改为如下
CACHE_STORE=file
这里把他设置为文件,因为laravel12默认把存储方式设置为database数据库,我们改为file文件就可以。再次执行,就不在报错了。
文章评论(0)