Code Monkey home page Code Monkey logo

mysql-replica's Introduction

MySQL Replication Master/Slave cluster example

One of the most common feature requested for the MySQL docker official image is adding replication master/slave ability. As docker-library/mysql#43 is closed by deciding that it's a feature should be implemented via init script, my example here is a demo of how to do it.

The official MySQL image have the customize ability by running script files under /docker-entrypoint-initdb.d/ directory. The script has to be either a shell script, .sh, or a SQL script, .sql or .sql.gz.

To implement the MySQL replication, I borrowed the code from PR: docker-library/mysql#43, modified and saved as replica.sh. As described in the script comments, we have 5 more special environment variables for replication.

  • MYSQL_REPLICA_USER: create the given user on the intended master host
  • MYSQL_REPLICA_PASS
  • MYSQL_MASTER_SERVER: change master on this location on the intended slave
  • MYSQL_MASTER_PORT: optional, by default 3306
  • MYSQL_MASTER_WAIT_TIME: seconds to wait for the master to come up

The Dockerfile is very simple, just copied replica.sh to /docker-entrypoint-initdb.d/ directory. That's it.

Let's use the new MySQL image with replication ability. To do that, I created a simple docker-compose.yml file:

version: '2'
services:
    master:
        image: twang2218/mysql:5.7-replica
        build: .
        restart: unless-stopped
        ports:
            - 3306:3306
        environment:
            - MYSQL_ROOT_PASSWORD=master_passw0rd
            - MYSQL_REPLICA_USER=replica
            - MYSQL_REPLICA_PASS=replica_Passw0rd
        command: ["mysqld", "--log-bin=mysql-bin", "--server-id=1"]
    slave:
        image: twang2218/mysql:5.7-replica
        build: .
        restart: unless-stopped
        ports:
            - 3307:3306
        environment:
            - MYSQL_ROOT_PASSWORD=slave_passw0rd
            - MYSQL_REPLICA_USER=replica
            - MYSQL_REPLICA_PASS=replica_Passw0rd
            - MYSQL_MASTER_SERVER=master
            - MYSQL_MASTER_WAIT_TIME=10
        command: ["mysqld", "--log-bin=mysql-bin", "--server-id=2"]

Then, just simply run it by docker-compose up -d, then our MySQL replication master and slave are running. Feel free to create something on the master and check whether it's been replicated to the slave.

mysql-replica's People

Contributors

twang2218 avatar

Stargazers

Sean avatar

Watchers

Simin avatar Sean avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.