├── .env ├── .gitignore ├── README.md ├── check_params.sh ├── conf ├── master1.cnf ├── master2.cnf ├── slave1.cnf └── slave2.cnf ├── data ├── master1 │ └── .gitignore ├── master2 │ └── .gitignore ├── slave1 │ └── .gitignore └── slave2 │ └── .gitignore ├── docker ├── common-services.yml ├── master-2-master.yml ├── master-2-slave.yml └── slave-2-master-2-master-2-slave.yml ├── info ├── scripts ├── common-functions.sh ├── master-2-master.sh ├── master-2-slave.sh ├── no-repl-selected.sh └── slave-2-master-2-master-2-slave.sh ├── start └── stop /.env: -------------------------------------------------------------------------------- 1 | MASTER_1_HOSTNAME=master_1 2 | MASTER_1_HOST_IP=10.208.0.100 3 | MASTER_1_ROOT_USER=root 4 | MASTER_1_ROOT_PASSWORD=rootpswdmaster1 5 | 6 | MASTER_2_HOSTNAME=master_2 7 | MASTER_2_HOST_IP=10.208.0.200 8 | MASTER_2_ROOT_USER=root 9 | MASTER_2_ROOT_PASSWORD=rootpswdmaster2 10 | 11 | 12 | SLAVE_1_HOSTNAME=slave_1 13 | SLAVE_1_HOST_IP=10.208.0.101 14 | SLAVE_1_ROOT_USER=root 15 | SLAVE_1_ROOT_PASSWORD=rootpswdslave1 16 | 17 | SLAVE_2_HOSTNAME=slave_2 18 | SLAVE_2_HOST_IP=10.208.0.201 19 | SLAVE_2_ROOT_USER=root 20 | SLAVE_2_ROOT_PASSWORD=rootpswdslave2 21 | 22 | REPL_USER=slaverepluser 23 | REPL_PASSWORD=slavereplpswd 24 | 25 | REPL_SCRIPT_MOUNT_PATH=/opt/repl/scripts 26 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | scripts/*.sql 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Introduction 2 | 3 | This is a sample work to learn about docker, containers, running MySQL in docker and setting up MySQL 4 | replication using docker containers. 5 | 6 | # Directory structure explained 7 | Here is the directory structure and its explanation 8 | 9 | MySQLReplDocker 10 | | 11 | +--- conf/ 12 | | +--- master1.cnf 13 | | +--- master2.cnf 14 | | +--- slave1.cnf 15 | | +--- slave2.cnf 16 | +--- data/ 17 | | +--- master1/ 18 | | +--- master2/ 19 | | +--- slave1/ 20 | | +--- slave2/ 21 | +--- docker/ 22 | | +--- master-2-slave.yml 23 | | +--- master-2-master.yml 24 | | +--- slave-2-master-2-master-2-slave.yml 25 | +--- scripts/ 26 | | +--- common-functions.sh 27 | | +--- master-2-slave.sh 28 | | +--- master-2-master.sh 29 | | +--- slave-2-master-2-master-2-slave.sh 30 | +--- start 31 | +--- stop 32 | +--- check_params.sh 33 | 34 | 35 | * The folder "___conf___" contains the MySQL configuration for master and slave nodes. 36 | * The folder "___data___" contains folders to hold data for master and slave nodes. 37 | * The folder "___docker___" contains the docker container configuation files 38 | * The folder "___scripts___" contains bash script which does the real job of setup of replication between the nodes 39 | 40 | # How to get MySQL containers up and running 41 | To run a MySQL master-slave container replication execute the command 42 | ```sh 43 | bash start master-slave 44 | ``` 45 | To run a MySQL master-master container replication execute the command 46 | ```sh 47 | bash start master-master 48 | ``` 49 | To run a MySQL slave-master-master-slave replication execute the command 50 | ```sh 51 | bash start slave-master-master-slave 52 | ``` 53 | 54 | # Seeing replication in action 55 | In order to see if the replication is actually working, you need to enable our nerd mode. Lets kick in. Execute the following command to see the docker containers which are currently running 56 | ```sh 57 | bash info master-slave 58 | ``` 59 | This will list down the containers, where you can clearly identify the MySQL master(s) and slave(s) nodes. In order to connect to any of the container, you can execute 60 | ```sh 61 | docker container exec -it mysql -u root -p 62 | ``` 63 | > The name of the container is the first column from the output of info command. Once you are inside the prompt you can execute either of the following commands 64 | > 65 | > SHOW MASTER STATUS 66 | > 67 | > SHOW SLAVE STATUS\G 68 | > 69 | > NOTE: Find the passwords in .env file 70 | 71 | You can repeat the command for all the MySQL containers and then play around and see the replication action coming live. 72 | 73 | In case you are not comfortable using command line for interacting with MySQL, this repo also has support for phpMyAdmin. 74 | You can access phpMyAdmin over the port 8080 75 | 76 | > Access http://localhost:8080 in your favorite browser 77 | > 78 | > For hostname, username, and password refer to the .env file 79 | -------------------------------------------------------------------------------- /check_params.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | BASEDIR=$(dirname "$0") 4 | 5 | file=$1 file_found=0 6 | usage_shown=0 7 | docker_folder="$BASEDIR/docker" 8 | 9 | function show_usage() { 10 | if [ $file_found -eq 0 ] 11 | then 12 | echo "Invalid configuration file specified" 13 | echo 14 | echo "Usage: bash start.sh " 15 | echo 16 | echo "Short options: ms | mm | smms" 17 | echo "Long options : master-to-slave | master-to-master | slave-to-master-to-slave" 18 | echo 19 | exit 0 20 | fi 21 | } 22 | 23 | function check_shortcut() { 24 | case $file in 25 | "ms"|"masterslave"|"master-slave"|"master2slave"|"master-2-slave") 26 | file="master-2-slave.yml" 27 | file_found=1 28 | ;; 29 | "mm"|"mastermaster"|"master-master"|"master2master"|"master-2-master") 30 | file="master-2-master.yml" 31 | file_found=1 32 | ;; 33 | "smms"|"slavemastermasterslave"|"slave-master-master-slave"|"slave2master2master2slave"|"slave-2-master-2-master-2-slave") 34 | file="slave-2-master-2-master-2-slave.yml" 35 | file_found=1 36 | ;; 37 | esac 38 | 39 | } 40 | 41 | function check_docker_config() { 42 | check_shortcut 43 | 44 | if [ $file_found -eq 1 ] 45 | then 46 | cfiles=("master-2-slave.yml" "master-2-master.yml" "slave-2-master-2-master-2-slave.yml") 47 | for cfile in ${cfiles[@]} 48 | do 49 | if [ "$cfile" == "$file" ] && [ -f "$docker_folder/$cfile" ] 50 | then 51 | file=$docker_folder/$cfile 52 | file_found=1 53 | break; 54 | fi 55 | done 56 | else 57 | show_usage 58 | fi 59 | } 60 | -------------------------------------------------------------------------------- /conf/master1.cnf: -------------------------------------------------------------------------------- 1 | [mysqld] 2 | server-id = 100 3 | bind-address = master_1 4 | innodb_flush_log_at_trx_commit = 1 5 | sync_binlog = 1 6 | binlog-format = ROW 7 | log_bin = /var/log/mysql/mysql-bin.log 8 | log_bin_index = /var/log/mysql/mysql-bin.log.index 9 | relay-log = /var/log/mysql/mysql-relay-bin 10 | relay-log-index = /var/log/mysql/mysql-relay-bin.index 11 | relay-log-info-file = /var/lib/mysql/mysql-relay-log.info 12 | master-info-file = /var/lib/mysql/mysql-master.info 13 | log-error = /var/lib/mysql/mysql.err 14 | auto-increment-offset = 1 15 | auto_increment_increment = 2 16 | log-slave-updates = 1 17 | 18 | skip-name-resolve 19 | 20 | gtid_mode = ON 21 | enforce-gtid-consistency 22 | -------------------------------------------------------------------------------- /conf/master2.cnf: -------------------------------------------------------------------------------- 1 | [mysqld] 2 | server-id = 200 3 | bind-address = master_2 4 | innodb_flush_log_at_trx_commit = 1 5 | sync_binlog = 1 6 | binlog-format = ROW 7 | log_bin = /var/log/mysql/mysql-bin.log 8 | log_bin_index = /var/log/mysql/mysql-bin.log.index 9 | relay-log = /var/log/mysql/mysql-relay-bin 10 | relay-log-index = /var/log/mysql/mysql-relay-bin.index 11 | relay-log-info-file = /var/lib/mysql/mysql-relay-log.info 12 | master-info-file = /var/lib/mysql/mysql-master.info 13 | log-error = /var/lib/mysql/mysql.err 14 | auto_increment_offset = 2 15 | auto_increment_increment = 2 16 | log-slave-updates = 1 17 | 18 | skip-name-resolve 19 | 20 | gtid_mode = ON 21 | enforce-gtid-consistency 22 | -------------------------------------------------------------------------------- /conf/slave1.cnf: -------------------------------------------------------------------------------- 1 | [mysqld] 2 | server-id = 101 3 | bind-address = slave_1 4 | log-error = /var/lib/mysql/mysql.err 5 | log-bin = /var/lib/mysql/mysql-bin 6 | relay-log = /var/lib/mysql/mysql-relay-bin 7 | relay-log-index = /var/lib/mysql/mysql-relay-bin.index 8 | relay-log-info-file = /var/lib/mysql/mysql-relay-log.info 9 | master-info-file = /var/lib/mysql/mysql-master.info 10 | 11 | gtid_mode = ON 12 | enforce-gtid-consistency 13 | skip-name-resolve 14 | -------------------------------------------------------------------------------- /conf/slave2.cnf: -------------------------------------------------------------------------------- 1 | [mysqld] 2 | server-id = 201 3 | bind-address = slave_2 4 | log-error = /var/lib/mysql/mysql.err 5 | log-bin = /var/lib/mysql/mysql-bin 6 | relay-log = /var/lib/mysql/mysql-relay-bin 7 | relay-log-index = /var/lib/mysql/mysql-relay-bin.index 8 | relay-log-info-file = /var/lib/mysql/mysql-relay-log.info 9 | master-info-file = /var/lib/mysql/mysql-master.info 10 | 11 | gtid_mode = ON 12 | enforce-gtid-consistency 13 | skip-name-resolve 14 | -------------------------------------------------------------------------------- /data/master1/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /data/master2/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /data/slave1/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /data/slave2/.gitignore: -------------------------------------------------------------------------------- 1 | * 2 | !.gitignore 3 | -------------------------------------------------------------------------------- /docker/common-services.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | services: 3 | common_master_1: 4 | image: mysql:5.7.27 5 | container_name: ${MASTER_1_HOSTNAME} 6 | hostname: ${MASTER_1_HOSTNAME} 7 | volumes: 8 | - ../conf/master1.cnf:/etc/mysql/conf.d/my.cnf:ro 9 | - ../data/master1:/var/lib/mysql:rw 10 | - /usr/share/zoneinfo/:/usr/share/zoneinfo/:ro 11 | environment: 12 | MYSQL_ROOT_PASSWORD: ${MASTER_1_ROOT_PASSWORD} 13 | networks: 14 | replication_network: 15 | ipv4_address: ${MASTER_1_HOST_IP} 16 | common_slave_1: 17 | image: mysql:5.7.27 18 | container_name: ${SLAVE_1_HOSTNAME} 19 | hostname: ${SLAVE_1_HOSTNAME} 20 | volumes: 21 | - ../conf/slave1.cnf:/etc/mysql/conf.d/my.cnf:ro 22 | - ../data/slave1:/var/lib/mysql:rw 23 | - /usr/share/zoneinfo/:/usr/share/zoneinfo/:ro 24 | environment: 25 | MYSQL_ROOT_PASSWORD: ${SLAVE_1_ROOT_PASSWORD} 26 | networks: 27 | replication_network: 28 | ipv4_address: ${SLAVE_1_HOST_IP} 29 | common_master_2: 30 | container_name: ${MASTER_2_HOSTNAME} 31 | image: mysql:5.7.27 32 | hostname: ${MASTER_2_HOSTNAME} 33 | volumes: 34 | - ../conf/master2.cnf:/etc/mysql/conf.d/my.cnf:ro 35 | - ../data/master2:/var/lib/mysql:rw 36 | - /usr/share/zoneinfo/:/usr/share/zoneinfo/:ro 37 | environment: 38 | MYSQL_ROOT_PASSWORD: ${MASTER_2_ROOT_PASSWORD} 39 | networks: 40 | replication_network: 41 | ipv4_address: ${MASTER_2_HOST_IP} 42 | common_slave_2: 43 | image: mysql:5.7.27 44 | container_name: ${SLAVE_2_HOSTNAME} 45 | hostname: ${SLAVE_2_HOSTNAME} 46 | volumes: 47 | - ../conf/slave2.cnf:/etc/mysql/conf.d/my.cnf:ro 48 | - ../data/slave2:/var/lib/mysql:rw 49 | - /usr/share/zoneinfo/:/usr/share/zoneinfo/:ro 50 | environment: 51 | MYSQL_ROOT_PASSWORD: ${SLAVE_2_ROOT_PASSWORD} 52 | networks: 53 | replication_network: 54 | ipv4_address: ${SLAVE_2_HOST_IP} 55 | replication: 56 | image: ubuntu:18.04 57 | volumes: 58 | - ../scripts:${REPL_SCRIPT_MOUNT_PATH} 59 | networks: 60 | - replication_network 61 | environment: 62 | MASTER_1_HOSTNAME: ${MASTER_1_HOSTNAME} 63 | MASTER_1_HOST_IP: ${MASTER_1_HOST_IP} 64 | MASTER_1_ROOT_USER: ${MASTER_1_ROOT_USER} 65 | MASTER_1_ROOT_PASSWORD: ${MASTER_1_ROOT_PASSWORD} 66 | 67 | SLAVE_1_HOSTNAME: ${SLAVE_1_HOSTNAME} 68 | SLAVE_1_HOST_IP: ${SLAVE_1_HOST_IP} 69 | SLAVE_1_ROOT_USER: ${SLAVE_1_ROOT_USER} 70 | SLAVE_1_ROOT_PASSWORD: ${SLAVE_1_ROOT_PASSWORD} 71 | 72 | MASTER_2_HOSTNAME: ${MASTER_2_HOSTNAME} 73 | MASTER_2_HOST_IP: ${MASTER_2_HOST_IP} 74 | MASTER_2_ROOT_USER: ${MASTER_2_ROOT_USER} 75 | MASTER_2_ROOT_PASSWORD: ${MASTER_2_ROOT_PASSWORD} 76 | 77 | SLAVE_2_HOSTNAME: ${SLAVE_2_HOSTNAME} 78 | SLAVE_2_HOST_IP: ${SLAVE_2_HOST_IP} 79 | SLAVE_2_ROOT_USER: ${SLAVE_2_ROOT_USER} 80 | SLAVE_2_ROOT_PASSWORD: ${SLAVE_2_ROOT_PASSWORD} 81 | 82 | REPL_USER: ${REPL_USER} 83 | REPL_PASSWORD: ${REPL_PASSWORD} 84 | command: bash ${REPL_SCRIPT_MOUNT_PATH}/slave-2-master-2-master-2-slave.sh 85 | pma: 86 | image: phpmyadmin/phpmyadmin 87 | container_name: phpmyadmin 88 | hostname: phpmyadmin 89 | environment: 90 | - PMA_ARBITRARY=1 91 | restart: always 92 | ports: 93 | - 8080:80 94 | volumes: 95 | - /sessions 96 | networks: 97 | - replication_network 98 | -------------------------------------------------------------------------------- /docker/master-2-master.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | services: 3 | master_1: 4 | extends: 5 | file: common-services.yml 6 | service: common_master_1 7 | master_2: 8 | extends: 9 | file: common-services.yml 10 | service: common_master_2 11 | replication: 12 | extends: 13 | file: common-services.yml 14 | service: replication 15 | depends_on: 16 | - master_1 17 | - master_2 18 | command: bash ${REPL_SCRIPT_MOUNT_PATH}/master-2-master.sh 19 | pma: 20 | extends: 21 | file: common-services.yml 22 | service: pma 23 | depends_on: 24 | - master_1 25 | - master_2 26 | - replication 27 | networks: 28 | replication_network: 29 | ipam: 30 | config: 31 | - subnet: 10.208.0.0/16 32 | -------------------------------------------------------------------------------- /docker/master-2-slave.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | services: 3 | master_1: 4 | extends: 5 | file: common-services.yml 6 | service: common_master_1 7 | slave_1: 8 | extends: 9 | file: common-services.yml 10 | service: common_slave_1 11 | depends_on: 12 | - master_1 13 | replication: 14 | extends: 15 | file: common-services.yml 16 | service: replication 17 | depends_on: 18 | - master_1 19 | - slave_1 20 | command: bash ${REPL_SCRIPT_MOUNT_PATH}/master-2-slave.sh 21 | pma: 22 | extends: 23 | file: common-services.yml 24 | service: pma 25 | depends_on: 26 | - master_1 27 | - slave_1 28 | - replication 29 | networks: 30 | replication_network: 31 | ipam: 32 | config: 33 | - subnet: 10.208.0.0/16 34 | -------------------------------------------------------------------------------- /docker/slave-2-master-2-master-2-slave.yml: -------------------------------------------------------------------------------- 1 | version: "2" 2 | services: 3 | master_1: 4 | extends: 5 | file: common-services.yml 6 | service: common_master_1 7 | slave_1: 8 | extends: 9 | file: common-services.yml 10 | service: common_slave_1 11 | depends_on: 12 | - master_1 13 | master_2: 14 | extends: 15 | file: common-services.yml 16 | service: common_master_2 17 | slave_2: 18 | extends: 19 | file: common-services.yml 20 | service: common_slave_2 21 | depends_on: 22 | - master_2 23 | replication: 24 | extends: 25 | file: common-services.yml 26 | service: replication 27 | depends_on: 28 | - master_1 29 | - slave_1 30 | - master_2 31 | - slave_2 32 | command: bash ${REPL_SCRIPT_MOUNT_PATH}/slave-2-master-2-master-2-slave.sh 33 | pma: 34 | extends: 35 | file: common-services.yml 36 | service: pma 37 | depends_on: 38 | - master_1 39 | - slave_1 40 | - master_2 41 | - slave_2 42 | - replication 43 | networks: 44 | replication_network: 45 | ipam: 46 | config: 47 | - subnet: 10.208.0.0/16 48 | -------------------------------------------------------------------------------- /info: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | set -x 4 | 5 | source check_params.sh 6 | 7 | check_docker_config 8 | 9 | docker-compose -f $file ps 10 | -------------------------------------------------------------------------------- /scripts/common-functions.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | function create_slave_replication_user_on_master() { 4 | MASTER_HOST_IP=$1 5 | MASTER_ROOT_USER=$2 6 | MASTER_ROOT_PASSWORD=$3 7 | 8 | SLAVE_HOST_IP=$4 9 | REPL_USER=$5 10 | REPL_PASSWORD=$6 11 | 12 | MYSQL_QUERY="CREATE USER '$REPL_USER'@'$SLAVE_HOST_IP' IDENTIFIED BY '$REPL_PASSWORD';" 13 | mysql --user="$MASTER_ROOT_USER" --password="$MASTER_ROOT_PASSWORD" --host="$MASTER_HOST_IP" -e "$MYSQL_QUERY" 14 | } 15 | 16 | 17 | function grant_replication_slave_permission_on_master() { 18 | MASTER_HOST_IP=$1 19 | MASTER_ROOT_USER=$2 20 | MASTER_ROOT_PASSWORD=$3 21 | REPL_USER=$4 22 | SLAVE_HOST_IP=$5 23 | 24 | MYSQL_QUERY="GRANT REPLICATION SLAVE ON *.* TO \"$REPL_USER\"@\"$SLAVE_HOST_IP\";" 25 | mysql --user="$MASTER_ROOT_USER" --password="$MASTER_ROOT_PASSWORD" --host="$MASTER_HOST_IP" -AN -e "$MYSQL_QUERY" 26 | } 27 | 28 | # Reload privileges for replication to take effect 29 | function flush_privileges_for_replication_to_take_effect() { 30 | MASTER_HOST_IP=$1 31 | MASTER_ROOT_USER=$2 32 | MASTER_ROOT_PASSWORD=$3 33 | 34 | MYSQL_QUERY="FLUSH PRIVILEGES;" 35 | mysql --user="$MASTER_ROOT_USER" --password="$MASTER_ROOT_PASSWORD" --host="$MASTER_HOST_IP" -e "$MYSQL_QUERY" 36 | } 37 | 38 | function get_master_replication_file_and_position_and_update_slave () { 39 | MASTER_HOST_IP=$1 40 | MASTER_ROOT_USER=$2 41 | MASTER_ROOT_PASSWORD=$3 42 | 43 | SLAVE_HOST_IP=$4 44 | SLAVE_ROOT_USER=$5 45 | SLAVE_ROOT_PASSWORD=$6 46 | 47 | REPL_USER=$7 48 | REPL_PASSWORD=$8 49 | 50 | REPL_LOG_FILE=$(eval "mysql --user=$MASTER_ROOT_USER --password=$MASTER_ROOT_PASSWORD --host=$MASTER_HOST_IP -e 'SHOW MASTER STATUS\G' | grep File | sed -n -e 's/^.*: //p'") 51 | REPL_LOG_POSITION=$(eval "mysql --user=$MASTER_ROOT_USER --password=$MASTER_ROOT_PASSWORD --host=$MASTER_HOST_IP -e 'SHOW MASTER STATUS\G' | grep Position | sed -n -e 's/^.*: //p'") 52 | 53 | MYSQL_QUERY="CHANGE MASTER TO MASTER_HOST = '$MASTER_HOST_IP', MASTER_USER = '$REPL_USER', MASTER_PASSWORD = '$REPL_PASSWORD', MASTER_LOG_FILE = '$REPL_LOG_FILE', MASTER_LOG_POS = $REPL_LOG_POSITION;" 54 | mysql --user="$SLAVE_ROOT_USER" --password="$SLAVE_ROOT_PASSWORD" --host="$SLAVE_HOST_IP" -e "$MYSQL_QUERY" 55 | } 56 | 57 | function start_slave () { 58 | SLAVE_HOST_IP=$1 59 | SLAVE_ROOT_USER=$2 60 | SLAVE_ROOT_PASSWORD=$3 61 | 62 | MYSQL_QUERY="START SLAVE;" 63 | mysql --user="$SLAVE_ROOT_USER" --password="$SLAVE_ROOT_PASSWORD" --host="$SLAVE_HOST_IP" -e "$MYSQL_QUERY" 64 | } 65 | 66 | function check_slave_status() { 67 | SLAVE_HOST_IP=$1 68 | SLAVE_ROOT_USER=$2 69 | SLAVE_ROOT_PASSWORD=$3 70 | 71 | MYSQL_QUERY="SHOW SLAVE STATUS \G;" 72 | mysql --user="$SLAVE_ROOT_USER" --password="$SLAVE_ROOT_PASSWORD" --host="$SLAVE_HOST_IP" -e "$MYSQL_QUERY" 73 | } 74 | 75 | 76 | function setup_master_2_slave_replication() { 77 | MASTER_HOST_IP=$1 78 | MASTER_ROOT_USER=$2 79 | MASTER_ROOT_PASSWORD=$3 80 | 81 | SLAVE_HOST_IP=$4 82 | SLAVE_ROOT_USER=$5 83 | SLAVE_ROOT_PASSWORD=$6 84 | 85 | REPL_USER=$7 86 | REPL_PASSWORD=$8 87 | 88 | sleep 1 89 | create_slave_replication_user_on_master $MASTER_HOST_IP $MASTER_ROOT_USER $MASTER_ROOT_PASSWORD $SLAVE_HOST_IP $REPL_USER $REPL_PASSWORD 90 | 91 | sleep 1 92 | grant_replication_slave_permission_on_master $MASTER_HOST_IP $MASTER_ROOT_USER $MASTER_ROOT_PASSWORD $REPL_USER $SLAVE_HOST_IP 93 | 94 | sleep 1 95 | flush_privileges_for_replication_to_take_effect $MASTER_HOST_IP $MASTER_ROOT_USER $MASTER_ROOT_PASSWORD 96 | 97 | sleep 1 98 | get_master_replication_file_and_position_and_update_slave $MASTER_HOST_IP $MASTER_ROOT_USER $MASTER_ROOT_PASSWORD $SLAVE_HOST_IP $SLAVE_ROOT_USER $SLAVE_ROOT_PASSWORD $REPL_USER $REPL_PASSWORD 99 | 100 | sleep 1 101 | start_slave $SLAVE_HOST_IP $SLAVE_ROOT_USER $SLAVE_ROOT_PASSWORD 102 | 103 | sleep 1 104 | check_slave_status $SLAVE_HOST_IP $SLAVE_ROOT_USER $SLAVE_ROOT_PASSWORD 105 | } -------------------------------------------------------------------------------- /scripts/master-2-master.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | set -x 4 | 5 | ####################################################################################################################### 6 | # Install the mysql client to connect to mysql nodes 7 | ####################################################################################################################### 8 | apt-get -qq update 9 | apt-get -qq install mysql-client -y --no-install-recommends 10 | 11 | 12 | ####################################################################################################################### 13 | # Import common functions 14 | ####################################################################################################################### 15 | BASEDIR=$(dirname "$0") 16 | source $BASEDIR/common-functions.sh 17 | 18 | 19 | ####################################################################################################################### 20 | # Setup replication from master1 to master2 21 | ####################################################################################################################### 22 | setup_master_2_slave_replication $MASTER_1_HOST_IP $MASTER_1_ROOT_USER $MASTER_1_ROOT_PASSWORD $MASTER_2_HOST_IP $MASTER_2_ROOT_USER $MASTER_2_ROOT_PASSWORD $REPL_USER $REPL_PASSWORD 23 | 24 | # Add some gap intentionally 25 | sleep 5 26 | 27 | ####################################################################################################################### 28 | # Setup replication from master2 to master1 29 | ####################################################################################################################### 30 | setup_master_2_slave_replication $MASTER_2_HOST_IP $MASTER_2_ROOT_USER $MASTER_2_ROOT_PASSWORD $MASTER_1_HOST_IP $MASTER_1_ROOT_USER $MASTER_1_ROOT_PASSWORD $REPL_USER $REPL_PASSWORD -------------------------------------------------------------------------------- /scripts/master-2-slave.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | set -x 4 | 5 | ####################################################################################################################### 6 | # Install the mysql client to connect to mysql nodes 7 | ####################################################################################################################### 8 | apt-get -qq update 9 | apt-get -qq install mysql-client -y --no-install-recommends 10 | 11 | 12 | ####################################################################################################################### 13 | # Import common functions 14 | ####################################################################################################################### 15 | BASEDIR=$(dirname "$0") 16 | source $BASEDIR/common-functions.sh 17 | 18 | 19 | ####################################################################################################################### 20 | # Setup replication from master1 to slave1 21 | ####################################################################################################################### 22 | setup_master_2_slave_replication $MASTER_1_HOST_IP $MASTER_1_ROOT_USER $MASTER_1_ROOT_PASSWORD $SLAVE_1_HOST_IP $SLAVE_1_ROOT_USER $SLAVE_1_ROOT_PASSWORD $REPL_USER $REPL_PASSWORD -------------------------------------------------------------------------------- /scripts/no-repl-selected.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | echo 4 | echo "Looks like you did not select the correct MySQL replication scheme" 5 | -------------------------------------------------------------------------------- /scripts/slave-2-master-2-master-2-slave.sh: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | set -x 4 | 5 | ####################################################################################################################### 6 | # Install the mysql client to connect to mysql nodes 7 | ####################################################################################################################### 8 | apt-get -qq update 9 | apt-get -qq install mysql-client -y --no-install-recommends 10 | 11 | 12 | ####################################################################################################################### 13 | # Import common functions 14 | ####################################################################################################################### 15 | BASEDIR=$(dirname "$0") 16 | source $BASEDIR/common-functions.sh 17 | 18 | 19 | ####################################################################################################################### 20 | # Setup replication from master1 to slave1 21 | ####################################################################################################################### 22 | setup_master_2_slave_replication $MASTER_1_HOST_IP $MASTER_1_ROOT_USER $MASTER_1_ROOT_PASSWORD $SLAVE_1_HOST_IP $SLAVE_1_ROOT_USER $SLAVE_1_ROOT_PASSWORD $REPL_USER $REPL_PASSWORD 23 | 24 | 25 | ####################################################################################################################### 26 | # Setup replication from master2 to slave2 27 | ####################################################################################################################### 28 | setup_master_2_slave_replication $MASTER_2_HOST_IP $MASTER_2_ROOT_USER $MASTER_2_ROOT_PASSWORD $SLAVE_2_HOST_IP $SLAVE_2_ROOT_USER $SLAVE_2_ROOT_PASSWORD $REPL_USER $REPL_PASSWORD 29 | 30 | 31 | ####################################################################################################################### 32 | # Setup replication from master1 to master2 33 | ####################################################################################################################### 34 | setup_master_2_slave_replication $MASTER_1_HOST_IP $MASTER_1_ROOT_USER $MASTER_1_ROOT_PASSWORD $MASTER_2_HOST_IP $MASTER_2_ROOT_USER $MASTER_2_ROOT_PASSWORD $REPL_USER $REPL_PASSWORD 35 | 36 | 37 | ####################################################################################################################### 38 | # Setup replication from master2 to master1 39 | ####################################################################################################################### 40 | setup_master_2_slave_replication $MASTER_2_HOST_IP $MASTER_2_ROOT_USER $MASTER_2_ROOT_PASSWORD $MASTER_1_HOST_IP $MASTER_1_ROOT_USER $MASTER_1_ROOT_PASSWORD $REPL_USER $REPL_PASSWORD -------------------------------------------------------------------------------- /start: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | set -x 4 | 5 | source check_params.sh 6 | 7 | check_docker_config 8 | 9 | if [ $file_found -eq 1 ]; 10 | then 11 | echo Using docker configuration file : $file 12 | echo 13 | docker-compose -f $file up $2 14 | fi 15 | -------------------------------------------------------------------------------- /stop: -------------------------------------------------------------------------------- 1 | #! /bin/bash 2 | 3 | set -x 4 | 5 | source check_params.sh 6 | 7 | check_docker_config 8 | 9 | if [ $file_found -eq 1 ]; 10 | then 11 | echo "Using configuration file $file" 12 | echo 13 | docker-compose -f $file down 14 | fi 15 | 16 | if [ "$2" == "clean" ]; 17 | then 18 | for node in "master" "slave" 19 | do 20 | for num in 1 2 21 | do 22 | sudo rm -rf data/$node$num/* 23 | done 24 | done 25 | fi 26 | --------------------------------------------------------------------------------