21

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,
    ],
Viktor
  • 1,532
  • 6
  • 22
  • 61
  • 3
    do a composer dump autoload and artisan config:clear – Sari Yono Sep 29 '16 at 16:33
  • `'prefix' => 'test_'` along with table named `lang`? This can cause conflict for eloquent builder I guess – Bartłomiej Sobieszek Sep 29 '16 at 20:56
  • 1
    Before you dig any deeper, make sure the file `config/database.php` is present... sounds stupid, but it happened to me when I cloned an external Laravel project where the author had not committed `config/database.php` for some reason! – Janaka Bandara Feb 02 '19 at 01:41
  • 1
    I had a similar problem with Database [mysql] not configured. If I ran php artisan cache:clear or config:cache I would get the same error. I solved it by going into laravel/bootstrap/cache and deleting the config.php file that was there, and then running the above commands again. I believe it was a file permission error that was preventing the cached config.php file from actually being cleared. – Scotch Design Nov 22 '19 at 05:11

9 Answers9

30

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.

Arthur Kushman
  • 3,449
  • 10
  • 49
  • 64
  • In my case, i was with one connection, and then switch to another DB. I dont know much of Laravel, i already got the code from another person. I was trying to log on the system and it presents me the error: "Malformed UTF-8 characters!" So i was trying to modified the charset of database!! Thanks to your answer, now it works! – Luiza Rodrigues Aug 01 '20 at 20:08
8

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.

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • Or you could just call the correct DB connection (called "mysql") using `$langs = DB::connection('mysql')->select('select * from lang');` – barryvanveen Sep 29 '16 at 17:48
8

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:

php artisan config:cache 
Samer Abu Gahgah
  • 751
  • 1
  • 9
  • 18
6

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

jsonUK
  • 345
  • 3
  • 9
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.

Andrew Fox
  • 794
  • 4
  • 13
  • 30
2

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:cache because it throws error after you run it:

[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.

N. Djokic
  • 898
  • 1
  • 6
  • 19
2

make sure the parameters

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=dbuser
DB_PASSWORD="password"

and then issue,

php artisan optimize:clear
Arun Panneerselvam
  • 2,263
  • 1
  • 17
  • 24
2

I had the same issue but when i added

'default' => env('DB_CONNECTION', 'mysql'),

to database.php.It seems to have solved the problem

0

if this error shows when you use the validation so it is simple :) just check all the validation rules and messages are written > correctly


for me i faced this error and i solve it by replace only point by comma in the validation rules.