-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 15:13:21 25 4
gpt4 key购买 nike

我正在使用 Laravel,我正在登录系统中工作,但在注册页面中,单击确认按钮时,我收到消息:“数据库 [mydatabase] 未配置。”

我的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 进行编程,并使用名为 SFTP 的 VSCode 远程扩展。我的数据库暂时没有密码。

可能是mysql的外部访问错误,我的MySQL版本是10.4.11,我在Debian 9服务器上使用它,当前最新的XAMPP版本,我不知道如何释放对mysql的外部访问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 你的用户名-p

显示数据库;

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

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

php artisan 迁移

关于mysql - 错误 "Database [mydatabase] not configured."- Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59886964/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com