can't use migrate with sail laravel

18.7k Views Asked by At

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?

5

There are 5 best solutions below

6
On BEST ANSWER

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
...
0
On

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

1
On

This worked for me. Hoped it helps.

DB_HOST=0.0.0.0
0
On

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.

1
On

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