所以最近我部署了我的 laravel 项目。但是连接不上数据库。我正在使用 sqlite 并且已经创建了 database.sqlite 文件(内部数据库文件夹)。本地一切正常,但远程找不到文件。我已经尝试过以前的解决方案,例如; 数据库(database/database.sqlite)不存在。工匠修补匠的数据库作品
我正在使用数据库路径的默认值,请参见此处:
<?php
use Illuminate\Support\Str;
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',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' => env('DB_FOREIGN_KEYS', true),
],
'mysql' => [
'driver' => 'mysql',
'host' => env('DB_HOST', '127.0.0.1'),
'database' => env('DB_DATABASE', 'forge'),
'password' => env('DB_PASSWORD', ''),
'username' => env('DB_USERNAME', 'forge'),
],
我所有与数据库相关的 .env 变量都设置为空。所以它应该使用默认的 database_path() 函数,它确实这样做了,但是路径不正确,因为它给出了这个错误:
这也是我的 env 文件的样子:
APP_NAME=Laravel
APP_ENV=production
APP_KEY=base64:sKK5gF1A8SsZ1n+MUnBEc760zE9TkAtENbP0arpDtqk=
APP_DEBUG=false
APP_URL=http://admin.dimitrimanen.nl
LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug
DB_CONNECTION=sqlite
#DB_HOST=
#DB_PORT=
#DB_DATABASE=/home/strato/www/di/www.dimitrimanen.nl/htdocs/admin_portal/database/database.sqlite
#DB_USERNAME=
#DB_PASSWORD=
如您所见,我还尝试了我的服务器提供商记录的绝对路径; https://www.strato.nl/faq/hosting/absolute-paden-en-locaties-van-tools/
所以结论不是我。默认路径末尾有 .sqlite,但我的数据库实际上称为 database.db 而不是 sqlite。至少我现在可以在一切正常的情况下平静地死去!
从
config\database.php
下面的代码可以看到,如果env('DB_DATABASE')
路径存在,就会使用它,如果这个路径不存在,就会使用项目中的database_path('database.sqlite')
路径
所以最好的方法是将
env('DB_DATABASE')
留空,然后在默认database\database.sqlite
路径中创建database.sqlite文件。
您的 sqlite 数据库将在
database\database.sqlite
路径中创建。
见
config\database.php
'sqlite' => [
'driver' => 'sqlite',
'url' => env('DATABASE_URL'),
'database' => env('DB_DATABASE',
database_path('database.sqlite')),
'prefix' => '',
'foreign_key_constraints' =>
env('DB_FOREIGN_KEYS', true),
],
变化开始:
1.创建
database\database.sqlite
2.前往
.env
改变
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
到
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=root
DB_PASSWORD=
3.保存更改
4.在
cmd
中的代码下运行
php artisan optimize:clear
// Then
php artisan migrate
// OR
php artisan migrate:fresh
最好在
SQLite Viewer
软件上安装扩展程序VSCode
。