搭建mysql高可用架构mha
mha简介
mha的主要目的是自动化master故障转移和slave自动提升为master,在较短时间(一般为10-30秒)的停机时间,可以避免复制和一致性问题,不用花钱购买新的服务器,没有性能上的损失,不复杂的(容易安装),并且不用改变现有的部署。
mha还提供了一个在线预定master开关的方式:改变当前正在运行的master安全的转移到到新的mater,在几秒钟内的停机时间(0.5-2秒)(只支持块写入操作)。
mha提供了以下功能,并且可以满足许多部署的要求,如高可用性,数据完整性,不中断master的维护
测试环境:192.168.1.4(mhamanager+mhamaster(mysql))
192.168.1.5(mhanodeslave1)
192.168.1.6(mhanodeslave2)
1.每个节点需要ssh互通
ssh-keygen -t rsa
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]
2.安装mhanode每个节点都需安装
注:本机双光盘yum源+epel源
wget http://mirrors.hust.edu.cn/epel//6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
rpm --import /etc/pki/rpm-gpg/rpm-gpg-key-epel-6
yum clean all
yum list(会列出epel源)
yum -y install mysql*
yum -y install perl-dbd-mysql ncftp
yum -y install mha4mysql-node-0.53-0.el6.noarch.rpm
3.192.168.1.4 安装 配置mhamager
yum -y install mha4mysql-manager-0.53-0.el6.noarch.rpm
mkdir /etc/masterha
mkdir -p /masterha/app1
vi /etc/masterha/app1.cnf
[server default]
user=mhauser
#mha管理用户需添载访问mysql权限
password=mhauser123
#mha用户密码
manager_workdir=/masterha/app1
manager_log=/masterha/app1/manager.log
remote_workdir=/masterha/app1
ssh_user=root
#ssh用户
repl_user=rep
#主从同步用户
repl_password=rep123
#需添载访问mysql权限
#主从用户密码
ping_interval=1
[server1]
hostname=192.168.1.4
ssh_port=22
master_binlog_dir=/var/lib/mysql
#二进制日志位置
[server2]
hostname=192.168.1.5
ssh_port=22
master_binlog_dir=/var/lib/mysql
candidate_master=1
#备份mysql
[server3]
hostname=192.168.1.6
ssh_port=22
master_binlog_dir=/var/lib/mysql
验证ssh 信任登录是否成功
ln -s /usr/lib/perl5/vendor_perl/mha /usr/lib64/perl5/vendor_perl/mha
masterha_check_ssh --conf=/etc/masterha/app1.cnf
4.在每个节点上部署mysql 及主从同步
修改配置文件
server-id=1
#master id=1 slave1=2 slave2=3
log-bin=mysql-bin
设置的复制权限帐号
GRANT REPLICATION SLAVE,REPLICATION CLIENT ON *.* TO 'rep'@'%' IDENTIFIED BY 'rep123';
FLUSH PRIVILEGES;
change master to
master_host='192.168.1.4',
master_port=3306,
master_user='rep',
master_password='rep123',
master_log_file='mysql-bin.000001',
#不能随便复制 此处设置 来自在master点执行show master status;
master_log_pos=98;
slave start;
show slave status\G;
#建立mha使用的帐号并设置权限
grant all on *.* to mhauser@'%' identified by 'mhauser123';
grant all on *.* to mhauser@'%' identified by 'mhauser123';
grant all on *.* to mhauser@'%' identified by 'mhauser123';
FLUSH PRIVILEGES;
5
启动MHA manager
nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log 2>&1 &
验证MHA状态
masterha_check_status --conf=/etc/masterha/app1.cnf
app1 (pid:5605) is running(0:PING_OK), master:192.168.1.4
关闭MHA manager
ps auxf|grep masterha_manager|grep -v grep|awk '{print $2}'|xargs kill
测试master宕机后是否自动切换
手动停止 mastre节点
service mysqld stop