Code Monkey home page Code Monkey logo

mysql-dual-master-docker'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 dual Master cluster example

Added a new environment variable in the compoer file :

  • MYSQL_MASTER_PORT : Port for setting master

Command to add a auto-increment-offset parameter,set the self growth field to avoid conflict.

Other parameters are written in the optimised.cnf file ,Plus some optimized parameters of the mysqld, based on the 16G memory server. For reference only.

The mysql.sh script is based on the environment that has already been run on the MySQL master slave, will be the script copy / directory, waiting for the container to run it manually after the implementation of.

Tt like this docker exec -it master1_1 sh mysql.sh,then you can test the MySQL dual master. good luck !

mysql-dual-master-docker's People

Contributors

xiaoxiamin avatar

Stargazers

Sean avatar  avatar  avatar

Watchers

 avatar Sean avatar

Forkers

wanweihua

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.