tills98's avatar

Laravel 7 Default database connection invalid

At the beginning of developing i've problems with selecting rows in database tables that are not found by larave with

DB::query('select * from mytable');

or

DB::table('mytable')->select('*')->get();

I have found out through unit tests that laravel uses sqlite as default database connection instead of mysql. My settings in .env file are not imported correctly into conf/database.php. My .env contents:

...
DB_CONNECTION=mysql
DB_HOST=172.22.0.4
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=default
DB_PASSWORD=secret
...

The connection to my mysql database works fine when i test it with MySQL Workbench or other programs.

Here is my unit test:

public function testDefaultConnectionSettings()
    {
        $this->assertEquals('mysql', DB::getDefaultConnection());
    }

Is this an known issue in laravel 7? I'm running laravel 7 in laradock on MacOS Catalina.

0 likes
1 reply
Sti3bas's avatar
Sti3bas
Best Answer
Level 53

I have found out through unit tests that laravel uses sqlite as default database connection instead of mysql. Is this an known issue in laravel 7? I'm running laravel 7 in laradock on MacOS Catalina.

It's not an issue. It uses SQLite in-memory database for tests by default because it runs faster (most of the times) and doesn't require any additional setup (like creating a database). If you want to use MySQL for your tests, you should update DB_CONNECTION and DB_DATABASE env settings in phpunit.xml file.

Please or to participate in this conversation.