23

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,
    ],
4
  • 3
    do a composer dump autoload and artisan config:clear Commented Sep 29, 2016 at 16:33
  • 'prefix' => 'test_' along with table named lang? This can cause conflict for eloquent builder I guess Commented Sep 29, 2016 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! Commented Feb 2, 2019 at 1: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. Commented Nov 22, 2019 at 5:11

9 Answers 9

32

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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!
9

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 

Comments

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.

1 Comment

Or you could just call the correct DB connection (called "mysql") using $langs = DB::connection('mysql')->select('select * from lang');
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

1 Comment

This configuration is giving error ... what could be be the possible reason
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.

Comments

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.

Comments

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

Comments

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

Comments

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.

1 Comment

Thank you for your answer, but can you explain how it is related to the problem in the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.