# MySQL 8.0 master/replica compose for two separate servers. # # Master server: # COMPOSE_PROFILES=master docker compose -f config/docker-compose.mysql-replication.yml up -d # # Replica server: # MASTER_HOST= COMPOSE_PROFILES=replica docker compose -f config/docker-compose.mysql-replication.yml up -d # # Required env on both servers: # MYSQL_ROOT_PASSWORD= # MYSQL_REPLICATION_PASSWORD= # # Optional env: # MYSQL_DATABASE=ppanel # MYSQL_REPLICATION_USER=repl # MYSQL_MASTER_PORT=3306 # MYSQL_REPLICA_PORT=3306 # MYSQL_SERVER_ID=1 # master default # MYSQL_REPLICA_ID=2 # replica default # # If the master already has data, import a GTID-aware dump into the replica # before starting replication. Fresh empty deployments can start master first, # then replica, then point the application at the master. services: mysql-master: image: mysql:8.0 container_name: ppanel-mysql-master profiles: - master restart: always ports: - "${MYSQL_MASTER_PORT:-3306}:3306" environment: MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD:?please set MYSQL_ROOT_PASSWORD}" MYSQL_DATABASE: "${MYSQL_DATABASE:-ppanel}" MYSQL_REPLICATION_USER: "${MYSQL_REPLICATION_USER:-repl}" MYSQL_REPLICATION_PASSWORD: "${MYSQL_REPLICATION_PASSWORD:?please set MYSQL_REPLICATION_PASSWORD}" TZ: Asia/Shanghai command: - --default-authentication-plugin=mysql_native_password - --server-id=${MYSQL_SERVER_ID:-1} - --log-bin=mysql-bin - --binlog-format=ROW - --gtid-mode=ON - --enforce-gtid-consistency=ON - --log-replica-updates=ON - --binlog-expire-logs-seconds=604800 - --max_connections=1000 - --character-set-server=utf8mb4 - --collation-server=utf8mb4_unicode_ci volumes: - mysql_master_data:/var/lib/mysql configs: - source: mysql_master_init target: /docker-entrypoint-initdb.d/01-create-replication-user.sh mode: 0755 healthcheck: test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p$${MYSQL_ROOT_PASSWORD}"] interval: 10s timeout: 5s retries: 10 logging: driver: json-file options: max-size: 10m max-file: "3" mysql-replica: image: mysql:8.0 container_name: ppanel-mysql-replica profiles: - replica restart: always ports: - "${MYSQL_REPLICA_PORT:-3306}:3306" environment: MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD:?please set MYSQL_ROOT_PASSWORD}" MYSQL_DATABASE: "${MYSQL_DATABASE:-ppanel}" TZ: Asia/Shanghai command: - --default-authentication-plugin=mysql_native_password - --server-id=${MYSQL_REPLICA_ID:-2} - --relay-log=mysql-relay-bin - --read-only=ON - --super-read-only=ON - --gtid-mode=ON - --enforce-gtid-consistency=ON - --log-replica-updates=ON - --binlog-format=ROW - --max_connections=1000 - --character-set-server=utf8mb4 - --collation-server=utf8mb4_unicode_ci volumes: - mysql_replica_data:/var/lib/mysql healthcheck: test: ["CMD-SHELL", "mysqladmin ping -h 127.0.0.1 -uroot -p$${MYSQL_ROOT_PASSWORD}"] interval: 10s timeout: 5s retries: 10 logging: driver: json-file options: max-size: 10m max-file: "3" mysql-replica-init: image: mysql:8.0 container_name: ppanel-mysql-replica-init profiles: - replica restart: "no" depends_on: mysql-replica: condition: service_healthy environment: MYSQL_ROOT_PASSWORD: "${MYSQL_ROOT_PASSWORD:?please set MYSQL_ROOT_PASSWORD}" MYSQL_REPLICATION_USER: "${MYSQL_REPLICATION_USER:-repl}" MYSQL_REPLICATION_PASSWORD: "${MYSQL_REPLICATION_PASSWORD:?please set MYSQL_REPLICATION_PASSWORD}" MASTER_HOST: "${MASTER_HOST:?please set MASTER_HOST to the master server ip or hostname}" MASTER_PORT: "${MASTER_PORT:-3306}" entrypoint: - /bin/sh - -ec - | mysql -hmysql-replica -uroot -p"$${MYSQL_ROOT_PASSWORD}" <