Database [] not configured Laravel 5
Solution 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.
Solution 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.
Solution 3
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
Solution 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
Solution 5
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.
Viktor
PHP 6+ years, Golang 1+ year, JavaScript 3+ years (native, vue, react). Highly familiar with a big number of web programming tools. Diploma in Computer Science
Updated on July 05, 2022Comments
-
Viktor almost 2 years
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, ],
-
Admin over 7 yearsOr you could just call the correct DB connection (called "mysql") using
$langs = DB::connection('mysql')->select('select * from lang');
-
Veshraj Joshi about 4 yearsThis configuration is giving error ... what could be be the possible reason
-
Luiza Rodrigues over 3 yearsIn 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!