Database [] not configured Laravel 5

烂漫一生 提交于 2019-12-23 07:37:01

问题


I create new db in phpmyadmin and new tables.

Then i do

    public function next(Request $request){
    $langs = DB::connection('mydb')->select('select * from lang');
    }

and get

Database [compgen] not configured.

in my .env

DB_HOST=localhost
DB_DATABASE=test
DB_USERNAME=root
DB_PASSWORD=123

in my config/database.php

        'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'test'),
        'username'  => env('DB_USERNAME', 'root'),
        'password'  => env('DB_PASSWORD', '123'),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => 'test_',
        'strict'    => false,
    ],

回答1:


You're using single connection, so you don't need to specify it:

$langs = DB::table('lang')->get();

You should use connection() method only when you're working with multiple DB connections.




回答2:


In my case I used 2 db connections and the second added was not cached, the command call: php artisan config:cache did the trick.

To see what's going on it was sufficient to output the $connections variable in DatabaseManager->configure method.




回答3:


In my case it was a bad database username. I had it set in my config/database.php as well as the .env file and one of them was different than the other. Found this thread when searching, thought this might help someone.




回答4:


Anyone stumbling upon this - if you are using Laravel 5.4 or newer, you can setup a new database connection in config/database.php

    'mysql_test' => [
        'driver'      => 'mysql',
        'host'        => env('DB_HOST', '127.0.0.1'),
        'port'        => env('DB_PORT', '3306'),
        'database'    => env('DB_DATABASE_TEST', 'forge'),
        'username'    => env('DB_USERNAME_TEST', 'forge'),
        'password'    => env('DB_PASSWORD_TEST', ''),
        'unix_socket' => env('DB_SOCKET', ''),
        'charset'     => 'utf8mb4',
        'collation'   => 'utf8mb4_unicode_ci',
        'prefix'      => '',
        'strict'      => false,
        'modes'       => [
            'ONLY_FULL_GROUP_BY',
            'STRICT_TRANS_TABLES',
            'NO_ZERO_IN_DATE',
            'NO_ZERO_DATE',
            'ERROR_FOR_DIVISION_BY_ZERO',
            'NO_AUTO_CREATE_USER',
            'NO_ENGINE_SUBSTITUTION',
        ],
        'engine' => null,
    ],

I created 3 new env variables DB_USERNAME_TEST, DB_PASSWORD_TEST, DB_DATABASE_TEST

Edit .env with something like

DB_DATABASE_TEST=test_db
DB_USERNAME_TEST=local
DB_PASSWORD_TEST=localpassword

Make sure config is refreshed: php artisan config:cache

You should be able to run a database migrate on your new test database, like so:

php artisan migrate:refresh --database=mysql_test



来源:https://stackoverflow.com/questions/39774946/database-not-configured-laravel-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!