36

I have created a new Laravel project in Laravel 8. I have set up Sail installation correctly, everything working fine for the database connection locally but when I try to connect my database using Sail it gives me an error

SQLSTATE[HY000] [2002] Connection refused (SQL: select * from information_schema.tables where table_schema = laravel8 and table_name = migrations and table_type = 'BASE TABLE')

I have already cleared my config and cache files.

7 Answers 7

112

I was researching the same problem and found this.

https://github.com/laravel/sail/blob/7457004969dd62fa727fbc596bb2accccb1409a5/src/SailServiceProvider.php#L31

The code shows an artisan command added by laravel sail - sail:install which overwrites your .env file host variables with service names.

I changed DB_HOST from 127.0.0.1 to mysql and it was fixed

Edit to answer questions:

Sail is based on Docker, to establish a connection between two Docker containers on the same Docker network, you can reference the container by its name, and Docker will handle the resolution for you. This is why we use mysql instead of the address

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

10 Comments

Hello, I am facing the same issue. DB_HOST is already mysql. I can log in to the DB and see it's empty. When I try to run "sail artisan migrate" I get the same error message. Any idea what it could be?
This works, but I'm very curious to know the significance of "mysql" - is it a special keyword used by Laravel when accessing dbhost, or is based on the docker image name of mysql.. EDIT: Actually, it might be invoking mysql directly from the command line?
Good answer, that was exactly my case.
Great that worked. I had to sail down -v do remove the stored mysql volume with the wrong config and sail up -d to rebuild. After that it worked.
I am using mariadb so i needed to add DB_HOST=mariadb in my .env file and container_name: mariadb in the docker-compose.yml .
|
30

On cmd just run this command

docker network inspect bridge

You will get response like (similar) this:

[
{
    "Name": "bridge",
    "Id": "695c284c7b184839edf97d48161922dfe6c4827d0db917c3278f5634af7e00e2",
    "Created": "2022-02-22T22:17:30.8583513Z",
    "Scope": "local",
    "Driver": "bridge",
    "EnableIPv6": false,
    "IPAM": {
        "Driver": "default",
        "Options": null,
        "Config": [
            {
                "Subnet": "172.17.0.0/16",
                "Gateway": "172.17.0.1"
            }
        ]
    },
    .....
}

]

Copy "Gateway" ip address and replace with DB_HOST value in .env file. It will work

3 Comments

Thanks. I'm getting another error, but seems to be because of my PHP version (stackoverflow.com/a/50027581/7242535)
Thanks a lot @softiso! Also could we know why the mysql value stops working?
Thank you very much. But my question now is why it worked and if it wouldn't work if we had gotten the ip of the database container
20

if you want run mysql in different port for example 3307 like below

ports:
  - 3307:3306

you should add FORWARD_DB_PORT environment variable to .env file like this

DB_PORT=3306 // this is for container port
FORWARD_DB_PORT=3307 // this is for local port

2 Comments

This is exactly what I needed as I have moved the docker mysql port. Thanks
I just needed to add the FORWARD_DB_PORT. It works for me. Thanks
7

Changing DB_USERNAME from sail to root worked for me. I guess sail did not have the required privileges.

DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root # changed from sail
DB_PASSWORD=password

Comments

1

Maybe you should match the environment variables of .env.testing, since, as willpercey-gb says, the sail:install command updates the environment variables of .env, but not of .env.testing.

Comments

1

I had this same issue after creating a second sail project in my docker. It wasn't (still isn't) accepting the sail username or password but I could go in with root which gave me the error you gave above. I went to the Desktop Docker app, clicked the running container, found the mysql portion and ran the CLI, typed mysql and then ran the query CREATE DATABASE yourdatabasename; migration ran perfectly fine.

Comments

-3

First off stop all services using brew especially MySQL

brew services stop MySQL

in your docker-yaml file:

laravel.test
   ports:
     - '70:80'
mysql:
   ports:
     - '3456:3306'

Now run your sail artisan migrate command.

this should solve your problem.

1 Comment

You don't need to stop the mysql service if you have changed the host port. In your case, the host port is, 3456

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.