can't use migrate with sail laravel
Asked Answered
W

5

15

Hi I've just tried and to install Laravel 8 with sail and I've run into a problem

What I've done is to first run the curl one-liner from laravel's own page

curl -s https://laravel.build/sail-test | bash

once that finished I did as it said and run

cd sail-test && ./vendor/bin/sail up

Everything started as it should, I can connect to the database, I can see the site on http://localhost however I get the following error when trying to migrate:

$ sail artisan migrate:install

   Illuminate\Database\QueryException 

  SQLSTATE[HY000] [2002] No route to host (SQL: create table `migrations` (`id` int unsigned not null auto_increment primary key, `migration` varchar(255) not null, `batch` int not null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at vendor/laravel/framework/src/Illuminate/Database/Connection.php:678
    674▕         // If an exception occurs when attempting to run a query, we'll format the error
    675▕         // message to include the bindings with SQL, which will make this exception a
    676▕         // lot more helpful to the developer instead of just the database's errors.
    677▕         catch (Exception $e) {
  ➜ 678▕             throw new QueryException(
    679▕                 $query, $this->prepareBindings($bindings), $e
    680▕             );
    681▕         }
    682▕ 

      +29 vendor frames 
  30  artisan:37
      Illuminate\Foundation\Console\Kernel::handle()

Do i need to change anything in the .env file or do I need to do any other changes first?

Watersick answered 12/12, 2020 at 17:9 Comment(1)
Do you have a password set in your .env file? If so you probably need to set MYSQL_ALLOW_EMPTY_PASSWORD to 'no' in your docker-compose.yml file.Caspar
N
21

First (this command will return your mysql container IP address):

$ docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <container_name_or_container_ID>

Second in your .env file:

DB_HOST=<ip_address_returned_from_command_above>

And then:

$ sail artisan migrate

Edit:

Just add to your DB_HOST env var the name of your database container:

...
DB_HOST=mysql
...
Neediness answered 13/12, 2020 at 16:43 Comment(6)
I'm getting the same response after trying thisWatersick
Worked for me except only with root username and blank passwordEdveh
You can also use the name of the docker image (found in docker-compose.yml). By default that is DB_HOST=mysqlMccollough
Worked for me after replacing DB_HOST=127.0.0.1 with DB_HOST=mysql (the name of the docker image)Stereotaxis
container_name_or_container_ID =?? how can get container_name_or_container_ID ?Dodie
DB_HOST=mysql this work for meAdriannaadrianne
L
5

You need to artisan command by login in sail. Here are the steps.

  1. Go to your project directory and run the following command

    sail shell

  2. After that command you will be logged in your container and you can run the artisan migrate command.

Lucite answered 6/1, 2021 at 14:42 Comment(0)
L
0

change the port of XAMP MySQL from 3306 to any other port. Sail migration works when the local port should be 3306.

Liquescent answered 30/12, 2022 at 13:0 Comment(0)
M
0

I had this problem in 2024 with WSl2 and Ubuntu 22.04.

I solved it, either with the method in @Gabriel Fonseca's answer,

or this:

DB_CONNECTION=mysql
# Docker MYSQL NAME
DB_HOST=Docker mysql name --> see on docker container and see what is the name for mysql container
DB_PORT=3306
DB_DATABASE=projectname
DB_USERNAME=sail
DB_PASSWORD=password
FORWARD_DB_PORT=3306 --> add this too, I found this at laracast discussion

after that I used this in bash/cmd terminal:

php artisan config:clear
php artisan migrate
Metaplasia answered 16/1, 2024 at 23:31 Comment(1)
FORWARD_DB_PORT=3306 is not necessary, because it's the default.Mercedezmerceer
N
-2

This worked for me. Hoped it helps.

DB_HOST=0.0.0.0
Noneffective answered 11/8, 2022 at 3:5 Comment(1)
Thank you for your answer, however it it very similar to the other answer and doesn't explain much, not even that your code that not formatted as such should go to the .env file. Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Fieldsman

© 2022 - 2025 — McMap. All rights reserved.