In this guide, we will explore how to set up MySQL master-slave replication using Docker-Compose and establish a connection with a Laravel application. MySQL replication provides high availability and load balancing by allowing data to be replicated from a master database to one or more slave databases.
Docker-Compose Configuration
Start by creating a project directory and organizing it with the following structure:
mysql-replication/ |-- master/ | |-- Dockerfile | |-- my.cnf |-- slave/ | |-- Dockerfile | |-- my.cnf |-- laravel/ | |-- (Laravel files) |-- docker-compose.yml
Docker-Compose Configuration
Create a docker-compose.yml file in the project root:
version: '3'
services:
master-db:
build:
context: ./master
environment:
MYSQL_ROOT_PASSWORD: root_pass
MYSQL_DATABASE: laravel_db
MYSQL_USER: laravel_user
MYSQL_PASSWORD: laravel_pass
ports:
- "3306:3306"
slave-db:
build:
context: ./slave
environment:
MYSQL_ROOT_PASSWORD: root_pass
MYSQL_DATABASE: laravel_db
MYSQL_USER: laravel_user
MYSQL_PASSWORD: laravel_pass
ports:
- "3307:3306"
depends_on:
- master-db
laravel-app:
build:
context: ./laravel
environment:
DB_CONNECTION: mysql
DB_HOST: master-db
DB_PORT: 3306
DB_DATABASE: laravel_db
DB_USERNAME: laravel_user
DB_PASSWORD: laravel_pass
ports:
- "8000:8000"
depends_on:
- master-db
- slave-dbThis docker-compose.yml file defines three services: master-db, slave-db, and laravel-app. The master and slave database services are built from the respective Dockerfile configurations in the master and slave directories.
Dockerfile for MySQL Master
Create a Dockerfile inside the master directory:
FROM mysql:latest COPY ./my.cnf /etc/mysql/my.cnf CMD ["mysqld", "--server-id=1", "--log-bin=mysql-bin", "--binlog-format=row"]
Dockerfile for MySQL Slave
Create a Dockerfile inside the slave directory:
FROM mysql:latest COPY ./my.cnf /etc/mysql/my.cnf CMD ["mysqld", "--server-id=2", "--log-bin=mysql-bin", "--binlog-format=row", "--relay-log=mysql-relay-bin", "--log-slave-updates=1"]
MySQL Configuration Files (For Both Master and Slave)
Create a my.cnf file inside the master and slave directories. Here’s a basic example; adjust as needed:
my.cnf:
[mysqld] server-id = 1 # Set to 2 for the slave log-bin = mysql-bin binlog-format = row
Connecting Laravel to MySQL
Ensure your Laravel application is configured to use MySQL as its database. Update the .env file in the laravel directory with the following settings:
DB_CONNECTION=mysql DB_HOST=master-db DB_PORT=3306 DB_DATABASE=laravel_db DB_USERNAME=laravel_user DB_PASSWORD=laravel_pass
Running the Application
Navigate to the project root directory and execute:
docker-compose up --build
This command will build the necessary Docker images and start the containers for the MySQL master, slave, and Laravel application.
Access the Laravel application at http://localhost:8000. The application will use the MySQL master for read and write operations and the slave for read-only operations, creating a scalable and fault-tolerant database setup.
Conclusion
By utilizing Docker-Compose, you can easily orchestrate a MySQL master-slave replication setup along with a Laravel application. This configuration allows for improved database performance, high availability, and load balancing. As your Laravel application scales, this MySQL replication setup can be a crucial component in ensuring the reliability and efficiency of your database operations. Adjust the configurations based on your specific requirements and continue building robust and scalable applications.

