Connect Laravel to Mysql

Asked

Viewed 121 times

-1

I’m trying to develop an Laravel application with Docker

My Docker-Compose.yml file looks like this:

version: "3.3"
services:
  mysql:
    image: mysql:8.0.1
    container_name: mysql
    environment: 
      MYSQL_ROOT_PASSWORD: "123"
    ports:
      - '3307:3306'
    volumes:
      - mysql-db:/var/lib/mysql
    networks:
      - code-network 
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    container_name: phpmyadmin
    ports:
      - 8080:80
    links: 
      - mysql:db
    networks:
      - code-network 
  php7.3:
    image: quay.io/vesica/php73:latest
    container_name: php73
    volumes:
      - ./var/www/html:/var/www/html
      - ./etc/apache2/sites-available:/etc/apache2/sites-available
    ports:
      - 80:8080
    networks:
      - code-network   
volumes:
  mysql-db:  
networks:
  code-network:
    driver: bridge

The file configuration . env is like this:

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3307
DB_DATABASE=meubanco
DB_USERNAME=root
DB_PASSWORD=123

But when I try to access my application I have the following message:

Illuminate Database Queryexception (2002) SQLSTATE[HY000] [2002] Connection refused (SQL: select * from users Where enable = S)

What is still missing?

1 answer

2


I found the problem:

I added the link to the php73 container:

links: 
      - mysql:db

Then my file went like this:

version: "3.3"
services:
  mysql:
    image: mysql:8.0.1
    container_name: mysql
    environment: 
      MYSQL_ROOT_PASSWORD: "123"
    ports:
      - '3307:3306'
    volumes:
      - mysql-db:/var/lib/mysql

  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    container_name: phpmyadmin
    ports:
      - 8080:80
    links: 
      - mysql:db

  php7.3:
    image: quay.io/vesica/php73:latest
    container_name: php73
    volumes:
      - ./var/www/html:/var/www/html
      - ./etc/apache2/sites-available:/etc/apache2/sites-available
    ports:
      - 80:8080
    links: 
      - mysql:db
volumes:
  mysql-db: 

And in the file . env called the host mysql

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3307
DB_DATABASE=meubanco
DB_USERNAME=root
DB_PASSWORD=123

I hope I can help someone in the future

  • And what would be the db in mysql:db?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.