Lilian
Lilian

Reputation: 109

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

I'm using Laravel, I'm working in a Login System, but in register page, when confirm button is clicked, I get the message: "Database [mydatabase] not configured."

My database.php file:

'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'),
    ]) : [],
],

My .env file:

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

My system and database are in a debian 9 server, and I programming in VS Code in my Windows Client with a extension remote for VSCode called SFTP. My database don't have password for now.

it may be an external access error to mysql, my version of MySQL is 10.4.11 and I am using it on the Debian 9 server with the latest current XAMPP version, and I don't know how to release external access to mysql in XAMPP, because in the my.cnf file there is no bind_address field.

This is just a hypothesis, I came here because I tried everything on Google and nothing works.

Thanks for listening! =D


Edit:

When I run the command "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.

My code (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']),
        ]);
    }
}

My database on mysql (mysql -uroot; show databases;):

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

Edit (My full database.php file):

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

    ],

];

Upvotes: 1

Views: 10251

Answers (2)

Sudesh Ryan
Sudesh Ryan

Reputation: 13

In my case, I could not even run php artisan config:clear, without getting error.

Deleting bootstrap\cache\config.php file solved the issue

Upvotes: 0

LobsterBaz
LobsterBaz

Reputation: 2003

Did you create the database and run Laravel's migrations?

To check if your database exist on your server, login to MySQL on your server and list all databases:

mysql -u yourusername -p

show databases;

Your Laravel database "mydatabasename" should be listed here. If not, you'll have to create it on your server. Since it's not specific to Laravel, I will let you search how to do that.

Once your database exists, you will have to run Laravel's migrations command to create the database tables relevant to your Laravel application:

php artisan migrate

Upvotes: 1

Related Questions