title: SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `posts`) with sail docker container
body: <p>I am pretty new to Laravel but have worked with several frameworks in the past. I am using a Linux development host and have created a docker container as per documentation using:</p>
  <pre><code>curl -s https://laravel.build/example-app | bash
  </code></pre>
  <p>After that I have ramped it up using:</p>
  <pre><code>cd example-app
  ./vendor/bin/sail up
  </code></pre>
  <p>I have used migrations to create a post table, which worked fine, and used tinker to add some sample entries. Also, that worked without any issues. But when I am trying to read all table entries in a Controller using the model, I get a MySQL connection refused error.</p>
  <blockquote>
  <pre><code>Illuminate\Database\QueryException
  SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `posts`)
  </code></pre>
  </blockquote>
  <p>After quite a bit of research, I have not been able to solve it. I have tried various solution proposed in different other threads, like:</p>
  <pre><code>php artisan config:cache
  php artisan config:clear
  php artisan cache:clear
  </code></pre>
  <p>no success. I also changed IP address 127.0.0.1 to localhost in .env or towards the docker service mysql as described. All of that with NO success so far.</p>
  <h2>Summary of what is working:</h2>
  <ul>
  <li>php artisan migrate works fine</li>
  <li>php artisan tinker works fine</li>
  <li>phpMyAdmin or other db tools can connect without issues to the DB</li>
  <li>ramping up a development server with <code>php artisan serve</code> does work and my controller returns table entries as JSON (as expected)</li>
  </ul>
  <h2>What does NOT work:</h2>
  <ul>
  <li>PostControler and Post::all() results in connection refused when running in docker environment</li>
  </ul>
  <p>Has anyone had the same issue as this. It seems everything is setup correctly as everything works on the console and also when firing up a development server. However when running the normal app at http://locahost/posts (for the post controller index) it returns a connection refused.</p>
  <h2>Environment information</h2>
  <p>Laravel version: 8.40.0<br />
  Laravel locale:en<br />
  Laravel config cached: false<br />
  PHP version: 8.0.3</p>
  <h2>config entries</h2>
  <p>.env</p>
  <pre><code>DB_CONNECTION=mysql
  DB_HOST=127.0.0.1
  DB_PORT=3306
  DB_DATABASE=dbname
  DB_USERNAME=user
  DB_PASSWORD=password
  </code></pre>
  <p>docker-compose.yml</p>
  <pre><code># For more information: https://laravel.com/docs/sail
  version: '3'
  services:
      laravel.test:
          build:
              context: ./vendor/laravel/sail/runtimes/8.0
              dockerfile: Dockerfile
              args:
                  WWWGROUP: '${WWWGROUP}'
          image: sail-8.0/app
          ports:
              - '${APP_PORT:-80}:80'
          environment:
              WWWUSER: '${WWWUSER}'
              LARAVEL_SAIL: 1
          volumes:
              - '.:/var/www/html'
          networks:
              - sail
          depends_on:
              - mysql
              - redis
              - selenium
      mysql:
          image: 'mysql:8.0'
          ports:
              - '${FORWARD_DB_PORT:-3306}: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: [&quot;CMD&quot;, &quot;mysqladmin&quot;, &quot;ping&quot;]
      redis:
          image: 'redis:alpine'
          ports:
              - '${FORWARD_REDIS_PORT:-6379}:6379'
          volumes:
              - 'sailredis:/data'
          networks:
              - sail
          healthcheck:
            test: [&quot;CMD&quot;, &quot;redis-cli&quot;, &quot;ping&quot;]
      meilisearch:
          image: 'getmeili/meilisearch:latest'
          ports:
              - '${FORWARD_MEILISEARCH_PORT:-7700}:7700'
          volumes:
              - 'sailmeilisearch:/data.ms'
          networks:
              - sail
      mailhog:
          image: 'mailhog/mailhog:latest'
          ports:
              - '${FORWARD_MAILHOG_PORT:-1025}:1025'
              - '${FORWARD_MAILHOG_DASHBOARD_PORT:-8025}:8025'
          networks:
              - sail
      selenium:
         image: 'selenium/standalone-chrome'
         volumes:
              - '/dev/shm:/dev/shm'
         networks:
             - sail
      phpMyAdmin:
          image: 'phpmyadmin:latest'
          ports:
              - 8080:80
          environment:
              MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
          links:
              - 'mysql:db'
          depends_on:
              - mysql
          networks:
              - sail
  networks:
      sail:
          driver: bridge
  volumes:
      sailmysql:
          driver: local
      sailredis:
          driver: local
      sailmeilisearch:
          driver: local
  </code></pre>
  <p>Post Controller</p>
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Models\Post;
  
  class PostsController extends Controller
  {
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index()
      {
          return Post::all();
          //return view('posts.index');
  
      }
  ...
  </code></pre>