数据库.高可用MHA

MHA 简介
Master High Availability
1 MySQL 高可用性环境下故障切换和主从提升的高可用软件。
2 在故障切换过程中 能做到0~30秒之内自动完成数据库的故障切换操作
3 在进行故障切换的过程中,能在最大程度上保证数据的一致性,以达到真正意义上的高可用。

MHA 组成
1 MHA Manager (管理节点)
2 MHA Node (数据节点)

MHA 工作过程
MHA Manager 会定时探测集群中的 master 节点,当 master 出现故障时,它可以自动将最新数据的 slave 提升为新的 master ,然后将所有其他的 slave重新指向新的 master 。整个故障转移过程对应用程序完全透明。
– ( 1 )从宕机崩溃的 master 保存二进制日志事件( binlog events)
– ( 2 )识别含有最新更新的 slave
– ( 3 )应用差异的中继日志( relay log )到其他的 slave
– ( 4 )应用从 master 保存的二进制日志事件( binlog events )
– ( 5 )提升一个 slave 为新的 master ;
– ( 6 )使其他的 slave 连接新的 master 进行复制;

拓扑结构
master51
|
|


| | | | |
slave52 slave53 slave54 slave55 mgm56
Manager

拓扑简介:master51是主服务器 slave52和slave53是备用主服务器 slave54和slave是从服务器 mgm56Manager是管理节点

基本的配置步骤
1 配置 ssh 密钥认证登陆
2 配置 MySQL 一主多从(主从同步)
3 安装软件包
4 配置管理节点
5 启动管理服务
6 测试配置
7 测试故障转移

本案例具体的配置步骤
1 1.1 配置所有数据节点主机之间可以互相以ssh密钥对方式认证登陆
1.2 配置manager56主机 无密码ssh登录 所有数据节点主机
# ssh-key-gen
# ssh-copy-id [email protected]

2 51 主库 开半同步复制
52 从库(备用主库) 开半同步复制
53 从库(备用主库) 开半同步复制
54 从库 不做备用主库所以不用开半同步复制
55 从库 不做备用主库所以不用开半同步复制

2.1 master51配置:
vim /etc/my.cnf
[mysqld]
plugin-load = “rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so”
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1

server_id=51
log-bin=master51
binlog-format="mixed"
:wq

[root@db107 ~]# systemctl  restart mysqld

[root@db107 ~]# ls /var/lib/mysql/master51.*
/var/lib/mysql/master51.000001  /var/lib/mysql/master51.index

[root@db107 ~]# mysql -uroot -p123456
mysql> grant  replication slave  on  *.*  to repluser@"%"  identified by "123456";
Query OK, 0 rows affected, 1 warning (10.04 sec)

mysql> set global relay_log_purge=off;
Query OK, 0 rows affected (0.15 sec)

mysql> show master status;
+-----------------+----------+--------------+------------------+-------------------+
| File            | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+-----------------+----------+--------------+------------------+-------------------+
| master51.000001 |      441 |              |                  |                   |
+-----------------+----------+--------------+------------------+-------------------+
mysql> quit;

2.2 备用master52的配置
vim /etc/my.cnf
[mysqld]
plugin-load = “rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so”
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1

server_id=52
log-bin=master52
binlog-format="mixed"

]# systemctl restart mysqld
[root@db108 ~]# ls /var/lib/mysql/master52.*
/var/lib/mysql/master52.000001  /var/lib/mysql/master52.index

[root@db108 ~]# mysql  -uroot  -p123456

mysql> set global relay_log_purge=off; 
Query OK, 0 rows affected (0.13 sec)

mysql> change master to 
    -> master_host="192.168.4.51",
    -> master_user="repluser",
    -> master_password="123456",
    -> master_log_file="master51.000001",
    -> master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.04 sec)

mysql> start slave;
Query OK, 0 rows affected (0.01 sec)


[root@db108 ~]# mysql -uroot -p123456 -e "show slave status\G"  | grep -i YES
mysql: [Warning] Using a password on the command line interface can be insecure.
         Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
[root@db108 ~]# 

2.3 备用master53的配置
]# vim /etc/my.cnf
[mysqld]
plugin-load = “rpl_semi_sync_master=semisync_master.so;rpl_semi_sync_slave=semisync_slave.so”
rpl-semi-sync-master-enabled = 1
rpl-semi-sync-slave-enabled = 1

server_id=53
log-bin=master53
binlog-format="mixed"
:wq

[root@db109 ~]# systemctl  restart mysqld
[root@db109 ~]# ls /var/lib/mysql/master53.*
/var/lib/mysql/master53.000001  /var/lib/mysql/master53.index
[root@db109 ~]# 

[root@db109 ~]# mysql -uroot -p123456
mysql> set global relay_log_purge=off;
Query OK, 0 rows affected (0.14 sec)

mysql> change master  to master_host="192.168.4.51",master_user="repluser",master_password="123456",master_log_file="master51.000001",master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.05 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> 

[root@db109 ~]# mysql -uroot -p123456 -e "show slave status\G" | grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
         Slave_IO_Running: Yes
         Slave_SQL_Running: Yes
[root@db109 ~]# 

2.4 配置从服务器54
[root@db111 ~]# vim /etc/my.cnf
[mysqld]
server_id=54
:wq

[root@db111 ~]# systemctl  restart mysqld
[root@db111 ~]# mysql -uroot -p123456
mysql> change master  to master_host="192.168.4.51",master_user="repluser",master_password="123456",master_log_file="master51.000001",master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.09 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye
[root@db111 ~]# 
[root@db111 ~]# mysql -uroot -p123456 -e "show slave status\G" | grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
         Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
[root@db111 ~]# 

2.5 配置从服务器55
[root@db111 ~]# vim /etc/my.cnf
[mysqld]
server_id=55
:wq

[root@db111 ~]# systemctl  restart mysqld
[root@db111 ~]# mysql -uroot -p123456
mysql> change master  to master_host="192.168.4.51",master_user="repluser",master_password="123456",master_log_file="master51.000001",master_log_pos=441;
Query OK, 0 rows affected, 2 warnings (0.09 sec)

mysql> start slave;
Query OK, 0 rows affected (0.00 sec)

mysql> quit;
Bye
[root@db111 ~]# 
[root@db111 ~]# mysql -uroot -p123456 -e "show slave status\G" | grep -i yes
mysql: [Warning] Using a password on the command line interface can be insecure.
         Slave_IO_Running: Yes
        Slave_SQL_Running: Yes
[root@db111 ~]# 

2.6 在客户端测试主从同步配置
2.6.1 在主库51上添加访问数据的授权用户
[root@db107 ~]# mysql -uroot -p123456
mysql> grant all on gamedb.* to admin@”%” identified by “123456”;

2.6.2  在客户端主机连接主库51 建库表记录
mysql> create database gamedb;
Query OK, 1 row affected (0.01 sec)

mysql> create table  gamedb.t1 (id int);
Query OK, 0 rows affected (0.04 sec)

mysql> insert into gamedb.t1 values(999);
Query OK, 1 row affected (0.15 sec)

mysql> insert into gamedb.t1 values(999);
Query OK, 1 row affected (0.05 sec)

mysql> select  * from gamedb.t1;
+------+
| id   |
+------+
|  999 |
|  999 |
+------+
2 rows in set (0.00 sec)

mysql> 

2.6.3  在客户端使用授权用户连接从库52-55,也能看到同样的库表及记录

[root@host114 ~]# mysql -h从库IP地址 -uadmin -p123456
mysql> select  * from gamedb.t1;
+------+
| id   |
+------+
|  999 |
|  999 |
+------+

2.6.4 检查配置环境
在主机51 52  53  检查是否有同步数据的用户 repluser   
mysql> show  grants  for  repluser@"%";

在主机51~55 做如下授权
mysql> grant  all on  *.*  to  root@"%"    identified by "123456";

在主机51~55 做如下设置
mysql> set global relay_log_purge=off;

3.1 在所有主机上安装perl软件包mha_node软件包(51~56)
yum -y install perl-*.rpm
yum -y install perl-DBD-mysql
rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm
3.2 只在管理 “主机56” 上安装mha_manager软件包
]# yum -y install perl-ExtUtils-* perl-CPAN*
]#tar -zxvf mha4mysql-manager-0.56.tar.gz
]#cd mha4mysql-manager-0.56
]#perl Makefile.PL
]#make
]#make install

4 4.1 拷贝命令(56)
]# cp mha4mysql-manager-0.56/bin/* /usr/local/bin/

4.2 创建工作目录 和主配置文件 (56)
]# mkdir    /etc/mha_manager/

]# cp mha4mysql-manager-0.56/samples/conf/app1.cnf  

/etc/mha_manager/

4.3 创建故障切换脚本(56)
]# ls  /usr/local/bin/master_ip_failover

]# cp mha4mysql-manager-0.56/samples/scripts/ master_ip_failover   /usr/local/bin/

4.4 编辑主配置文件 app1.cnf(56)
vim  /etc/mha_manager/app1.cnf


4.5 在主库上手动部署vip 地址   192.168.4.100
[root@db51 ~]# ifconfig  eth0:1 192.168.4.100/24

[root@db51 ~]# ifconfig  eth0:1
eth0:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 

1500
    inet 192.168.4.100  netmask 255.255.255.0  broadcast 

192.168.4.255
    ether 74:52:09:07:51:01  txqueuelen 1000  (Ethernet)


4.6  修改故障切换脚本 指定vip地址的部署信息(56)
]# vim /usr/local/bin/master_ip_failover 
my $vip = '192.168.4.100/24';  # Virtual IP 
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig eth0:$key $vip";
my $ssh_stop_vip = "/sbin/ifconfig eth0:$key down";
:wq

5 启动管理服务,并查看服务状态(56)
]# masterha_manager –conf=/etc/mha/app1.cnf
–remove_dead_master_conf –ignore_last_failover

[root@host56 ~]# masterha_check_status --conf=/etc/mha_manager/app1.cnf
app1 (pid:16944) is running(0:PING_OK), master:192.168.4.51
[root@host56 ~]# 

6 验证配置
验证ssh 免密码登录 数据节点主机(56)
]# masterha_check_ssh –conf=/etc/mha_manager/app1.cnf

Sun May  6 16:38:19 2018 - [info] All SSH connection tests passed 

successfully.

验证 数据节点的主从同步配置(要不调用故障切换脚本)
masterha_check_repl --conf=/etc/mha_manager/app1.cnf

MySQL Replication Health is OK.

7 7.1 测试故障转移
在主库51 上执行 ]# shutdown -h now

7.2  在管理主机上查看服务状态(如果服务停止了,手动启动一下服务,再查看状态)
[root@host56 ~]# masterha_check_status --conf=/etc/mha_manager/app1.cnf
app1 (pid:17507) is running(0:PING_OK), master:192.168.4.52

7.3   在52 本机查看是否获取vip地址
[root@db52 ~]# ip addr  show  | grep  192.168.4
    inet 192.168.4.52/24 brd 192.168.4.255 scope global eth0
    inet 192.168.4.100/24 brd 192.168.4.255 scope global secondary eth0:1
[root@db52 ~]# 

7.4   客户端连接vip地址 ,访问数据服务
]#mysql   -h192.168.4.100   -uwebadmin   -p123456
mysql> 

猜你喜欢

转载自blog.csdn.net/liao__ran/article/details/81705782