https://github.com/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.
https://github.com/tariqulgithub/master-replica-example
database devops docker docker-compose docker-container docker-image gtid gtid-replication master-replica mysql mysql-database open-source read-only replication
Last synced: about 2 months ago
JSON representation
MySQL 8 Master-Replica Docker Compose setup with separate users for app, replication, and read-only reporting. Ready for development and testing.
- Host: GitHub
- URL: https://github.com/tariqulgithub/master-replica-example
- Owner: tariqulgithub
- License: mit
- Created: 2025-12-17T18:11:46.000Z (6 months ago)
- Default Branch: main
- Last Pushed: 2025-12-17T19:44:23.000Z (6 months ago)
- Last Synced: 2025-12-21T06:52:51.747Z (6 months ago)
- Topics: database, devops, docker, docker-compose, docker-container, docker-image, gtid, gtid-replication, master-replica, mysql, mysql-database, open-source, read-only, replication
- Homepage: https://github.com/tariqulgithub/master-replica-example
- Size: 22.5 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.