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

automica's avatar

Access denied for user 'sail'@'%' to database

I've just spun up a new laravel 8 instance and have decided on sail/ docker as the local development environment.

Whilst I have got the site up, when I try to run migrations I get the following error:

SQLSTATE[HY000] [1044] Access denied for user 'sail'@'%' to database 'my_database' (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')

I have the following in my .env

DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE= my_database
DB_USERNAME=sail
DB_PASSWORD=password

Any suggestions what the default is?

BTW I'm on m1 Mac so have switched to mariadb.

    mariadb:
        image: 'mariadb'
        ports:
            - '${FORWARD_DB_PORT:-3308}: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", "-p${DB_PASSWORD}"]
          retries: 3
          timeout: 5s

any suggestions on what I can do to resolve this?

0 likes
24 replies
Sergiu17's avatar

try to change DB_HOST to host.docker.internal

DB_HOST=host.docker.internal
3 likes
automica's avatar

changing host, just changes the message to include an IP

Access denied for user 'sail'@'172.27.0.1'

Sinnbeck's avatar

Did you change any of the env values after starting sail?

automica's avatar

I've destroyed the images via docker desktop but haven't changed anything since.

Sinnbeck's avatar

Did you also remove the volumes? Mysql data is stored in volumes not images

1 like
automica's avatar

deleting the volumes using ./vendor/bin/sail down -v is the same as deleting via docker-desktop.

Either way I'm not able to connect using via artisan.

if I access

./vendor/bin/sail mariadb then I can connect to the db server and can see a table has been created.

bugsysha's avatar

@automica I assume you have, but just to make sure, have you noticed the space in the .env file before the name of the database?

DB_DATABASE= my_database

Not saying this is going to fix the problem. Just that I find it annoying :D

automica's avatar

@bugsysha that space is due to me editing the .env example in my thread so I didn't reveal my clients name , rather than my 'real' env having a space.

automica's avatar

I'm wondering if this issue was specific to incompatibilities with sail and the M1 Mac architecture.

I have docker running on other laravel projects but dont use sail for these. My hunch is that issue with sail rather than docker, as if I run

./vendor/bin/sail php artisan test

I get no response, but doing:

docker exec my_application.test_1 php artisan test

Runs the tests.

I have managed to get my migrations running now, my some slightly unknown means, but I did add:

        image: 'mysql:8.0'
        platform: 'linux/x86_64'

which (I think) allowed me to use mysql instead of mariadb.

I'll try this properly on a new project and see if I can establish consistent behaviour and report back when I find out more.

1 like
braed's avatar

@automica I have this issue on an Intel Macbook so it may not be related to the M1 architecture.

alabamustapha's avatar

@enadabuzaid

I fixed this by running:

sail down -v

This will help you to recreate a fresh database for the next sail up.

Works for me, I am using Ubuntu 22, WSL and Laravel 9

23 likes
technodwarf's avatar

@alabamustapha thanks, it worked! It seems that docker desktop doesn't delete volumes completely or smth like that, but the CLI does!

dev-leper's avatar

I came here after having the same issue.

TL;DR For a brand new container (eg after a first clone) make sure the .env is set before sail up! Otherwise it's created without the login info you think you have set in .env. If you screwed up like me, stop the container, delete the container and volumes but you can keep the images, then after setting .env, sail up again and the container will be built with your .env config.

It was on a new machine and so an initial clone and docker/sail build so I was a bit rusty and made a rookie mistake...

The .env usually isn't in the repo and so this needs setting before you sail up. Because I didn't do this first, it wasn't connecting to mysql as we built that without the login info. This means no matter how many times you change .env thinking you must have done something wrong, it will never work.

You need to tear it down and rebuild it. Stop and deleted the container and volumes, leaving the images for a quicker re-build. This should now work providing your mysql login details are correct in .env file. For sail the username is sail and the password is password.

To do the above there is a command but docker desktop makes this very easy to do as well.

6 likes
baumgars's avatar

@dev-leper helped me too. But is there nothing like a hot reaload feature? I mean if i develop a UI with let´s say Vue and change something in the codebase, save it, then the server auto recompiles it and i see the result in the browser. But with such config things...i am still new to docker and these things can take you tons of time to figure out.

iambateman's avatar

switching my username from sail to root seems to have worked.

codeOnJim's avatar

@iambateman I tried this did not work for me. sail down -v did though. Curious what really causes this.

viktorminator's avatar

For me solution was to check if database exists (it was not), create DB and grant priviledges.

mysql -u root -p
show databases;
CREATE DATABASE pronto_fuel;
GRANT ALL PRIVILEGES ON pronto_fuel.* TO 'sail'@'192.168.0.6' IDENTIFIED BY 'your_database_password';
FLUSH PRIVILEGES;
SHOW DATABASES;

Please or to participate in this conversation.