其他分享
首页 > 其他分享> > MHA主库切换

MHA主库切换

作者:互联网

1.MHA主库切换机制

1.读取配置中的指定优先级
	candidate_master=1
	check_repl_delay=0
2.如果数据量不同,数据量多的为主库
3.如果数据量相同,按照主机标签,值越小优先级越高 # 不问顺序

2.主机标签优先级测试

#配置MHA
[root@db03 ~]# vim /service/mha/app1.cnf
......
[serverc]
hostname=172.16.1.53
port=3306

[serverb]
hostname=172.16.1.52
port=3306

[serverd]
hostname=172.16.1.51
port=3306
......

#停掉主库,查看切换
mysql> show slave status\G
                  Master_Host: 172.16.1.52
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

3.指定优先级测试

#配置优先级
[root@db03 ~]# vim /service/mha/app1.cnf 
......
[server3]
candidate_master=1
check_repl_delay=0
hostname=172.16.1.53
port=3306
......

#停掉主库,查看切换
mysql> show slave status\G
                  Master_Host: 172.16.1.53
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

4.数据量不同时切换的优先级

1.主库建库

mysql> use test
Database changed

mysql> create table linux9(id int not null primary key auto_increment,name varchar(10));
Query OK, 0 rows affected (0.31 sec)

2.脚本循环插入数据

[root@db03 ~]# vim insert.sh 
#/bin/bash
while true;do
    mysql -e "use test;insert into linux9(name) values('lhd')"
    sleep 1
done

[root@db03 ~]# sh insert.sh 

3.停掉一台机器的IO线程

#此时db03是主库,停掉db01的IO线程
mysql> stop slave io_thread;

4.停掉db03的主库

[root@db03 ~]# systemctl stop mysqld

5.查看主库切换

mysql> show slave status\G
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.16.1.52
                  Master_User: rep
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000008
          Read_Master_Log_Pos: 67437
               Relay_Log_File: db01-relay-bin.000002
                Relay_Log_Pos: 14904
        Relay_Master_Log_File: mysql-bin.000008
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

标签:主库,Slave,mysql,MHA,Master,切换,172.16,db03
来源: https://www.cnblogs.com/Applogize/p/13393815.html