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:
curl -s https://laravel.build/example-app | bash
After that I have ramped it up using:
cd example-app
./vendor/bin/sail up
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.
Illuminate\Database\QueryException SQLSTATE[HY000] [2002] Connection refused (SQL: select * from `posts`)
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:
php artisan config:cache
php artisan config:clear
php artisan cache:clear
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.
php artisan serve does work and my controller returns table entries as JSON (as expected)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.
Laravel version: 8.40.0
Laravel locale:en
Laravel config cached: false
PHP version: 8.0.3
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dbname
DB_USERNAME=user
DB_PASSWORD=password
docker-compose.yml
# 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: ["CMD", "mysqladmin", "ping"]
redis:
image: 'redis:alpine'
ports:
- '${FORWARD_REDIS_PORT:-6379}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
healthcheck:
test: ["CMD", "redis-cli", "ping"]
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
Post Controller
<?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');
}
...
I have found a solution to the issue.
Changing to localhost did end trying to use a socket, which apparently did not work. I even added the docker mysql socket to the php.ini as default_socket, but it did not resolve anything.
I have changed localhost/127.0.0.1 to the real IP address of my machine and now everything works as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With