GitHunt
TA

tariqulgithub/master-replica-example

MySQL 8 Master-Replica Docker Compose setup with separate users for app, replication, and read-only reporting. Ready for development and testing.

MySQL Master-Replica Example ๐Ÿฌ

This project demonstrates a MySQL 8 master-replica setup using Docker Compose, with separate users for application, replication, and read-only reporting.

The setup is ready for development and testing, and can be adapted for production reporting replicas.


Features โœ…

  • Master (`mysql-master`)

    • App database: `relvora`
    • App user: `relvora` (full access to `relvora` database)
    • Replication user: `repl`
  • Replica (`mysql-replica`)

    • Read-only replica of master database
    • Reporting user: `report` (read-only access to all databases)
  • Automatic replication via GTID (Global Transaction IDs)

  • `read-only` mode on replica ensures safety

  • Fully scripted with Docker Compose + SQL init scripts


Project Structure ๐Ÿ“

master-replica-example/
โ”‚
โ”œโ”€ docker-compose.yml         # Docker Compose setup for master and replica
โ”œโ”€ master-init/
โ”‚   โ””โ”€ master-init.sql        # Master initialization: app user, replication user, reporting user
โ”œโ”€ replica-init/
โ”‚   โ””โ”€ replica-init.sql       # Replica initialization: configure replication from master
โ””โ”€ README.md                  # Project documentation

Setup Instructions ๐Ÿ› ๏ธ

1. Clone the repository

git clone https://github.com/tariqulgithub/master-replica-example.git
cd master-replica-example

2. Create network if not exist

docker network create --driver bridge relvora-network
docker network ls

3. Start the containers

Warning: For the first run, remove old volumes to allow init scripts to execute properly.

docker compose down -v
docker compose up -d

4. Verify the setup

Check master databases:

docker exec -it mysql-master mysql -urelvora -prelvorapassword -e "SHOW DATABASES;"

Check replica databases (read-only):

docker exec -it mysql-replica mysql -ureport -preportpassword -e "SHOW DATABASES;"

Check replication status:

docker exec -it mysql-replica mysql -uroot -prootpassword -e "SHOW REPLICA STATUS\G"

Users & Permissions ๐Ÿ”‘

User Host Permissions Usage
`root` `%` Full access (master), limited on replica Admin
`relvora` `%` All privileges on `relvora` database App
`repl` `%` Replication privileges Replication threads
`report` `%` `SELECT` on all databases Read-only reporting on replica

Notes โš ๏ธ

  • The replica is read-only, so no writes are allowed except through replication.
  • Always remove volumes for the first run to ensure init scripts execute.

License

This project is open source and available under the MIT License.