Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kossiecoder's avatar

Laravel Sail testing database

curl -s "https://laravel.build/example-app" | bash

I used above command to install laravel8 and sail up

It all works good.

However, I would like to have testing mysql as well.

mysql_test:
  image: "mysql:8.0"
  environment:
    MYSQL_ROOT_PASSWORD: "${DB_PASSWORD}"
    MYSQL_DATABASE: "${DB_DATABASE}"
    MYSQL_USER: "${DB_USERNAME}"
    MYSQL_PASSWORD: "${DB_PASSWORD}"
    MYSQL_ALLOW_EMPTY_PASSWORD: "yes"
  networks:
    - sail

I added above to docker-composer.yml and ran sail up

DB_CONNECTION=mysql
DB_HOST=mysql_test
DB_PORT=3306
DB_DATABASE=laravel8_test
DB_USERNAME=sail
DB_PASSWORD=password

.env.testing

but when I ran sail test, I get an error

SQLSTATE[HY000] [1044] Access denied for user 'sail'@'%' to database 'laravel8_test' (SQL: select * from `users`)

and the test I ran is

public function test_example()
    {
        dd(User::all());
        $response = $this->get('/');

        $response->assertStatus(200);
    }

What else should I do?

0 likes
3 replies
siangboon's avatar

perhaps, double check that your SQL instance in your machine is not running or not conflict with the port in using by the SQL instance in Sail.

mikeferry's avatar

I ran into the same problem and what fixed it for me was setting the DB_* variables in .env.testing to match the ones in .env exactly with the only exception being DB_HOST=mysql_test. I assume that in your case having _test suffixed to the name of your database is why it isn't working.

This is what I have in .env:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=sail
DB_PASSWORD=password

This is what I have in .env.testing:

DB_CONNECTION=mysql
DB_HOST=mysql_test
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=sail
DB_PASSWORD=password

Here are the mysql and mysql_test services from my docker-compose.yml for reference:

mysql:
    image: 'mysql:8.0'
    ports:
        - '${FORWARD_DB_PORT:-3306}:3306'
    environment:
        MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
        MYSQL_DATABASE: '${DB_DATABASE}'
        MYSQL_USER: '${DB_USERNAME}'
        MYSQL_PASSWORD: '${DB_PASSWORD}'
        MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    volumes:
        - 'sailmysql:/var/lib/mysql'
    networks:
        - sail
    healthcheck:
      test: ["CMD", "mysqladmin", "ping"]
mysql_test:
    image: 'mysql:8.0'
    environment:
        MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
        MYSQL_DATABASE: '${DB_DATABASE}'
        MYSQL_USER: '${DB_USERNAME}'
        MYSQL_PASSWORD: '${DB_PASSWORD}'
        MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
    networks:
        - sail
5 likes
JeffJones's avatar

I'm using Laravel 11 and had the same problem. I couldn't find any .env.testing file so I figured the testing setup must be different.

I had read somewhere that docker (or sail?) assumes the test website is named 'testing'. And I saw this database when logged in using Table Plus.

So, instead of using .env.testing and adding a 'mysql_test' section to docker-compose.yml, I updated the phpunit.xml file.

<phpunit ...>
    ...
    <php>
        ...
        <env name="DB_DATABASE" value="testing"/>
        ...
    </php>
</phpunit>

This worked for me.

Please or to participate in this conversation.