Mar 17, 2023
0
Level 1
How to properly configure multiple database connections in sail?
Hello, I'm trying to configure my project with two databases. One is postgresql (the main db) and the other one is mysql.
This is my dockerfile:
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.2
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.2/app
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
- '${VITE_PORT:-5173}:${VITE_PORT:-5173}'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- pgsql
- mysql
pgsql:
image: 'postgres:14'
ports:
- '${FORWARD_DB_PORT:-5432}:5432'
environment:
PGPASSWORD: '${DB_PASSWORD:-secret}'
POSTGRES_DB: '${DB_DATABASE}'
POSTGRES_USER: '${DB_USERNAME}'
POSTGRES_PASSWORD: '${DB_PASSWORD:-secret}'
volumes:
- 'sail-pgsql:/var/lib/postgresql/data'
- './vendor/laravel/sail/database/pgsql/create-testing-database.sql:/docker-entrypoint-initdb.d/10-create-testing-database.sql'
networks:
- sail
healthcheck:
test: ["CMD", "pg_isready", "-q", "-d", "${DB_DATABASE}", "-U", "${DB_USERNAME}"]
retries: 3
timeout: 5s
mysql:
image: 'mysql/mysql-server:8.0'
ports:
- '${FORWARD_DB_PORT2:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD2}'
MYSQL_ROOT_HOST: '%'
MYSQL_DATABASE: '${DB_DATABASE2}'
MYSQL_USER: '${DB_USERNAME2}'
MYSQL_PASSWORD: '${DB_PASSWORD2}'
MYSQL_ALLOW_EMPTY_PASSWORD: 1
volumes:
- 'sail-mysql:/var/lib/mysql'
- './vendor/laravel/sail/database/mysql/create-testing-database.sh:/docker-entrypoint-initdb.d/10-create-testing-database.sh'
networks:
- sail
healthcheck:
test:
- CMD
- mysqladmin
- ping
- '-p${DB_PASSWORD2}'
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sail-pgsql:
driver: local
sail-mysql:
driver: local
My .env file is:
DB_CONNECTION=pgsql
DB_HOST=pgsql
DB_PORT=5432
DB_DATABASE=entropia_api
DB_USERNAME=sail
DB_PASSWORD=password
DB_CONNECTION2=mysql
DB_HOST2=mysql
DB_PORT2=3306
DB_DATABASE2=entropia_api
DB_USERNAME2=sail
DB_PASSWORD2=password
The postgresql connection is working as expected. The other one is giving me
SQLSTATE[HY000] [2006] MySQL server has gone away
I've been able to test the connection by importing some data to the database and it worked:
cat cosmo.sql | docker exec -i edb9f60b1531 /usr/bin/mysql -u sail --password=password entropia_api
What am I missing here?
Please or to participate in this conversation.