Skip to content

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

License

Notifications You must be signed in to change notification settings

tariqulgithub/master-replica-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

10 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

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.

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published