数据库 [] 未配置 Laravel 6.5.2

Database [] not configured Laravel 6.5.2

我正在尝试为新的 PHP Laravel 项目设置服务器。我做了以下步骤

克隆存储库

运行 composer install

创建了一个 .env 文件

APP_NAME=TestApp
APP_ENV=local
APP_KEY=
APP_DEBUG=true
APP_URL=http://localhost

LOG_CHANNEL=stack

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

BROADCAST_DRIVER=log
CACHE_DRIVER=file
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

使用 MySQLWorkBench 创建了一个名为 test1 的模式。 运行php artisan migrate

然后我得到以下错误

  InvalidArgumentException  : Database [mysql] not configured.

  at /Users/isaac/Downloads/TestApp/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:152
    148|         // If the configuration doesn't exist, we'll throw an exception and bail.
    149|         $connections = $this->app['config']['database.connections'];
    150| 
    151|         if (is_null($config = Arr::get($connections, $name))) {
  > 152|             throw new InvalidArgumentException("Database [{$name}] not configured.");
    153|         }
    154| 
    155|         return (new ConfigurationUrlParser)
    156|                     ->parseConfiguration($config);

  Exception trace:

  1   Illuminate\Database\DatabaseManager::configuration("mysql")
      /Users/isaac/Downloads/TestApp/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:115

  2   Illuminate\Database\DatabaseManager::makeConnection("mysql")
      /Users/isaac/Downloads/TestApp/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php:86

  Please use the argument -v to see more details.

   Whoops\Exception\ErrorException  : PHP Startup: Unable to load dynamic library 'intl.so' (tried: /Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so (dlopen(/Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so, 0x0009): dlopen(): file not found: /Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so), /Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so.so (dlopen(/Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so.so, 0x0009): dlopen(): file not found: /Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so.so))

  at Unknown:0
    1| 

  Exception trace:

  1   Whoops\Run::handleError("PHP Startup: Unable to load dynamic library 'intl.so' (tried: /Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so (dlopen(/Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so, 0x0009): dlopen(): file not found: /Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so), /Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so.so (dlopen(/Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so.so, 0x0009): dlopen(): file not found: /Applications/MAMP/bin/php/php7.3.9/lib/php/extensions/no-debug-non-zts-20180731/intl.so.so))", "Unknown")
      /Users/isaac/Downloads/TestApp/vendor/filp/whoops/src/Whoops/Run.php:433

  2   Whoops\Run::handleShutdown()
      [internal]:0

我尝试了使用 php artisan config:cache,但得到了这个

Configuration cache cleared!

   Symfony\Component\Debug\Exception\FatalErrorException  : Cannot redeclare returnSQLdatabase() (previously declared in /Users/isaac/Downloads/TestApp/config/database.php:5)

  at /Users/isaac/Downloads/TestApp/config/database.php:22
    18|         'prefix_indexes' => true,
    19|         'strict' => true,
    20|         'engine' => null
    21|     ];
  > 22| }
    23| 
    24| return [
    25| 
    26|     /*


   Whoops\Exception\ErrorException  : Cannot redeclare returnSQLdatabase() (previously declared in /Users/isaac/Downloads/TestApp/config/database.php:5)

  at /Users/isaac/Downloads/TestApp/config/database.php:22
    18|         'prefix_indexes' => true,
    19|         'strict' => true,
    20|         'engine' => null
    21|     ];
  > 22| }
    23| 
    24| return [
    25| 
    26|     /*

  Exception trace:

  1   Whoops\Run::handleError("Cannot redeclare returnSQLdatabase() (previously declared in /Users/isaac/Downloads/TestApp/config/database.php:5)", "/Users/isaac/Downloads/TestApp/config/database.php")
      /Users/isaac/Downloads/TestApp/vendor/filp/whoops/src/Whoops/Run.php:433

  2   Whoops\Run::handleShutdown()
      [internal]:0

我也认为错误也可能与我的 php.ini 文件有关。所以在 this 之后,我尝试转到 /etc/php.ini,但没有找到类似于 extension=php_intl.dll.

的评论扩展

所以我刚刚将这行添加到文件中。 extension=intl.so 并且错误仍然存​​在。我可以知道代码还有什么问题吗?

这是我的config/database.php文件供参考

<?php

use Illuminate\Support\Str;

function returnSQLdatabase($var) {
    return [
        'driver' => 'mysql',
        'url' => env('DATABASE_URL'),
        'host' => env('DB_HOST', '127.0.0.1'),
        'port' => env('DB_PORT', '3306'),
        'database' => $var,
        'username' => env('DB_USERNAME', 'forge'),
        'password' => env('DB_PASSWORD', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset' => 'utf8mb4',
        'collation' => 'utf8mb4_unicode_ci',
        'prefix' => '',
        'prefix_indexes' => true,
        'strict' => true,
        'engine' => null
    ];
}

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', 'test1'),

    /*
    |--------------------------------------------------------------------------
    | 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' => [
        'test1' => returnSQLdatabase('test1')
    ],

    /*
    |--------------------------------------------------------------------------
    | 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 body of commands than a typical key-value system
    | such as APC or Memcached. Laravel makes it easy to dig right in.
    |
    */

    'redis' => [

        'client' => env('REDIS_CLIENT', 'phpredis'),

        'options' => [
            'cluster' => env('REDIS_CLUSTER', 'redis'),
            'prefix' => env('REDIS_PREFIX', Str::slug(env('APP_NAME', 'laravel'), '_').'_database_'),
        ],

        'default' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_DB', 0),
        ],

        'cache' => [
            'url' => env('REDIS_URL'),
            'host' => env('REDIS_HOST', '127.0.0.1'),
            'password' => env('REDIS_PASSWORD', null),
            'port' => env('REDIS_PORT', 6379),
            'database' => env('REDIS_CACHE_DB', 1),
        ],

    ],

];

您已在 config/database.php 中重新声明函数 returnSQLdatabase()。删除该功能并重试。

你为什么要创建这个函数?这里看原代码——https://github.com/laravel/laravel/blob/master/config/database.php