MySQL主从、主主互备

发布时间:2022-10-05 21:00

为什么80%的码农都做不了架构师?>>>   hot3.png

Docker部署MySQL主/从复制集群搭建

一共三个文件,按需求修改配置;保存文件。

vim Dockerfile

FROM mysql:5.7
COPY replica.sh /docker-entrypoint-initdb.d/

##############################################################################################
vim docker-compose.yml
# Master密码:master_passw0rd  Slave密码:slave_passw0rd ,同步账户replica_Passw0rd 可以修改。
# 端口建议修改。

version: '2'
services:
    master:
        container_name: master
        image: mysql:5.7-replica
        build:
            context: .
            dockerfile: Dockerfile
        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:
        container_name: slave
        image: mysql:5.7-replica
        build:
            context: .
            dockerfile: Dockerfile
        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"]

##############################################################################################
vim replica.sh
# 如果docker-compose.yml文件改了端口,这里注意也要修改端口。

#!/bin/bash

# Most of the code are copied from ioggstream's PR, docker-library/mysql#43:
# 
#
# Supported environment variables for this replica.sh:
#  - 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

#
# A replication user (actually created on both master and slaves)
#
if [ "$MYSQL_REPLICA_USER" ]; then
        if [ -z "$MYSQL_REPLICA_PASS" ]; then
                echo >&2 'error: MYSQL_REPLICA_USER set, but MYSQL_REPLICA_PASS not set'
                exit 1
        fi
        echo "CREATE USER '$MYSQL_REPLICA_USER'@'%' IDENTIFIED BY '$MYSQL_REPLICA_PASS'; " | "${mysql[@]}"
        echo "GRANT REPLICATION SLAVE ON *.* TO '$MYSQL_REPLICA_USER'@'%'; " | "${mysql[@]}"
        # REPLICATION CLIENT privileges are required to get master position
        echo "GRANT REPLICATION CLIENT ON *.* TO '$MYSQL_REPLICA_USER'@'%'; " | "${mysql[@]}"
fi

#
# On the slave: point to a master server
#
if [ "$MYSQL_MASTER_SERVER" ]; then
    MYSQL_MASTER_PORT=${MYSQL_MASTER_PORT:-3306}
    MYSQL_MASTER_WAIT_TIME=${MYSQL_MASTER_WAIT_TIME:-3}

    if [ -z "$MYSQL_REPLICA_USER" ]; then
            echo >&2 'error: MYSQL_REPLICA_USER not set'
            exit 1
    fi
    if [ -z "$MYSQL_REPLICA_PASS" ]; then
            echo >&2 'error: MYSQL_REPLICA_PASS not set'
            exit 1
    fi

    # Wait for eg. 10 seconds for the master to come up
    # do at least one iteration
    for i in $(seq $((MYSQL_MASTER_WAIT_TIME + 1))); do
        if ! mysql "-u$MYSQL_REPLICA_USER" "-p$MYSQL_REPLICA_PASS" "-h$MYSQL_MASTER_SERVER" -e 'select 1;' |grep -q 1; then
            echo >&2 "Waiting for $MYSQL_REPLICA_USER@$MYSQL_MASTER_SERVER"
            sleep 1
        else
            break
        fi
    done

    if [ "$i" -gt "$MYSQL_MASTER_WAIT_TIME" ]; then
        echo 2>&1 "Master is not reachable"
        exit 1
    fi

    # Get master position and set it on the slave. NB: MASTER_PORT and MASTER_LOG_POS must not be quoted
    MasterPosition=$(mysql "-u$MYSQL_REPLICA_USER" "-p$MYSQL_REPLICA_PASS" "-h$MYSQL_MASTER_SERVER" -e "show master status \G" | awk '/Position/ {print $2}')
    MasterFile=$(mysql  "-u$MYSQL_REPLICA_USER" "-p$MYSQL_REPLICA_PASS" "-h$MYSQL_MASTER_SERVER"   -e "show master status \G"     | awk '/File/ {print $2}')
    echo "CHANGE MASTER TO MASTER_HOST='$MYSQL_MASTER_SERVER', MASTER_PORT=$MYSQL_MASTER_PORT, MASTER_USER='$MYSQL_REPLICA_USER', MASTER_PASSWORD='$MYSQL_REPLICA_PASS', MASTER_LOG_FILE='$MasterFile', MASTER_LOG_POS=$MasterPosition;"  | "${mysql[@]}"

    echo "START SLAVE;"  | "${mysql[@]}"
fi

配置文件准备好了,启动容器。

yum install docker-compose -y
docker-compose up -d

以下是在Neil的博客上看到的一篇博文,由于没有试验环境,没有经过测试,以后测试后会更新并删除本段文字,摘录分享,仅供参考。

MySQL Replication

mysql 主主互备

双机热备的概念简单说一下,就是要保持两个数据库的状态自动同步。对任何一个数据库的操作都自动应用到另外一个数据库,始终保持两个数据库数据一致。 这样做的好处多。

  • 1.可以做灾备,其中一个坏了可以切换到另一个。
  • 2.可以做负载均衡,可以将请求分摊到其中任何一台上,提高网站吞吐量。
  • 3.对于异地热备,尤其适合灾备。

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号