Back to All Posts

MySQL Replication Automation with Shell & Ansible

The MySQL Replication Application simplifies the deployment of both unidirectional and bidirectional replication for MySQL databases. It relies on a main installation and configuration script called projet.sh, as well as an auxiliary sql.sh file distributed through Ansible to streamline replication operations.

Project Repository

The full project, including projet.sh, sql.sh and the detailed report, is available on GitHub:

https://github.com/Benhaddou-Lahcen/MySQL-replication

Installation and Configuration

1. Adjust MySQL Directory Path

Before running the projet.sh script located in the mysql directory, you must:

  1. Update the MySQL installation directory by editing the variable:
    projet_directory="/path/to/mysql"
  2. Ensure that each machine participating in the replication uses the same Linux username.
  3. Update the user variable inside projet.sh:
    user="your_username"

2. The sql.sh Management Script

Each replication node will receive the sql.sh file (usually placed in /home) to help automate replication operations.

Note: All machines must have SSH enabled to ensure communication between replication nodes.

Usage

Required Information

You will need the following details to configure replication:

  • Database name
  • IP addresses of all replication machines
  • Total number of machines

1. MySQL Installation (Optional)

If MySQL is not yet installed on the participating machines, the script includes an automatic installation option.

2. Replication Configuration

  1. Run projet.sh on the master (configuration machine).
  2. Choose between:
    • Unidirectional replication
    • Bidirectional replication
  3. Provide:
    • Database name
    • IP addresses of all participating machines
    • The number of nodes

3. Environment Preparation (Optional)

This option helps prepare the environment of each machine by configuring necessary prerequisites before setting up replication.

Conclusion

With this automated tool, setting up MySQL replication becomes significantly faster, more reliable, and easier to maintain. Whether you are using one-way or two-way replication, the combination of shell scripting and Ansible greatly simplifies the process.