数据库 [] 未配置 Laravel 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39774946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:32:06  来源:igfitidea点击:

Database [] not configured Laravel 5

laravellaravel-5laravel-5.1

提问by Viktor

I create new db in phpmyadmin and new tables.

我在 phpmyadmin 和新表中创建新数据库。

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

在我的 .env 中

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

in my config/database.php

在我的 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,
    ],

采纳答案by Alexey Mezenin

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.

connection()只有在处理多个数据库连接时才应该使用方法。

回答by Arthur Kushman

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.

在我的情况下,我使用了 2 个 db 连接,第二个添加的连接没有缓存,命令调用: php artisan config:cache 成功了。

To see what's going on it was sufficient to output the $connectionsvariable in DatabaseManager->configuremethod.

要查看发生了什么,$connectionsDatabaseManager->configure方法中输出变量就足够了。

回答by jsonUK

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

任何人在这方面绊倒 - 如果您使用 Laravel 5.4 或更新版本,您可以在 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 envvariables DB_USERNAME_TEST, DB_PASSWORD_TEST, DB_DATABASE_TEST

我创建了3级新env的变量DB_USERNAME_TESTDB_PASSWORD_TESTDB_DATABASE_TEST

Edit .envwith something like

.env用类似的东西编辑

DB_DATABASE_TEST=test_db
DB_USERNAME_TEST=local
DB_PASSWORD_TEST=localpassword

Make sure config is refreshed: php artisan config:cache

确保配置已刷新: 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

php artisan migrate:refresh --database=mysql_test

回答by Andrew Fox

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.

就我而言,这是一个错误的数据库用户名。我在我的 config/database.php 以及 .env 文件中设置了它,其中一个与另一个不同。搜索时发现此线程,认为这可能对某人有所帮助。

回答by Samer Abu Gahgah

For me the connection worked in development but not in the production environment, so after a lot of searching, I had to rebuild the configuration cache and It has worked. I ran the following command in the laravel root directory:

对我来说,连接在开发中有效,但在生产环境中无效,所以经过大量搜索,我不得不重建配置缓存并且它已经起作用了。我在 Laravel 根目录中运行以下命令:

php artisan config:cache 

回答by N. Djokic

I struggled with this error for a few hours and found out solution that works for me. If you can not delete cache with php artisan config:cachebecause it throws error after you run it:

我与这个错误斗争了几个小时,并找到了适合我的解决方案。如果您无法删除缓存,php artisan config:cache因为它在运行后抛出错误:

[InvalidArgumentException]

Database [] not configured

[无效参数异常]

数据库 [] 未配置

And if you are sure that your connections parameters are good then try manually delete bootstrap cache config files which are placed in app/bootstrap/cache folder. Hope it helps someone.

如果您确定您的连接参数良好,请尝试手动删除放置在 app/bootstrap/cache 文件夹中的引导缓存配置文件。希望它可以帮助某人。