错误 "Database [mydatabase] not configured." - Laravel

Error "Database [mydatabase] not configured." - Laravel

我正在使用 Laravel,我在登录系统中工作,但在注册页面中,当单击确认按钮时,我收到消息:"Database [mydatabase] not configured."

我的 database.php 文件:

'mysql' => [
    'driver' => 'mysql',
    'url' => env('DATABASE_URL'),
    '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' => '',
    'prefix_indexes' => true,
    'strict' => true,
    'engine' => null,
    'options' => extension_loaded('pdo_mysql') ? array_filter([
        PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
    ]) : [],
],

我的 .env 文件:

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=mydatabasename
DB_USERNAME=root
DB_PASSWORD=

我的系统和数据库在 debian 9 服务器中,我在我的 Windows 客户端中用 VS Code 编程,VSCode 的远程扩展名为 SFTP。我的数据库暂时没有密码。

可能是mysql的外部访问错误,我的MySQL版本是10.4.11,我在Debian 9服务器上使用它,最新的是XAMPP版本,而且我不知道如何在 XAMPP 中释放对 mysql 的外部访问,因为在 my.cnf 文件中没有 bind_address 字段。

这只是一个假设,我来这里是因为我在 Google 上尝试了所有方法,但没有任何效果。

感谢收听! =D


编辑:

当我运行命令"php artisan migrate":

root@LARAVEL:~/Projetos/SistemaX# php artisan migrate

Warning: PHP Startup: Unable to load dynamic library 'zip.so' (tried: /opt/lampp                                                                             /lib/php/extensions/no-debug-non-zts-20190902/zip.so (/opt/lampp/lib/php/extensi                                                                             ons/no-debug-non-zts-20190902/zip.so: wrong ELF class: ELFCLASS32), /opt/lampp/l                                                                             ib/php/extensions/no-debug-non-zts-20190902/zip.so.so (/opt/lampp/lib/php/extens                                                                             ions/no-debug-non-zts-20190902/zip.so.so: cannot open shared object file: No suc                                                                             h file or directory)) in Unknown on line 0

   InvalidArgumentException  : Database [mydatabase] not configured.

  at /root/Projetos/SistemaX/vendor/laravel/framework/src/Illuminate/D                                                                             atabase/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("mydatabase")
      /root/Projetos/SistemaX/vendor/laravel/framework/src/Illuminate/                                                                             Database/DatabaseManager.php:115

  2   Illuminate\Database\DatabaseManager::makeConnection("mydatabase")
      /root/Projetos/SistemaX/vendor/laravel/framework/src/Illuminate/                                                                             Database/DatabaseManager.php:86

  Please use the argument -v to see more details.

我的代码(RegisterController.php):

<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use App\User;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class RegisterController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Register Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles the registration of new users as well as their
    | validation and creation. By default this controller uses a trait to
    | provide this functionality without requiring any additional code.
    |
    */

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/painel';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    public function index() {
        return view('admin.register');
    }

    public function register(Request $request) {
        $data = $request->only([
            'name',
            'email',
            'password',
            'password_confirmation'
        ]);

        $validator = $this->validator($data);

        if ($validator->fails()) {
            return redirect()->route('register')
            ->withErrors($validator)
            ->withInput();
        }

        $user = $this->create($data);
        Auth::login($user);
        return redirect()->route('admin');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => ['required', 'string', 'max:100'],
            'email' => ['required', 'string', 'email', 'max:100', 'unique:users'],
            'password' => ['required', 'string', 'min:6', 'confirmed'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

我在 mysql 上的数据库(mysql -uroot;显示数据库;):

MariaDB [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| phpmyadmin         |
| mydatabase        |
+--------------------+

编辑(我的完整 database.php 文件):

<?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',
            'url' => env('DATABASE_URL'),
            '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' => '',
            'prefix_indexes' => true,
            'strict' => true,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

        'pgsql' => [
            'driver' => 'pgsql',
            'url' => env('DATABASE_URL'),
            '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' => '',
            'prefix_indexes' => true,
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

        'sqlsrv' => [
            'driver' => 'sqlsrv',
            'url' => env('DATABASE_URL'),
            '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' => '',
            'prefix_indexes' => true,
        ],

    ],

    /*
    |--------------------------------------------------------------------------
    | 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'),
        ],

    ],

];

您是否创建了数据库和 运行 Laravel 的迁移?

要检查您的数据库是否存在于您的服务器上,请在您的服务器上登录 MySQL 并列出所有数据库:

mysql -u yourusername -p

show databases;

您的 Laravel 数据库 "mydatabasename" 应该列在此处。如果没有,您必须在您的服务器上创建它。由于它不是特定于 Laravel,我会让你搜索如何做到这一点。

一旦您的数据库存在,您将必须 运行 Laravel's migrations command 创建与您的 Laravel 应用程序相关的数据库表:

php artisan migrate