├── docker-compose.yml └── README.md /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3' 2 | 3 | services: 4 | mysql-master: 5 | image: mysql:latest 6 | container_name: mysql-master 7 | command: --server-id=1 --log-bin=mysql-bin --binlog-format=row 8 | environment: 9 | MYSQL_ROOT_PASSWORD: root_password 10 | MYSQL_DATABASE: mydatabase 11 | MYSQL_USER: replication_user 12 | MYSQL_PASSWORD: replication_password 13 | ports: 14 | - "3306:3306" 15 | 16 | mysql-slave: 17 | image: mysql:latest 18 | container_name: mysql-slave 19 | depends_on: 20 | - mysql-master 21 | command: --server-id=2 --log-bin=mysql-bin --binlog-format=row 22 | environment: 23 | MYSQL_ROOT_PASSWORD: root_password 24 | MYSQL_DATABASE: mydatabase 25 | MYSQL_USER: replication_user 26 | MYSQL_PASSWORD: replication_password 27 | ports: 28 | - "3307:3306" 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Get your own mini MySQL backup server Up and Running (in Docker!) 🐳 2 | 3 | > [!TIP] 4 | > **Originally published** in [**Dev Community**](https://dev.to/siddhantkcode/how-to-set-up-a-mysql-master-slave-replication-in-docker-4n0a) 5 | 6 | Hey there! Ever wanted to build your own mini MySQL backup server, but worried it'd be a total headache? Well, buckle up, because I'm here to show you it's actually pretty straightforward! 7 | 8 | This guide will walk you through setting up a MySQL master and replica server using Docker Compose. Think of it like a tiny, self-contained MySQL world where your data gets mirrored – pretty cool, right? 9 | 10 | **Why even bother?** 11 | 12 | While some cloud platforms offer automatic backups, building your own replica server gives you a deeper understanding of how replication works. Plus, it's a fun project! 13 | 14 | **Before we dive in, you'll need:** 15 | 16 | - A basic understanding of MySQL (knowing your way around databases is a plus!) 17 | - Docker installed ([https://www.docker.com/](https://www.docker.com/)) 18 | - Docker Compose installed ([https://docs.docker.com/compose/install/](https://docs.docker.com/compose/install/)) 19 | 20 | If you didn't want to setup anything and just wanted to started right away. You can click on the button below to launch the project in [Gitpod](https://www.gitpod.io). 21 | 22 | [![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)](https://gitpod.io/#https://github.com/Siddhant-K-code/mysql-replica-server) 23 | 24 | ## High-Level Architecture 🏗️ 25 | 26 | Here's a revised version of the mermaid diagram for the MySQL Master-Slave replication setup in a vertical layout: 27 | 28 | ```mermaid 29 | graph TD; 30 | A[Docker Compose File] --> B[Docker Compose Up] 31 | B --> C{MySQL Servers Start} 32 | C --> D["MySQL Master (mysql-master)"] 33 | C --> E["MySQL Slave (mysql-slave)"] 34 | D --> F[Configure Master Server] 35 | E --> G[Configure Replica Server] 36 | F --> H[Set Master Replication User] 37 | G --> I[Set Slave Replication Settings] 38 | H --> J[Start Replication on Slave] 39 | I --> J 40 | J --> K[Verify Replication Status] 41 | K --> L{Replication Successful?} 42 | L -->|Yes| M[Operation Check] 43 | L -->|No| N[Review Configuration] 44 | M --> O[Data Consistency Check] 45 | O --> P[Cleanup] 46 | N -.-> F 47 | style D fill:#f9f,stroke:#333,stroke-width:2px 48 | style E fill:#ccf,stroke:#333,stroke-width:2px 49 | style J fill:#cfc,stroke:#333,stroke-width:4px 50 | style L fill:#fcc,stroke:#333,stroke-width:2px 51 | style O fill:#cff,stroke:#333,stroke-width:2px 52 | ``` 53 | 54 | ### Diagram Explanation: 55 | 56 | - **Docker Compose File**: Begin by creating and setting up the `docker-compose.yml` file. 57 | - **Docker Compose Up**: Use Docker Compose to launch the containers. 58 | - **MySQL Servers Start**: Represents the point where both MySQL servers (master and slave) start up. 59 | - **MySQL Master and MySQL Slave**: These nodes split to represent configurations specific to each server. 60 | - **Configure Master Server**: Setting up the master MySQL server with necessary configurations for replication. 61 | - **Configure Replica Server**: Setting up the replica MySQL server according to the master's configurations. 62 | - **Set Master Replication User**: Specific command execution on the master to facilitate replication. 63 | - **Set Slave Replication Settings**: Slave server is set with master's log file and position details. 64 | - **Start Replication on Slave**: The replication process is initiated on the slave server. 65 | - **Verify Replication Status**: Checking if the replication has been set up correctly. 66 | - **Replication Successful?**: Decision node to check if replication is successful. 67 | - **Operation Check**: Performing operations to ensure data consistency across master and slave. 68 | - **Data Consistency Check**: Verifying that the data on both servers are consistent. 69 | - **Cleanup**: Bringing down the Docker containers once testing and verification are complete. 70 | - **Review Configuration**: In case of failed replication, review and correct the configurations. 71 | 72 | ## 1. Setup Docker Compose 🛠️ 73 | 74 | Create a file named `docker-compose.yml` with the following content to define the MySQL primary (master) and replica (slave) services: 75 | 76 | ```yaml 77 | # This line tells Docker Compose the version we're using 78 | version: "3" 79 | 80 | # Here's where we define our services: 81 | services: 82 | # The master server, the OG in this world 83 | mysql-master: 84 | # We'll use the latest MySQL image from Docker Hub 85 | image: mysql:latest 86 | # Give it a cool name (mysql-master is pretty clear, right?) 87 | container_name: mysql-master 88 | # Extra commands to configure the master for replication 89 | command: --server-id=1 --log-bin=mysql-bin --binlog-format=row 90 | # Set some environment variables for passwords and database details 91 | # Remember to replace these with your own strong passwords! 92 | environment: 93 | MYSQL_ROOT_PASSWORD: your_super_secure_root_password 94 | MYSQL_DATABASE: mydatabase # Feel free to change this database name 95 | MYSQL_USER: replication_user # This user will handle replication 96 | MYSQL_PASSWORD: your_super_secure_replication_password 97 | # Map the container port (3306) to your host machine's port (also 3306) 98 | # This lets you access the master server from your machine 99 | ports: 100 | - "3306:3306" 101 | 102 | # The replica server, the master's trusty sidekick 103 | mysql-slave: 104 | # Same image as the master 105 | image: mysql:latest 106 | # Another cool name (can you guess what it is?) 107 | container_name: mysql-slave 108 | # This tells the replica to wait for the master to be ready before starting 109 | depends_on: 110 | - mysql-master 111 | # Similar commands and environment variables as the master 112 | command: --server-id=2 --log-bin=mysql-bin --binlog-format=row 113 | environment: 114 | MYSQL_ROOT_PASSWORD: your_super_secure_root_password # Same password for both 115 | MYSQL_DATABASE: mydatabase 116 | MYSQL_USER: replication_user 117 | MYSQL_PASSWORD: your_super_secure_replication_password 118 | # Map the container port (3306) to a different host machine port (3307 in this case) 119 | ports: 120 | - "3307:3306" 121 | ``` 122 | 123 | This configuration sets up two services: `mysql-master` and `mysql-slave`. 124 | 125 | ## 2. Launch Containers 🚀 126 | 127 | Run the following command to start your containers in detached mode: 128 | 129 | ```bash 130 | docker-compose up -d 131 | ``` 132 | 133 | ## 3. Configure the Master Server 🔧 134 | 135 | Access the master server and configure the MySQL settings: 136 | 137 | ```bash 138 | docker exec -it mysql-master bash 139 | mysql -uroot -p 140 | ``` 141 | 142 | Execute the following SQL commands: 143 | 144 | ```sql 145 | ALTER USER 'replication_user'@'%' IDENTIFIED WITH 'mysql_native_password' BY 'replication_password'; 146 | GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%'; 147 | FLUSH PRIVILEGES; 148 | SHOW MASTER STATUS; 149 | ``` 150 | 151 | Note the log file and position from `SHOW MASTER STATUS` for later use. 152 | 153 | ## 4. Configure the Replica Server 🔧 154 | 155 | Access the replica server: 156 | 157 | ```bash 158 | docker exec -it mysql-slave bash 159 | mysql -uroot -p 160 | ``` 161 | 162 | Configure the replication settings using the master log file and position: 163 | 164 | ```sql 165 | CHANGE MASTER TO 166 | MASTER_HOST='mysql-master', 167 | MASTER_USER='replication_user', 168 | MASTER_PASSWORD='replication_password', 169 | MASTER_LOG_FILE='mysql-bin.xxxxxx', 170 | MASTER_LOG_POS=xxxx; 171 | ``` 172 | 173 | ## 5. Start Replication on Replica Server ▶️ 174 | 175 | Initiate the replication process: 176 | 177 | ```sql 178 | START SLAVE; 179 | ``` 180 | 181 | ## 6. Verify Replication Status 🕵️‍♂️ 182 | 183 | Check the replication status to ensure everything is working correctly: 184 | 185 | ```sql 186 | SHOW SLAVE STATUS\G 187 | ``` 188 | 189 | Confirm that `Slave_IO_Running` and `Slave_SQL_Running` show as "Yes". 190 | 191 | ## 7. Operation Check ✔️ 192 | 193 | Perform a simple data replication test to confirm the setup: 194 | 195 | 1. **On the master server**: 196 | ```sql 197 | use mydatabase; 198 | create table user (id int); 199 | insert into user values (1); 200 | select * from user; 201 | ``` 202 | 2. **On the replica server**: 203 | ```sql 204 | use mydatabase; 205 | select * from user; 206 | ``` 207 | 208 | Ensure the data is consistent across both servers. 209 | 210 | ## Cleanup 🧹 211 | 212 | When done, you can bring down the Docker containers with: 213 | 214 | ```bash 215 | docker-compose down 216 | ``` 217 | 218 | ## Conclusion 🎉 219 | 220 | While cloud providers offer one-click solutions for database replication, setting it up manually provides valuable insights into the operational mechanics. This setup is straightforward but consider advanced features like failover and storage distribution for production environments. 221 | --------------------------------------------------------------------------------