DirectAdmin部署Laravel出现Base table or view not found错误如何解决
问题描述
我在DirectAdmin上安装了Laravel应用,将项目上传至服务器后,通过域名访问时收到如下报错:
[12:41:39] LOG.error: SQLSTATE[42S02]: Base table or view not found: 1146 Table 'captaina_slotsystem.events' doesn't exist (SQL: select * from `events` where `endEvent` > 2021-10-18 12:41:39 and `is_online` = 1 and `show_on_homepage` = 1 order by `startEvent` asc) {"exception":{"errorInfo":["42S02",1146,"Table 'captaina_slotsystem.events' doesn't exist"]}}
该项目在本地PC的XAMPP环境中运行完全正常,我已配置.env文件连接MySQL数据库,确认所有文件均正确上传,但相同文件部署到DirectAdmin后就出现上述错误,不清楚是否是数据库配置有误还是和DirectAdmin设置有关。
项目文件结构如下:
database.php配置文件内容如下:
<?php return [ /* |-------------------------------------------------------------------------- | Default Database Connection Name |-------------------------------------------------------------------------- | | Here you may specify which of the database connections below you wish | to use as your default connection for all database work. Of course | you may use many connections at once using the Database library. | */ 'default' => env('DB_CONNECTION', 'mysql'), /* |-------------------------------------------------------------------------- | Database Connections |-------------------------------------------------------------------------- | | Here are each of the database connections setup for your application. | Of course, examples of configuring each database platform that is | supported by Laravel is shown below to make development simple. | | | All database work in Laravel is done through the PHP PDO facilities | so make sure you have the driver for your particular database of | choice installed on your machine before you begin development. | */ 'connections' => [ 'sqlite' => [ 'driver' => 'sqlite', 'database' => env('DB_DATABASE', database_path('database.sqlite')), 'prefix' => env('DB_TABLE_PREFIX', ''), ], 'sqlite_testing' => [ 'driver' => 'sqlite', 'database' => ':memory:', 'prefix' => env('DB_TABLE_PREFIX', ''), ], 'mysql' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci', 'prefix' => env('DB_TABLE_PREFIX', ''), 'strict' => true, 'engine' => null, ], 'pgsql' => [ 'driver' => 'pgsql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '5432'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => env('DB_TABLE_PREFIX', ''), 'schema' => 'public', 'sslmode' => 'prefer', ], 'sqlsrv' => [ 'driver' => 'sqlsrv', 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', '1433'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => env('DB_TABLE_PREFIX', ''), ], ], /* |-------------------------------------------------------------------------- | Migration Repository Table |-------------------------------------------------------------------------- | | This table keeps track of all the migrations that have already run for | your application. Using this information, we can determine which of | the migrations on disk haven't actually been run in the database. | */ 'migrations' => 'migrations', /* |-------------------------------------------------------------------------- | Redis Databases |-------------------------------------------------------------------------- | | Redis is an open source, fast, and advanced key-value store that also | provides a richer set of commands than a typical key-value systems | such as APC or Memcached. Laravel makes it easy to dig right in. | */ 'redis' => [ 'client' => 'predis', 'default' => [ 'host' => env('REDIS_HOST', '127.0.0.1'), 'password' => env('REDIS_PASSWORD', null), 'port' => env('REDIS_PORT', 6379), 'database' => 0, ], ], ];
请问该错误是数据库配置错误导致的,还是和DirectAdmin的设置有关?
问题解答
该错误本质是线上目标数据库captaina_slotsystem中不存在events表,和DirectAdmin本身的系统设置无关,通常是数据库配置不匹配或者未同步表结构导致的,可按以下步骤排查:
- 核对数据库配置
检查服务器端.env文件的配置项:- 确认
DB_DATABASE值确实为captaina_slotsystem,DirectAdmin创建数据库时会自动在自定义库名前拼接主机账号前缀,不要和本地库名混淆 - 确认
DB_USERNAME对应的数据库账号拥有该库的读写权限,DirectAdmin创建的数据库账号默认格式也为账号名_自定义用户名,避免拼写错误 - 确认未配置额外的
DB_TABLE_PREFIX表前缀,若本地环境有设置表前缀而线上未同步,也会出现找不到表的问题 - 执行
php artisan config:clear清除配置缓存,避免旧配置残留生效
- 确认
- 同步表结构到线上数据库
仅上传项目代码不会自动生成数据库表,需要手动同步表结构:- 若使用Laravel迁移管理表结构,可进入项目根目录执行命令
php artisan migrate自动生成所有表 - 若需要同步本地的测试数据,可导出本地XAMPP中对应数据库的SQL文件,通过DirectAdmin自带的phpMyAdmin导入到线上的
captaina_slotsystem库中
- 若使用Laravel迁移管理表结构,可进入项目根目录执行命令
内容的提问来源于stack exchange,提问作者CaptainAB
相关产品推荐
相关产品推荐

