Just installed Laravel Sail. How to connect to database?

4.6k views Asked by At

I am trying to connect and create schema using TablePlus (or PhpStorm) in my database since May of 6. Please understand me what I should to do? I will discribe what did I do step by step.

  1. $ laravel new app. I have got /app/ folder with my new project.
  2. cd app/ to enter this folder.
  3. composer require laravel/sail --dev to receive Laravel Sail.
  4. php artisan sail:install. There terminal asks me Which services I would like to install?. I write mysql and press Enter.
  5. gedit .env to open .env file to see connection settings.
APP_ENV=local
APP_KEY=base64:fXZ06W8BPoRShk8cYlHgS0u/+IBIZTSOM20gFRON6d4=
APP_DEBUG=true
APP_URL=http://app.test

LOG_CHANNEL=stack
LOG_LEVEL=debug

DB_CONNECTION=mysql
DB_HOST=mysql
DB_PORT=3306
DB_DATABASE=app
DB_USERNAME=sail
DB_PASSWORD=password

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

MEMCACHED_HOST=memcached

REDIS_HOST=redis
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
  1. gedit docker-compose.yml to see these setting here.
# For more information: https://laravel.com/docs/sail
version: '3'
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.0
            dockerfile: Dockerfile
            args:
                WWWGROUP: '${WWWGROUP}'
        image: sail-8.0/app
        ports:
            - '${APP_PORT:-80}:80'
        environment:
            WWWUSER: '${WWWUSER}'
            LARAVEL_SAIL: 1
        volumes:
            - '.:/var/www/html'
        networks:
            - sail
        depends_on:
            - mysql
    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
        healthcheck:
          test: ["CMD", "mysqladmin", "ping"]
networks:
    sail:
        driver: bridge
volumes:
    sailmysql:
        driver: local
  1. sail up -d to run Sail.
  2. Run TablePlus to connect to the database. What should I write in host? When i write mysql I am getting error "Unknown MySQL server mysql". Please help!
1

There are 1 answers

0
Nobu On BEST ANSWER

From TablePlus, the host connecting to would be 127.0.0.1. mysql is a host name inside the Docker Compose network which TablePlus can not access to.

ref. https://docs.docker.com/compose/networking/

# if your local environment has mysql-client, this will connect to the database
mysql --port=3306 --host=127.0.0.1 --user=sail --password

# from your project directory
./vendor/bin/sail mysql

# via docker exec
docker ps | grep mysql
docker exec -it YOUR-MYSQL-CONTAINER-NAME mysql --password