实验环境:三台数据库服务器上已经安装好的5.7版本的mysql
server3: matser 172.25.20.3
server4: master-slave 172.25.20.4
server5:master-salve 172.25.20.5
server6: mha-manager 172.25.20.6
1.server的配置文件/etc/my.cnf 必须相同
2. server1,server2,server3 数据库的操作
在server1上的数据库操作
mysql> alter user root@localhost identified by 'Xiaozhuang+006';
mysql> flush privileges;
mysql> grant replication slave on *.* to repl@'%' identified by 'Xiaozhuang+006';
mysql> show master status;
server4 和server5上数据库操作(server4,sever5设置相同)
mysql> alter user root@localhost identified by 'Xiaozhuang+006';
mysql> change master to master_host='172.25.20.3',master_user='repl',master_password='Xiaozhuang+006',master_log_file='mysql-bin.00003', master_log_pos=833
mysql> start slave;
mysql> show slave status\G;
server3,server4,server5进行半同步配置,深度保证数据一致性(操作完全一致)
在配置之前一定要在数据库中安装好 否则重启数据库的时候会失败
在server3 server4 server5上进行同样的操作
mysql> INSTALL PLUGIN rpl_semi_sync_master SONAME 'semisync_master.so';
mysql> INSTALL PLUGIN rpl_semi_sync_slave SONAME 'semisync_slave.so';
同样也都编辑 /etc/my.cnf 然后重启
可见 重启成功l
主从复制测试成功:再server3中建数据库,server4和server5都成功出现xiaozhuang
server6 上先安装mha4mysql-node-0.56-0.el6.noarch.rpm
然后解决依赖性后再安装
mha4mysql-manager-0.56-0.el6.noarch.rpm
在server3 server4 server5 上都安装
mha4mysql-node-0.56-0.el6.noarch.rpm
1.配置MHA manger(server6)目录,编辑配置文件
自己创建目录文件
mkdir /etc/mhamanger
vim /etc/mhamanger/app.conf
cat /etc/mhamanger/app.conf
[server default]
manager_workdir=/etc/masterha/
manager_log=/etc/masterha/manager.log
master_binlog_dir=/var/lib/mysql
#master_ip_failover_script= /usr/local/bin/master_ip_failover
#master_ip_online_change_script= /usr/local/bin/master_ip_online_change
password=Xiaozhuang+006
user=root
ping_interval=1
remote_workdir=/tmp
repl_password=Xiaozhiang+006
repl_user=repl
#report_script=/usr/local/send_report
#secondary_check_script= /usr/local/bin/masterha_secondary_check -s server5 -s server3
#shutdown_script=""
ssh_user=root
[server3]
hostname=172.25.20.3
port=3306
[server4]
hostname=172.25.20.4
port=3306
candidate_master=1
check_repl_delay=0
[server5]
hostname=172.25.20.5
port=3306
2. 让server1,server2,server3,server4可以互相免密ssh
server6上 直接回车默认 生成的文件在/root/.ssh 下
[root@server6 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
5b:9c:23:e5:91:26:98:be:af:e9:ef:da:d2:08:b2:ef root@server6
The key's randomart image is:
+--[ RSA 2048]----+
| |
| o . |
| o . = |
| . * o |
| . S * |
| . . . + . |
| o ..o. |
| . o+. |
| oE.=*= |
+-----------------+
直接将/root/.ssh/id_rsa.pub 加到 /root/.ssh/authorized_keys 中
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
#这样可以让master免密登陆slave
ssh-copy-id 172.25.20.3
ssh-copy-id 172.25.20.4
ssh-copy-id 172.25.20.5
注意了,在这里我给大家提醒一下
免密连接 不是server6可以通信server3 server4 server5 就ok了
而是server3 server4 server5 server6 都可以互相通信
这里也要给server3 server4 server5生成ssh的key
把server3 server4 server5的key文件都加到/root/.ssh中
这样就可以实现它们之间互相通信了
还有注意的是 第一次连接需要密码的
免密连接成功
* 检测 masterha_check_ssh –conf=/etc/masterha/app.conf*
出现All SSH connection tests passed successfully. 则证明连接成功
数据库授权
mysql> grant all on *.* to root@'%' identified by 'Xiaozhuang+006';
Query OK, 0 rows affected, 1 warning (0.43 sec)
mysql>
检测结果
出现 MySQL Replication Health is NOT OK! 则说明成功
[root@server6 ~]# masterha_check_repl --conf=/etc/masterha/app.conf
在监控管理的server6上开启MHA进程
masterha_manager --conf=/etc/masterha/app.conf &
这个启动时间挺长 打入后台
查看进程 状态
masterha_check_status --conf=/etc/masterha/app.conf
tail -n2 /etc/masterha/manager.log
出现 Ping(SELECT) succeeded, waiting until MySQL doesn’t respond..
说明已经开始进行监控了
4.检测MHA基本高可用功能
关闭master(server3)上的mysql
server3:
/etc/iinit.d/mysqld stop
serevr5:
从原来的:
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: server3
变为:
show slave status\G;
Empty set (0.00 sec)
MariaDB [(none)]> show master status\G;
*************************** 1. row ***************************
File: mysql-bin.000001
Position: 313
Binlog_Do_DB: server4
Binlog_Ignore_DB:
1 row in set (0.00 sec)
Manager日志:
Master failover to mha4(172.25.20.4:3306) completed successfully.
server5:
show slave status\G;
*************************** 1. row ***************************
Slave_IO_State: Waiting for master to send event
Master_Host: server4