https://copyfuture.com/blogs-details/20210611040614554j
这篇也得很好!!
很早之前写过MHA的文章,但是常常在技术群看到有同学问MHA搭建的问题,不是权限问题就是配置问题,我在这里就再次一写下配置过程以及快速的搭建。如果想知道更多的细节与原理,请参考:MySQL高可用架构之MHA
环境:
1主1从,manager放在从库。
主库:192.168.0.10
从库:192.168.0.20
两台机器的mysql安装完成初始化以后进行复制搭建,首先登录主库(192.168.0.10),查看pos点:
mysql> show master status; +------------------+----------+--------------+------------------+-------------------+ | File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set | +------------------+----------+--------------+------------------+-------------------+ | mysql-bin.000001 | 154 | | | | +------------------+----------+--------------+------------------+-------------------+ 1 row in set (0.00 sec)
然后在主库(192.168.0.10)
添加复制账号以及mha用的账号
mysql> grant replication slave on *.* to 'repl'@'192.168.0.10' identified by 'repl'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant replication slave on *.* to 'repl'@'192.168.0.20' identified by 'repl'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all on *.* to 'root'@'192.168.0.20' identified by '123'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> grant all on *.* to 'root'@'192.168.0.10' identified by '123'; Query OK, 0 rows affected, 1 warning (0.01 sec) mysql>
从库(192.168.0.10 )change到mysql-bin.000001,pos点154
CHANGE MASTER TO MASTER_HOST='192.168.0.10',MASTER_USER='repl',MASTER_PASSWORD='repl',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=154;
mysql> start slave; Query OK, 0 rows affected (0.15 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.10 Master_User: repl Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000001 Read_Master_Log_Pos: 1344 Relay_Log_File: relaylog.000002 Relay_Log_Pos: 1510 Relay_Master_Log_File: mysql-bin.000001 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table:
复制到这来就搭建完成了,然后配置192.168.0.10和192.168.0.20 ssh互信。(两台机器都执行)
ssh-keygen -t rsa ssh-copy-id -i /root/.ssh/id_rsa.pub '-p 22 192.168.0.10' ssh-copy-id -i /root/.ssh/id_rsa.pub '-p 22 192.168.0.20'
安装MHA软件,首先安装epel源。(2台机器)
rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
安装依赖包
yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes -y
安装MHA软件(两台机器)
tar xf mha4mysql-node-0.56.tar.gz cd mha4mysql-node-0.56 perl Makefile.PL make && make install tar xf mha4mysql-manager-0.56.tar.gz cd mha4mysql-manager-0.56 perl Makefile.PL make && make install
在从库(192.168.0.20)创建目录:
mkdir /data/mha/3306/log cd /data/mha/3306/touch mha.cnf
mha.cnf配置文件内容如下:
[server default] client_bindir=/usr/local/mysql/bin/ manager_log=/data/mha/3306/log/manager.log manager_workdir=/data/mha/3306/log master_binlog_dir=/data/mysql/3306/binlog/ master_ip_failover_script=/usr/local/bin/master_ip_failover master_ip_online_change_script= /usr/local/bin/master_ip_online_change report_script=/usr/local/bin/send_report init_conf_load_script=/usr/local/bin/load_cnf remote_workdir=/data/mysql/3306/mysqltmp #secondary_check_script= /usr/local/bin/masterha_secondary_check -s 192.168.0.30 -s 192.168.0.40 user=root ping_interval=3 repl_user=repl ssh_port=22 ssh_user=root max_ping_errors=40 [server1] hostname=192.168.0.10 port=3306 [server2] candidate_master=1 check_repl_delay=0 hostname=192.168.0.20 port=3306
编辑文件 /usr/local/bin/load_cnf 里面的密码修改成对应的密码
#!/usr/bin/perl print "password=123\n"; print "repl_password=repl\n";
执行chek命令查看复制是否正常:
View Code
主库(192.168.0.10)执行命令,启动vip:
/sbin/ifconfig eth1:1 192.168.0.88/16
在线切换,把主库切到192.168.0.20
masterha_master_switch --master_state=alive --conf=/data/mha/3306/mha.cnf --new_master_host=192.168.0.20 --new_master_port=3306 --orig_master_is_new_slave
输出如下:
View Code
关于配置文件中的参数:
max_ping_errors=40,这个是修改了源码,增加了检测次数的定义,默认是3次,太容易误切换。
启动管理进程:
/usr/bin/nohup /usr/local/bin/masterha_manager --conf=/data/mha/3306/mha.cnf --ignore_last_failover > /data/mha/3306/log/manager.log 2>&1 &
在主库(192.168.0.10)封掉ip,可以看到日志输出。
iptables -I INPUT -s 192.168.0.20 -j DROP
View Code
修改以后的软件包下载地址:链接:百度网盘 请输入提取码 密码:f9u2