Skip to content

Instantly share code, notes, and snippets.

@andersonpem
Created August 6, 2023 17:10
Show Gist options
  • Save andersonpem/f96d97cd9006fd663e5f16813d80d8a3 to your computer and use it in GitHub Desktop.
Save andersonpem/f96d97cd9006fd663e5f16813d80d8a3 to your computer and use it in GitHub Desktop.
A Doctrine (Symfony) master-replica database configuration
# SET YOUR ENVS :)
# Master Database Environment Variables
DATABASE_DRIVER=pdo_mysql
DATABASE_HOST=mysql-master-host
DATABASE_PORT=3306
DATABASE_NAME=app
DATABASE_USER=root
DATABASE_PASSWORD=root
DATABASE_SERVER_VERSION=8
DATABASE_CHARSET=utf8mb4
# Database replica 1 configuration
DATABASE_REPLICA1_HOST=mysql-replica1-host
DATABASE_REPLICA1_DATABASE_NAME=app
DATABASE_REPLICA1_PORT=3306
DATABASE_REPLICA1_USER=root
DATABASE_REPLICA1_PASSWORD=root
DATABASE_REPLICA1_CHARSET=utf8mb4
# Database replica 2 configuration
DATABASE_REPLICA2_HOST=mysql-replica2-host
DATABASE_REPLICA2_DATABASE_NAME=app
DATABASE_REPLICA2_PORT=3306
DATABASE_REPLICA2_USER=root
DATABASE_REPLICA2_PASSWORD=root
DATABASE_REPLICA2_CHARSET=utf8mb4

Doctrine (Symfony) Master-Replica(s) configuration

I was struggling to set this up because most documentation I found refers to the replicas as slaves (which is a terminology still in use by MySQL, for example). The newer Doctrine versions use the term replicas to refer to slave servers.

You're welcome :)

doctrine:
dbal:
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '15'
default_connection: default
connections:
default:
driver: "%env(DATABASE_DRIVER)%"
host: "%env(DATABASE_HOST)%"
port: "%env(DATABASE_PORT)%"
dbname: "%env(DATABASE_NAME)%"
user: "%env(DATABASE_USER)%"
password: "%env(DATABASE_PASSWORD)%"
server_version: "%env(DATABASE_SERVER_VERSION)%"
charset: "%env(DATABASE_CHARSET)%"
replicas:
replica1:
dbname: "%env(DATABASE_REPLICA1_DATABASE_NAME)%"
host: "%env(DATABASE_REPLICA1_HOST)%"
port: "%env(DATABASE_REPLICA1_PORT)%"
user: "%env(DATABASE_REPLICA1_USER)%"
password: "%env(DATABASE_REPLICA1_PASSWORD)%"
charset: "%env(DATABASE_REPLICA1_CHARSET)%"
replica2:
dbname: "%env(DATABASE_REPLICA2_DATABASE_NAME)%"
host: "%env(DATABASE_REPLICA2_HOST)%"
port: "%env(DATABASE_REPLICA2_PORT)%"
user: "%env(DATABASE_REPLICA2_USER)%"
password: "%env(DATABASE_REPLICA2_PASSWORD)%"
charset: "%env(DATABASE_REPLICA2_CHARSET)%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment