SQLSTATE[HY000] [2002] Connection refused in Docker Compose when starting my container with Laravel and AWS and running commands - Hack The Tech - Latest News related to Computer and Technology

logo

Get Daily Latest News related to Computer and Technology and hack the world.

Monday, May 22, 2023

demo-image

SQLSTATE[HY000] [2002] Connection refused in Docker Compose when starting my container with Laravel and AWS and running commands

SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from information_schema.tables where table_schema = wallamotop and table_name = migrations and table_type = 'BASE TABLE')

Quiero desplegar una aplicación laravel en AWS usando docker, levanto los 3 contenedores con: docker-compose up -d y los contenedores "mariadb" y "phpmyadmin" se levantan correctamente pero el contenedor de "myapp" se cierra. En el docker-compose.yml en el contenedor "myapp" tengo una seccion "command" donde quiero ejecutar los comandos: "php artisan migrate php artisan db:seed" Este error ocurre si en mi docker-compose.yml tengo puesta la sección "command" si la elimino, se me levantan los 3 contenedores y no se cierra ninguno. Al hacer docker-compose logs -f veo que me sale este error

SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from information_schema.tables where table_schema = wallamotop and table_name = migrations and table_type = 'BASE TABLE')

Este error me aparece unas veces si y unas veces no. Si salgo de los logs y vuelvo a ejecutar "docker-compose up -d" el contenedor de "myapp" vuelve a cerrarse y en los logs aparece el mismo error, pero realiza los comandos: "php artisan migrate php artisan db:seed"

Me gustaría solucionar el error de la conexión rechazada de la base de datos, y ser capaz de poder ejecutar los comandos "php artisan migrate php artisan db:seed" para migrar las tablas a la base de datos. Ayuda por favor.

I want to deploy a laravel application on AWS using docker, I raise the 3 containers with: docker-compose up -d and "mariadb" and "phpmyadmin" containers are raised correctly but the "myapp" container is closed. In the docker-compose.yml in the "myapp" container I have a "command" section where I want to execute the commands: "php artisan migrate php artisan db:seed" This error occurs if in my docker-compose.yml I have the "command" section set, if I remove it, all 3 containers are raised and none are closed. When doing docker-compose logs -f I see that I get this error

SQLSTATE[HY000] [2002] Connection refused (Connection: mysql, SQL: select * from information_schema.tables where table_schema = wallamotop and table_name = migrations and table_type = 'BASE TABLE')

This error appears sometimes yes and sometimes no. If I exit the logs and run "docker-compose up -d" again the "myapp" container closes again and the logs show the same error, but perform the commands: "php artisan migrate php artisan db:seed"

I would like to fix the database connection refused error, and be able to run the "php artisan migrate php artisan db:seed" commands to migrate the tables into the database. Please help.

Adjunto algunas imágenes, explicando con mas detalle myapp closes logs I exit the logs and do docker-compose up -d again, still the error but execute the commands I remove the command section and myapp does not close I add the command section again, it does not give me a connection refused error, it executes the commands but myapp closes, this happens very rarely archive config/database.php

.env archive:

`APP_NAME=Laravel
APP_ENV=local
APP_KEY=base64:sKPVZblHmpBF0G5Jgax5aucPYYfm+GPPGZn6DgePymw=
APP_DEBUG=true
APP_URL=http://proyecto.test

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=mariadb
DB_PORT=3306
DB_DATABASE=wallamotop
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DISK=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
.....`

docker-compose.yml archive:

version: '2'

services:
  mariadb:
    image: docker.io/bitnami/mariadb:10.6
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
      - MARIADB_USER=bn_myapp
      - MARIADB_DATABASE=wallamotop 

  myapp:
    image: docker.io/bitnami/laravel:10
    ports:
      - '8000:8000'
    environment:
      - DB_HOST=mariadb
      - DB_PORT=3306
      - DB_USERNAME=root
      - DB_DATABASE=wallamotop
    volumes:
      - './:/app'
    depends_on:
      - mariadb

    command: sh -c "php artisan config:clear &&  php artisan migrate && php artisan db:seed"

#    command: bash -c "php artisan migrate && php artisan db:seed"


  phpmyadmin:
    image: phpmyadmin/phpmyadmin
    links:
      - mariadb:db
    ports:
      - '8080:80' 
    environment:
      - PMA_HOST=mariadb
      - MYSQL_USER=root
      - MYSQL_PASSWORD=
      - MYSQL_ROOT_PASSWORD=


source https://stackoverflow.com/questions/76301313/sqlstatehy000-2002-connection-refused-in-docker-compose-when-starting-my-con

No comments:

Post a Comment