zoukankan      html  css  js  c++  java
  • mysql主从架构主从正常切换,主库宕机切换。

     

     

     

    MySQL主从切换手册

    Master-Slave架构

    运维部

    V1.0

     

     

     

     

     

     

     

     

     

    2016年      05月    24   日

     

     

     

    正常切换

    1. 检查slave同步状态

    在开始切换之前先对主库进行锁表:

    flush tables with read lock

    (在执行完成后生产环境必须等待所有语句执行完成)

    在flush tables with read lock成功获得锁之前,必须等待所有语句执行完成(包括SELECT)。所以如果有个慢查询在执行,或者一个打开的事务,或者其他进程拿着表锁,flush tables with read lock就会被阻塞,直到所有的锁被释放。请看下面的例子:

    复制代码
    mysql> show processlist;
    +----+------+-----------+------+------------+------+-------------------+----------------------------------------------------------------------+
    | Id | User | Host | db | Command | Time | State | Info |
    +----+------+-----------+------+------------+------+-------------------+----------------------------------------------------------------------+
    | 4 | root | localhost | test | Query | 80 | Sending data | select count(*) from t t1 join t t2 join t t3 join t t4 where t1.b=0 |
    | 5 | root | localhost | test | Query | 62 | Flushing tables | flush tables with read lock |
    | 6 | root | localhost | test | Field List | 35 | Waiting for table | |
    | 7 | root | localhost | test | Query | 0 | NULL | show processlist |
    +----+------+-----------+------+------------+------+-------------------+----------------------------------------------------------------------+
    4 rows in set (0.00 sec)

    flush data

    切换完成后可以释放锁

    unlock tables

    1)在master执行:show processlist;

    显示Master has sent all binlog to slave; waiting for binlog to be updated

     

    2)在slave执行:show processlist;

    显示Slave has read all relay log; waiting for the slave I/O thread to update it

    mysql> show slave status \G;

     

    检查IO及SQL线程是否正常,如果为NO表明同步不一致,需要重新将slave同步保持主从数据一致。

    3)停止slave io线程

    在slave执行:mysql> STOP SLAVE IO_THREAD

    mysql> SHOW PROCESSLIST;

    确保状态为:has read all relay log

     

    以上都执行完成后可以把slave提升为master:

    4)提升slave为master

    Stop slave;

    Reset master;

    Reset slave all; 在5.6.3版本之后

    Reset slave; 在5.6.3版本之前

    查看slave是否只读模式:show variables like 'read_only';

    只读模式需要修改my.cnf文件,注释read-only=1并重启mysql服务。

    或者不重启使用命令关闭只读,但下次重启后失效:set global read_only=off;

    mysql> show master status \G;

    备注:reset slave all 命令会删除从库的 replication 参数,之后 show slave status\G 的信息返回为空。

    5)将原来master变为slave

    在新的master上创建同步用户:

    grant replication slave on *.* repl@'IP of slave' identified by 'replpwd';

    在新的slave上重置binlog:

    Reset master;

    change master to master_host='192.168.0.104', //Master 服务器Ip

    master_port=3306,

    master_user='repl',

    master_password=’replpwd’,

    master_log_file='master-bin.000001',//Master服务器产生的日志

    master_log_pos=?;//master binlog pos

    以上最后两步可以在master执行:show master status

     

    启动slave:start slave; 并查看slave状态:show slave status\G;

               异常切换

    主机故障或者宕机:

    1)   在salve执行:

    stop slave;

    reset master;

    查看是否只读模式:show variables like 'read_only';

    只读模式需要修改my.cnf文件,注释read-only=1并重启mysql服务。

    或者不重启使用命令关闭只读,但下次重启后失效:set global read_only=off;

    查看show slave status \G;

    查看show master status \G;

    将从库IP地址改为主库IP地址,测试应用连接是否正常。

     

     

    <完>

     

  • 相关阅读:
    tyvj 1031 热浪 最短路
    【bzoj2005】 [Noi2010]能量采集 数学结论(gcd)
    hdu 1394 Minimum Inversion Number 逆序数/树状数组
    HDU 1698 just a hook 线段树,区间定值,求和
    ZeptoLab Code Rush 2015 C. Om Nom and Candies 暴力
    ZeptoLab Code Rush 2015 B. Om Nom and Dark Park DFS
    ZeptoLab Code Rush 2015 A. King of Thieves 暴力
    hdoj 5199 Gunner map
    hdoj 5198 Strange Class 水题
    vijos 1659 河蟹王国 线段树区间加、区间查询最大值
  • 原文地址:https://www.cnblogs.com/nbuntu/p/5523331.html
Copyright © 2011-2022 走看看