John Davidson

php - Laravel 8 migration works, but MySQL Refuses Connection

0 comments
Message:


I have a laravel project setup with docker (I currently run it using docker-compose). The project was working fine yesterday but it has stopped functioning partially today.

The most amazing thing is that although migrations and seeders work just fine, I can't fetch any result from the database! I've tried clearing caches and every other guide I could find but they didn't work at all.
My file contents are:


.env


APP_NAME=bookshelf
APP_ENV=local
APP_KEY=base64:/PqZs+/8YfRYtCth0U5LATD5oOXCN/5pdAs5onk1z7U=
APP_DEBUG=true
APP_URL=http://0.0.0.0:8000

DB_CONNECTION=mysql
DB_HOST=0.0.0.0
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=laravel
DB_PASSWORD=secret

docker-compose.yml


version: "3.7"

services:
web:
image: $COMPOSE_PROJECT_NAME
container_name: $COMPOSE_PROJECT_NAME-laravel
ports:
- 8000:8000
volumes:
- ./:/app

mysql:
image: mysql:8
container_name: $COMPOSE_PROJECT_NAME-mysql
ports:
- '3306:3306'
environment:
MYSQL_USER: laravel
MYSQL_PASSWORD: secret
MYSQL_DATABASE: laravel
MYSQL_ROOT_PASSWORD: secret

phpmyadmin:
image: phpmyadmin/phpmyadmin:latest
environment:
PMA_HOST: 'mysql'
ports:
- '127.0.0.1:8080:80'

Dockerfile


FROM php:8.0

RUN apt-get update && apt-get install -y \
libzip-dev \
&& docker-php-ext-install \
pdo_mysql \
zip
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
WORKDIR /app
COPY . /app
RUN composer install

CMD php artisan serve --host=0.0.0.0 --port=8000
EXPOSE 8000

As I said, the following commands work totally ok:


php artisan migrate
php artisan db:seed

But when I want to load a page with the following Controller@method:


public function index()
{
$records = Book::all();
...

which fetches results from books table, I get the following error in the corresponding page/url/route:



So... I appreciate any help!



My Answer:
It looks like the issue might be with the database connection settings in your Laravel project.

First, make sure that your MySQL container is running and accessible. You can check this by running `docker ps` and verifying that the MySQL container is up and running.

Next, try updating the `DB_HOST` in your `.env` file to `mysql` instead of `0.0.0.0`. This is because Docker containers communicate with each other using the service name as the hostname. So in your case, the MySQL service is named `mysql`.

Your `.env` file should look like this:


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


After updating the `.env` file, try running your Laravel project again and see if you are able to fetch results from the database.

If you are still facing issues, you can try connecting to the MySQL container using a tool like MySQL Workbench or phpMyAdmin to verify that the database and tables are set up correctly.

Let me know if this helps or if you need further assistance!

Rate this post

4 of 5 based on 4849 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us