29

I have created a new laravel project in laravel 8. I have set up Sail installation correctly, everything working fine for database connection in local 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.

Rutul Thakkar
  • 291
  • 1
  • 3
  • 6

7 Answers7

92

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

willpercey-gb
  • 1,028
  • 6
  • 9
  • 6
    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? – Bill Jan 10 '21 at 21:45
  • 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? – Chris Feb 09 '21 at 12:54
  • Good answer, that was exactly my case. – Hasnat Safder Feb 23 '21 at 09:55
  • how do i have to write that command? because $ sail:install didnt work – Ivandez Mar 26 '21 at 19:16
  • 5
    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. – Alexander Taubenkorb Jul 20 '21 at 13:06
  • This did it for me. Thank you!!!!! I was about to give up all hope. – travisneids Aug 01 '21 at 16:08
  • @AlexanderTaubenkorb This was the only solution given here that worked for me. – maesk Sep 19 '21 at 21:54
  • 2
    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 . – Wow Oct 21 '21 at 15:47
  • This is interesting. In my case, GET methods are working fine, but in POST I encountered the same error. The change in DB_HOST also fixed mine. Is this a bug ? – Jur P May 11 '22 at 15:13
  • I was stuck in this issue for hours with multiple re-installations! Thanks for your suggestion. finally, the issue was solved. – Qin Wang Jul 18 '23 at 15:35
23

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

ismayil-dev
  • 331
  • 2
  • 3
13

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
milad nazari
  • 339
  • 3
  • 5
4

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
aifodu
  • 254
  • 3
  • 2
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.

Marcus
  • 51
  • 7
0

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.

eOssa
  • 44
  • 5
-2

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.

Raphael Amponsah
  • 549
  • 6
  • 13