skip to Main Content

I’m making some adjustments to a docker project with laravel but I’m having the following problem: SQLSTATE[HY000] [1049] Unknown database ‘ead’. Which is strange because I have the database created, every time I use the command: php artisan migrate I get this error. See my .env file:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=ead
DB_USERNAME=root
DB_PASSWORD=root

I looked for help in some answers here but nothing helped

2

Answers


  1. Chosen as BEST ANSWER

    I will add more information about the project. docker-compose.yml:

      mysql:
             image: mysql:8
             restart: unless-stopped
             environment:
                 MYSQL_DATABASE: ${DB_DATABASE}
                 MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
                 MYSQL_PASSWORD: ${DB_PASSWORD}
                 MYSQL_USER: ${DB_USERNAME}
             volumes:
                 - ./.docker/mysql/dbdata:/var/lib/mysql
             ports:
                 - 3388:3306
             networks:
                 - laravel-eti
    

    .env:

    DB_CONNECTION=mysql
    DB_HOST=mysql
    DB_PORT=3306
    DB_DATABASE=laravel_ead
    DB_USERNAME=root
    DB_PASSWORD=root
    

    I started using sail, but the problem continues. A new problem has arisen: "SQLSTATE[HY000] [1049] Unknown database 'laravel_ead' (SQL: select * from information_schema.tables where table_schema = laravel_ead and table_name = migrations and table_type = 'BASE TABLE')"

    But there is the database when I access phpmyadmin, if I use the command: "docker exec -it laravel-api-ead-mysql-1 mysql -u root -p" and then SHOW DATABASE; my finger banks are:

      Database |
    +---------------------+
    | information_schema |
    | mysql |
    | you_db |
    | performance_schema |
    | sys
    

    My banks within phpmyadmin do not appear


  2. It’s hard to give you a definitive answer without seeing your Docker configuration, but one recommendation is to make sure the DB_HOST value matches the name of the Docker service that contains your database. For example if your docker-compose.yml looks like this:

      app: # service name for php-fpm
        container_name: php-app
        build:
          context: ./
          dockerfile: app.dockerfile
        working_dir: /var/www
        volumes:
          - ./:/var/www
    
      database: # service name for database
        container_name: app-db
        image: mariadb:10.6.3
        volumes:
          - dbdata:/var/lib/mysql
        ports:
            - "33061:3306"
    
      web: # Nginx service name
        container_name: webserver-web
        build:
          context: ./
          dockerfile: web.dockerfile
        working_dir: /var/www
        volumes_from:
          - app
        ports:
          - "8080:80"
    

    Then in my .env I would use DB_HOST=database. This is possible because Docker makes the service names of all related containers available as hostnames amongst each other.

    One way to verify, assuming your Docker structure is similar, is you could start a bash shell against the service container that is running PHP and try to make a database connection directly.

    cd some/path # root directory where your docker-compose.yml lives
    
    docker-compose exec app /bin/bash
    
    mysql --host=database --port=3306 -u root -proot
    
    

    My suggestions are a shot in the dark, but hopefully something here will get you closer to finding your answer.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search