zoukankan      html  css  js  c++  java
  • mysql主从(主备)同步一键配置,配自动检测功能

    主从一键shell配置

    做个笔记。

    #!/bin/bash
    #Mysql sync
    #chenglee
    #master机器ip
    MasterIP="192.168.137.174"
    #slave机器ip
    SlaveIp="192.168.137.170"
    #主从同步账号密码
    MASTER_USER="sync"
    MASTER_PASSWORD="sync"
    #master机mysql登录账号密码
    Muser="root"
    Mpass="123456"
    #slave机mysql登录账号密码
    Suser="root"
    Spass="123456"
    #slave机ssh的登录账号密码
    SSHuser="root"
    SSHpass="lizhenghua"
    
    function Master(){
        #MASTER
        function create(){
            mysql --protocol=TCP -u ${Muser} -p''${Mpass}'' --connect-expired-password -e "flush PRIVILEGES;" >/dev/null 2>&1
            mysql --protocol=TCP -u ${Muser} -p''${Mpass}'' --connect-expired-password -e "CREATE USER '${MASTER_USER}'@'${SlaveIp}' IDENTIFIED BY '${MASTER_PASSWORD}';" >/dev/null 2>&1
            mysql --protocol=TCP -u ${Muser} -p''${Mpass}'' --connect-expired-password -e "grant replication slave on *.* to '${MASTER_USER}'@'${SlaveIp}' identified by '${MASTER_PASSWORD}';" >/dev/null 2>&1
            mysql --protocol=TCP -u ${Muser} -p''${Mpass}'' --connect-expired-password -e "flush PRIVILEGES;" >/dev/null 2>&1
        }
        #my.cnf
        function addbin(){
            number=`cat -n /etc/my.cnf | grep -F "[mysqld]" | awk '{print$1}'`
            serverID=`echo ${MasterIP} | awk -F'.' '{print$NF}'`
            text1="server-id=${serverID}"
            text2="log-bin=mysql-bin"
            text3="lower_case_table_names=1"
            sed -i "${number}a\${text1}
    ${text2}
    ${text3}" /etc/my.cnf
        }
        #restart
        function Mrestart(){
            service mysql restart 
        }
        #ssh
        function mstscSSH(){
            echo -e "33[43;35m 开始配置双机交互,请按回车后输入slave机登录密码 33[0m"
            ssh-keygen -N ''
            ssh-copy-id ${SSHuser}@${SlaveIp}
        }
    echo "创建同步账号"
    create
    echo "添加master端id到my.cnf文件"
    addbin
    echo "重启master端数据库"
    Mrestart
    echo "开始账号交互"
    mstscSSH
    }
    #SLAVE
    function Slave(){
        #my.cnf
        function addbin2(){
            Snumber2=`ssh ${SSHuser}@${SlaveIp} "cat -n /etc/my.cnf | grep -F "[mysqld]" | awk '{print$1}'" | awk '{print$1}'`
            SserverID2=`echo ${SlaveIp} | awk -F'.' '{print$NF}'`
            Stext1="server-id=${SserverID2}"
            Stext3="lower_case_table_names=1"
            ssh ${SSHuser}@${SlaveIp} "sed -i '${Snumber2}a\${Stext1}
    ${Stext3}' /etc/my.cnf"
        }
        #restart
        function Srestart(){
            ssh ${SSHuser}@${SlaveIp} "service mysql restart"
        }
        function Ssync(){
            mkdir logs
            mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "show master status;" > logs/chenglee.logs
            MASTER_HOST=${MasterIP}
            MASTER_LOG_FILE=`cat logs/chenglee.logs | tail -n 1 | awk '{print$1}'`
            MASTER_LOG_POS=`cat logs/chenglee.logs | tail -n 1 | awk '{print$2}'`
            ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "stop slave;"" >/dev/null 2>&1
            ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "reset slave;"" >/dev/null 2>&1
            ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "reset slave all;"" >/dev/null 2>&1
            ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "change master to master_host='${MasterIP}', master_user='${MASTER_USER}', master_password='${MASTER_PASSWORD}', master_log_file='${MASTER_LOG_FILE}', master_log_pos=${MASTER_LOG_POS}, MASTER_CONNECT_RETRY=10;"" >/dev/null 2>&1
            ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "start slave;"" >/dev/null 2>&1
            ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "show slave status G;"" > logs/slave_sync.logs
    }
    echo "添加master端id到my.cnf文件"
    addbin2
    echo "重启slave端数据库"
    Srestart
    echo "开始同步配置"
    Ssync
    }
    #校准同步
    #:提示
    #当发现主从同步失败的时候, 开启此选项则自动检测并重新配置同步
    function calibrate(){
        mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "show master status;" > logs/chenglee.logs
        ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "show slave status G;"" > logs/slave_sync.logs
        MASTER_HOST=${MasterIP}
        MASTER_LOG_FILE=`cat logs/chenglee.logs | tail -n 1 | awk '{print$1}'`
        MASTER_LOG_POS=`cat logs/chenglee.logs | tail -n 1 | awk '{print$2}'`
        string1=`cat logs/slave_sync.logs | sed -n '12,13p' | awk -F ':' '{print$2}' | head -n 1`
        string2=`cat logs/slave_sync.logs | sed -n '12,13p' | awk -F ':' '{print$2}' | head -n 2 | tail -n 1`
        if [ ${string1} = ${string2} ];then
            if ( echo ${string1} |grep -q 'Yes' && echo ${string2} |grep -q 'Yes' );then
                echo "主从已同步,无需校准"
            else
                echo "1主从已断开,准备校准"
                ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "stop slave;""
                ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "reset slave;""
                ssh ${SSHuser}@${SlaveIp} "service mysql restart"
                ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "change master to master_host='${MasterIP}', master_user='${MASTER_USER}', master_password='${MASTER_PASSWORD}', master_log_file='${MASTER_LOG_FILE}', master_log_pos=${MASTER_LOG_POS}""
                ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "start slave;""
                echo "1校准成功"
            fi
            exit
        else
            echo "2主从已断开,准备校准"
            ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "stop slave;""
            ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "reset slave;""
            ssh ${SSHuser}@${SlaveIp} "service mysql restart"
            ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "change master to master_host='${MasterIP}', master_user='${MASTER_USER}', master_password='${MASTER_PASSWORD}', master_log_file='${MASTER_LOG_FILE}', master_log_pos=${MASTER_LOG_POS}""
            ssh ${SSHuser}@${SlaveIp} "mysql --protocol=TCP -u ${Suser} -p''${Spass} --connect-expired-password -e "start slave;""
            echo "2校准成功" 
        fi
    }
    
    function main(){
        Master
        Slave
        #calibrate
    }
    main
    

      

  • 相关阅读:
    如何设置IIS程序池的回收时间,才能最大程度的减少对用户的影响?
    C#实现执行数据库事务案例
    RGB色彩对照表
    C# list.ForEach用法
    C# 实现list=list.OrderBy(q=>q.字段名).ToList(); 按多个字段排序
    IIS 7如何实现http重定向https
    Windows 2008 R2上配置IIS7或IIS7.5中的URLRewrite(URL重写)实例
    MVC网站发布到 IIS
    完美解决IE(IE6/IE7/IE8)不兼容HTML5标签的方法
    js返回上一页的实现方法
  • 原文地址:https://www.cnblogs.com/chenglee/p/10249049.html
Copyright © 2011-2022 走看看