Search code examples
phpdockerlaravel-8mysql-8.0laravel-sail

Laravel Sail on Existing Project - Unknown Database SQLSTATE[HY000] [1049]


I'm trying to spool up an existing project locally using Laravel Sail. I'm continually getting the 1049 unknown database error. I've tried the following:

sail php artisan config:clear
sail php artisan config:cache

I've also tried replacing mysql as the host to using 127.1.1.0 like the .env.example file, but no luck.

I've also tried to rebuild the container but nothing seems to resolve the problem.

enter image description here

Here is my .env file (when I try with a user other than root I get a 1045 error saying access denied, wondering if these are related):

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

Here's my docker-compose.yml file:

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

database config:

'mysql' => [
            'driver' => 'mysql',
            'url' => env('DATABASE_URL'),
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', 'forge'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'prefix_indexes' => true,
            'strict' => false,
            'engine' => null,
            'options' => extension_loaded('pdo_mysql') ? array_filter([
                PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
            ]) : [],
        ],

UPDATE:

I installed a fresh install of laravel in an adjacent directory on the the same WSL installation and verified the docker-compose.yml, .env, and database.php config files are all the same. The new install booted up fine and I can connect to the DB, the existing project still doesn't work. Really confused..


Solution

  • Ok, after a LOT of digging, I was finally able to get this to work. I'm going to explain what I found for anyone who happens by this in the future.

    I think the root cause was that I attempted to run the sail up command initially without an .env file, which causes a volume to be created for mysql, and then won't be recreated when you do have an .env file. I found that information here, although the --rmi flag mentioned didn't seem to do anything for me.

    So I started by manually deleting the volumes. I got their location from this SO answer. I think manually deleting them was the wrong step, but here is where they are:

    \\wsl$\docker-desktop-data\version-pack-data\community\docker\volumes

    Manually deleting them caused another error the next time I built, but I was able to solve that using the info found here by running these commands:

    docker volume prune

    docker system prune

    Note: I think you could run these without deleting the volumes manually and probably have the same result. Generally I don't think you want to be manually deleting files within WSL from Windows Explorer.

    Once that was done I ran the following to clear the cache and config:

    sail php artisan cache:clear

    sail php artisan config:clear

    (These assume you have the sail alias set up in your .bashrc file, otherwise its ./vendor/bin/sail ...).

    then I did a sail down and a sail up -d followed by sail php artisan migrate and voila, it worked!