Cookie Consent by Free Privacy Policy Generator Aktuallisiere deine Cookie Einstellungen ๐Ÿ“Œ How to Set Up a MySQL Master-Slave Replication in Docker


๐Ÿ“š How to Set Up a MySQL Master-Slave Replication in Docker


๐Ÿ’ก Newskategorie: Programmierung
๐Ÿ”— Quelle: dev.to

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!

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?

Why even bother?

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!

Before we dive in, you'll need:

If you didn't want to setup anything and just wanted to start immediately. You can click on the button below to launch the project in Gitpod.

Open in Gitpod

High-Level Architecture ๐Ÿ—๏ธ

Here's a revised version of the mermaid diagram for the MySQL Master-Slave replication setup in a vertical layout:

High-level architecture diagram

Diagram Explanation:

  • Docker Compose File: Begin by creating and setting up the docker-compose.yml file.
  • Docker Compose Up: Use Docker Compose to launch the containers.
  • MySQL Servers Start: Represents the point where both MySQL servers (master and slave) start up.
  • MySQL Master and MySQL Slave: These nodes split to represent configurations specific to each server.
  • Configure Master Server: Setting up the master MySQL server with necessary configurations for replication.
  • Configure Replica Server: Setting up the replica MySQL server according to the master's configurations.
  • Set Master Replication User: Specific command execution on the master to facilitate replication.
  • Set Slave Replication Settings: Slave server is set with master's log file and position details.
  • Start Replication on Slave: The replication process is initiated on the slave server.
  • Verify Replication Status: Checking if the replication has been set up correctly.
  • Replication Successful?: Decision node to check if replication is successful.
  • Operation Check: Performing operations to ensure data consistency across master and slave.
  • Data Consistency Check: Verifying that the data on both servers are consistent.
  • Cleanup: Bringing down the Docker containers once testing and verification are complete.
  • Review Configuration: In case of failed replication, review and correct the configurations.

Setup

1. Setup Docker Compose ๐Ÿ› ๏ธ

Create a file named docker-compose.yml with the following content to define the MySQL primary (master) and replica (slave) services:

# This line tells Docker Compose the version we're using
version: "3"

# Here's where we define our services:
services:
  # The master server, the OG in this world
  mysql-master:
    # We'll use the latest MySQL image from Docker Hub
    image: mysql:latest
    # Give it a cool name (mysql-master is pretty clear, right?)
    container_name: mysql-master
    # Extra commands to configure the master for replication
    command: --server-id=1 --log-bin=mysql-bin --binlog-format=row
    # Set some environment variables for passwords and database details
    # Remember to replace these with your own strong passwords!
    environment:
      MYSQL_ROOT_PASSWORD: your_super_secure_root_password
      MYSQL_DATABASE: mydatabase # Feel free to change this database name
      MYSQL_USER: replication_user # This user will handle replication
      MYSQL_PASSWORD: your_super_secure_replication_password
    # Map the container port (3306) to your host machine's port (also 3306)
    # This lets you access the master server from your machine
    ports:
      - "3306:3306"

  # The replica server, the master's trusty sidekick
  mysql-slave:
    # Same image as the master
    image: mysql:latest
    # Another cool name (can you guess what it is?)
    container_name: mysql-slave
    # This tells the replica to wait for the master to be ready before starting
    depends_on:
      - mysql-master
    # Similar commands and environment variables as the master
    command: --server-id=2 --log-bin=mysql-bin --binlog-format=row
    environment:
      MYSQL_ROOT_PASSWORD: your_super_secure_root_password # Same password for both
      MYSQL_DATABASE: mydatabase
      MYSQL_USER: replication_user
      MYSQL_PASSWORD: your_super_secure_replication_password
    # Map the container port (3306) to a different host machine port (3307 in this case)
    ports:
      - "3307:3306"

This configuration sets up two services: mysql-master and mysql-slave.

2. Launch Containers ๐Ÿš€

Run the following command to start your containers in detached mode:

docker-compose up -d

3. Configure the Master Server ๐Ÿ”ง

Access the master server and configure the MySQL settings:

docker exec -it mysql-master bash
mysql -uroot -p

Execute the following SQL commands:

ALTER USER 'replication_user'@'%' IDENTIFIED WITH 'mysql_native_password' BY 'replication_password';
GRANT REPLICATION SLAVE ON *.* TO 'replication_user'@'%';
FLUSH PRIVILEGES;
SHOW MASTER STATUS;

Note the log file and position from SHOW MASTER STATUS for later use.

4. Configure the Replica Server ๐Ÿ”ง

Access the replica server:

docker exec -it mysql-slave bash
mysql -uroot -p

Configure the replication settings using the master log file and position:

CHANGE MASTER TO
  MASTER_HOST='mysql-master',
  MASTER_USER='replication_user',
  MASTER_PASSWORD='replication_password',
  MASTER_LOG_FILE='mysql-bin.xxxxxx',
  MASTER_LOG_POS=xxxx;

5. Start Replication on Replica Server โ–ถ๏ธ

Initiate the replication process:

START SLAVE;

6. Verify Replication Status ๐Ÿ•ต๏ธโ€โ™‚๏ธ

Check the replication status to ensure everything is working correctly:

SHOW SLAVE STATUS\G

Confirm that Slave_IO_Running and Slave_SQL_Running show as "Yes".

7. Operation Check โœ”๏ธ

Perform a simple data replication test to confirm the setup:

  1. On the master server:
   use mydatabase;
   create table user (id int);
   insert into user values (1);
   select * from user;
  1. On the replica server:
   use mydatabase;
   select * from user;

Ensure the data is consistent across both servers.

Cleanup ๐Ÿงน

When done, you can bring down the Docker containers with:

docker-compose down

Conclusion ๐ŸŽ‰

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.

...



๐Ÿ“Œ Working of MySQL Replication Filters When Using Statement-based and Row-based Replication


๐Ÿ“ˆ 37.26 Punkte

๐Ÿ“Œ Understanding Replication in PostgreSQL โ€“ How to Set Up PostgreSQL Streaming Replication


๐Ÿ“ˆ 37 Punkte

๐Ÿ“Œ MySQL Multi-Master Replication Manager 2.2.1 on Solaris mmm_agentd send_arp MMM Protocol Message command injection


๐Ÿ“ˆ 30.97 Punkte

๐Ÿ“Œ MySQL Multi-Master Replication Manager 2.2.1 on FreeBSD mmm_agentd clear_ip MMM Protocol Message command injection


๐Ÿ“ˆ 30.97 Punkte

๐Ÿ“Œ MySQL Multi-Master Replication Manager 2.2.1 on Solaris mmm_agentd clear_ip MMM Protocol Message command injection


๐Ÿ“ˆ 30.97 Punkte

๐Ÿ“Œ MySQL Multi-Master Replication Manager 2.2.1 on Linux mmm_agentd clear_ip MMM Protocol Message command injection


๐Ÿ“ˆ 30.97 Punkte

๐Ÿ“Œ MySQL Multi-Master Replication Manager 2.2.1 on FreeBSD mmm_agentd add_ip MMM Protocol Message command injection


๐Ÿ“ˆ 30.97 Punkte

๐Ÿ“Œ MySQL Multi-Master Replication Manager 2.2.1 on Solaris mmm_agentd add_ip MMM Protocol Message command injection


๐Ÿ“ˆ 30.97 Punkte

๐Ÿ“Œ MySQL Multi-Master Replication Manager 2.2.1 on Linux mmm_agentd add_ip MMM Protocol Message command injection


๐Ÿ“ˆ 30.97 Punkte

๐Ÿ“Œ MySQL Multi-Master Replication Manager 2.2.1 mmm_agentd _execute MMM Protocol Message command injection


๐Ÿ“ˆ 30.97 Punkte

๐Ÿ“Œ How To Skip Replication Errors in GTID-Based Replication


๐Ÿ“ˆ 29.89 Punkte

๐Ÿ“Œ How to Convert PostgreSQL Streaming Replication to Logical Replication


๐Ÿ“ˆ 29.89 Punkte

๐Ÿ“Œ Database Replication Encyclopaedia โ€” Single Leader Replication (1/3)


๐Ÿ“ˆ 29.89 Punkte

๐Ÿ“Œ Docker users unhappy with latest forced login to download Docker and Docker Store images


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Mehrere Probleme in containerd, docker-runc, go1.11, go1.12, golang-github-docker-libnetwork, go und docker (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Security: Mehrere Probleme in containerd, docker-runc, go1.11, go1.12, golang-github-docker-libnetwork, go und docker (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Mehrere Probleme in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Mangelnde Rechteprรผfung in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Preisgabe von Informationen in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Preisgabe von Informationen in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Denial of Service in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Preisgabe von Informationen in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Ausfรผhren von Code mit hรถheren Privilegien in docker-runc, golang-github-docker-libnetwork, docker und containerd (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Mehrere Probleme in docker-runc, golang-github-docker-libnetwork, docker und containerd (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Security: Mehrere Probleme in docker-runc, golang-github-docker-libnetwork, docker und containerd (SUSE)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Docker Stack Tutorial | Docker Stack Deploy Docker-Compose.yml


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Implementaciรณn de una Aplicaciรณn SpringBoot Utilizando Docker y Jenkins (Docker-outside-of-Docker - DooD)


๐Ÿ“ˆ 24.67 Punkte

๐Ÿ“Œ Percona XtraDB Multi-Master Replication cluster setup between 3 nodes


๐Ÿ“ˆ 23.6 Punkte

๐Ÿ“Œ Practical PostgreSQL Logical Replication: Setting Up an Experimentation Environment Using Docker


๐Ÿ“ˆ 23.17 Punkte

๐Ÿ“Œ Oracle MySQL Server 5.6.34/5.7.16 Replication Denial of Service


๐Ÿ“ˆ 22.32 Punkte

๐Ÿ“Œ Oracle MySQL Server bis 5.6.27/5.7.9 Replication Denial of Service


๐Ÿ“ˆ 22.32 Punkte

๐Ÿ“Œ Oracle MySQL Server up to 5.5.47/5.6.28/5.7.10 Replication denial of service


๐Ÿ“ˆ 22.32 Punkte

๐Ÿ“Œ Oracle MySQL Server up to 5.6.23 Replication denial of service


๐Ÿ“ˆ 22.32 Punkte

๐Ÿ“Œ Oracle MySQL Server up to 5.7.13 Replication denial of service


๐Ÿ“ˆ 22.32 Punkte

๐Ÿ“Œ Oracle MySQL Server bis 5.5.47/5.6.28/5.7.10 Replication Denial of Service


๐Ÿ“ˆ 22.32 Punkte











matomo