说明:
centos 6.5 mysql 5.5.37 mha4mysql-manager-0.55 mha4mysql-node-0.54
manager 192.168.1.1
db1 192.168.1.2
db2 192.168.1.3
db3 192.168.1.4
配置mysql主从
db1:
server-id = 1
read_only = 1
relay_log_purge=0
binlog_format=mixed
db2,db3忽略
db1主
grant replication slave on *.* to mharep@‘192.168.1.%‘ identified by ‘passwd‘; flush privileges; stop slave; #设置复制权限帐号 GRANT ALL PRIVILEGES ON *.* TO ‘mha_manager‘@‘192.168.1.%‘ IDENTIFIED BY ‘123456‘; reset master; show master status\G
db2备
grant replication slave on *.* to mharep@‘192.168.1.%‘ identified by ‘passwd‘; GRANT ALL PRIVILEGES ON *.* TO ‘mha_manager‘@‘192.168.1.%‘ IDENTIFIED BY ‘123456‘; flush privileges; reset master; slave stop; change master to MASTER_HOST=‘192.168.1.2‘, MASTER_PORT=3306,MASTER_USER=‘mharep‘, MASTER_PASSWORD=‘passwd‘,master_log_file=‘mysql-bin.000001‘, master_log_pos=107; slave start; show slave status\G;
db3从
reset master; slave stop; GRANT ALL PRIVILEGES ON *.* TO ‘mha_manager‘@‘192.168.1.%‘ IDENTIFIED BY ‘123456‘; flush privileges; change master to MASTER_HOST=‘192.168.1.2‘, MASTER_PORT=3306,MASTER_USER=‘mharep‘, MASTER_PASSWORD=‘passwd‘,master_log_file=‘mysql-bin.000001‘, master_log_pos=107; slave start; show slave status\G;
配置双机互信
manager
ssh-keygen -t rsa for i in db1 db2 db3;do ssh-copy-id -i ~/.ssh/id_rsa.pub root@$i;done
db1主
ssh-keygen -t rsa for i in manager db2 db3;do ssh-copy-id -i ~/.ssh/id_rsa.pub root@$i;done
db2备 和 db3从 配置略
安装mysql-MHA
https://downloads.mariadb.com/files/MHA
wget https://downloads.mariadb.com/files/MHA/mha4mysql-manager-0.55-0.el6.noarch.rpm wget https://downloads.mariadb.com/files/MHA/mha4mysql-node-0.54-0.el6.noarch.rpm
db1主 db2备 db3从
yum install perl-DBD-MySQL
yum localinstall mha4mysql-node-0.54-0.el6.noarch.rpm
manager
yum install perl cpan yum install perl-DBD-MySQL perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager
#这两个yum没有安装上 perl-Log-Dispatch perl-Parallel-ForkManager
wget http://downloads.naulinux.ru/pub/NauLinux/6x/x86_64/sites/School/RPMS/perl-Log-Dispatch-2.27-1.el6.noarch.rpm wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/perl-Parallel-ForkManager-0.7.5-2.2.el6.rf.noarch.rpm wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/perl-Mail-Sender-0.8.16-1.el6.rf.noarch.rpm wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/dag/RPMS/perl-Mail-Sendmail-0.79-1.2.el6.rf.noarch.rpm
rpm -ivh perl-Mail-Sender-0.8.16-1.el6.rf.noarch.rpm
rpm -ivh perl-Mail-Sendmail-0.79-1.2.el6.rf.noarch.rpm
yum localinstall perl-Log-Dispatch-2.27-1.el6.noarch.rpm
yum localinstall perl-Parallel-ForkManager-0.7.5-2.2.el6.rf.noarch.rpm
yum localinstall mha4mysql-node-0.54-0.el6.noarch.rpm
yum localinstall mha4mysql-manager-0.55-0.el6.noarch.rpm
配置mysql-MHA
[root@manager ~]# masterha_<Tab>
masterha_check_repl masterha_conf_host masterha_master_switch
masterha_check_ssh masterha_manager masterha_secondary_check
masterha_check_status masterha_master_monitor masterha_stop
tar -xzf mha4mysql-manager-0.55.tar.gz
ls mha4mysql-manager-0.55/samples/conf
masterha_default.cnf app1.cnf
mkdir -p /usr/local/mha/scripts
cp mha4mysql-manager-0.55/samples/scripts/* /usr/local/mha/scripts
[root@manager ~]# vim /usr/local/mha/mha.conf [server default] user=mha_manager password=123456 manager_workdir=/usr/local/mha manager_log=/usr/local/mha/manager.log remote_workdir=/usr/local/mha ssh_user=root repl_user=mharep repl_password=passwd ping_interval=1 secondary_check_script= masterha_secondary_check -s 192.168.1.2 -s 192.168.1.3 -s 192.168.1.4 master_ip_failover_script=/usr/local/mha/scripts/master_ip_failover #shutdown_script= /usr/local/mha/scripts/power_manager report_script= /usr/local/mha/scripts/send_report master_ip_online_change_script= /usr/local/mha/scripts/master_ip_online_change [server1] hostname=192.168.1.2 ssh_port=22 master_binlog_dir=/var/mysql/log candidate_master=1 [server2] hostname=192.168.1.3 ssh_port=22 master_binlog_dir=/var/mysql/log candidate_master=1 [server3] hostname=192.168.1.4 ssh_port=22 master_binlog_dir=/var/mysql/log no_master=1
#验证ssh通讯
masterha_check_ssh --conf=/usr/local/mha/mha.conf
#验证mysql复制
wget ftp://rpmfind.net/linux/dag/redhat/el6/en/x86_64/extras/RPMS/perl-Net-Telnet-3.03-2.el6.rfx.noarch.rpm
yum localinstall perl-Net-Telnet-3.03-2.el6.rfx.noarch.rpm
[root@manager ~]# masterha_check_repl --conf=/usr/local/mha/mha.conf
Thu Jan 8 18:05:40 2015 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping.
Thu Jan 8 18:05:40 2015 - [info] Reading application default configurations from /usr/local/mha/mha.conf..
Thu Jan 8 18:05:40 2015 - [info] Reading server configurations from /usr/local/mha/mha.conf..
Thu Jan 8 18:05:40 2015 - [info] MHA::MasterMonitor version 0.55.
Thu Jan 8 18:05:40 2015 - [info] Dead Servers:
Thu Jan 8 18:05:40 2015 - [info] Alive Servers:
Thu Jan 8 18:05:40 2015 - [info] 192.168.1.2(192.168.1.2:3306)
Thu Jan 8 18:05:40 2015 - [info] 192.168.1.3(192.168.1.3:3306)
Thu Jan 8 18:05:40 2015 - [info] 192.168.1.4(192.168.1.4:3306)
Thu Jan 8 18:05:40 2015 - [info] Alive Slaves:
Thu Jan 8 18:05:40 2015 - [info] 192.168.1.3(192.168.1.3:3306) Version=5.5.37-log (oldest major version between slaves) log-bin:enabled
Thu Jan 8 18:05:40 2015 - [info] Replicating from 192.168.1.2(192.168.1.2:3306)
Thu Jan 8 18:05:40 2015 - [info] Primary candidate for the new Master (candidate_master is set)
Thu Jan 8 18:05:40 2015 - [info] 192.168.1.4(192.168.1.4:3306) Version=5.5.37-log (oldest major version between slaves) log-bin:enabled
Thu Jan 8 18:05:40 2015 - [info] Replicating from 192.168.1.2(192.168.1.2:3306)
Thu Jan 8 18:05:40 2015 - [info] Not candidate for the new Master (no_master is set)
Thu Jan 8 18:05:40 2015 - [info] Current Alive Master: 192.168.1.2(192.168.1.2:3306)
Thu Jan 8 18:05:40 2015 - [info] Checking slave configurations..
Thu Jan 8 18:05:40 2015 - [info] Checking replication filtering settings..
Thu Jan 8 18:05:40 2015 - [info] binlog_do_db= , binlog_ignore_db=
Thu Jan 8 18:05:40 2015 - [info] Replication filtering check ok.
Thu Jan 8 18:05:40 2015 - [info] Starting SSH connection tests..
Thu Jan 8 18:05:41 2015 - [info] All SSH connection tests passed successfully.
Thu Jan 8 18:05:41 2015 - [info] Checking MHA Node version..
Thu Jan 8 18:05:42 2015 - [info] Version check ok.
Thu Jan 8 18:05:42 2015 - [info] Checking SSH publickey authentication settings on the current master..
Thu Jan 8 18:05:42 2015 - [info] HealthCheck: SSH to 192.168.1.2 is reachable.
Thu Jan 8 18:05:42 2015 - [info] Master MHA Node version is 0.54.
Thu Jan 8 18:05:42 2015 - [info] Checking recovery script configurations on the current master..
Thu Jan 8 18:05:42 2015 - [info] Executing command: save_binary_logs --command=test --start_pos=4 --binlog_dir=/var/mysql/log --output_file=/usr/local/mha/save_binary_logs_test --manager_version=0.55 --start_file=mysql-bin.000002
Thu Jan 8 18:05:42 2015 - [info] Connecting to root@192.168.1.2(192.168.1.2)..
Creating /usr/local/mha if not exists.. ok.
Checking output directory is accessible or not..
ok.
Binlog found at /var/mysql/log, up to mysql-bin.000002
Thu Jan 8 18:05:42 2015 - [info] Master setting check done.
Thu Jan 8 18:05:42 2015 - [info] Checking SSH publickey authentication and checking recovery script configurations on all alive slave servers..
Thu Jan 8 18:05:42 2015 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user=‘mha_manager‘ --slave_host=192.168.1.3 --slave_ip=192.168.1.3 --slave_port=3306 --workdir=/usr/local/mha --target_version=5.5.37-log --manager_version=0.55 --relay_log_info=/var/mysql/data/relay-log.info --relay_dir=/var/mysql/data/ --slave_pass=xxx
Thu Jan 8 18:05:42 2015 - [info] Connecting to root@192.168.1.3(192.168.1.3:22)..
Checking slave recovery environment settings..
Opening /var/mysql/data/relay-log.info ... ok.
Relay log found at /var/mysql/data, up to mysql-relay-bin.000007
Temporary relay log file is /var/mysql/data/mysql-relay-bin.000007
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Thu Jan 8 18:05:42 2015 - [info] Executing command : apply_diff_relay_logs --command=test --slave_user=‘mha_manager‘ --slave_host=192.168.1.4 --slave_ip=192.168.1.4 --slave_port=3306 --workdir=/usr/local/mha --target_version=5.5.37-log --manager_version=0.55 --relay_log_info=/var/mysql/data/relay-log.info --relay_dir=/var/mysql/data/ --slave_pass=xxx
Thu Jan 8 18:05:42 2015 - [info] Connecting to root@192.168.1.4(192.168.1.4:22)..
Checking slave recovery environment settings..
Opening /var/mysql/data/relay-log.info ... ok.
Relay log found at /var/mysql/data, up to mysql-relay-bin.000006
Temporary relay log file is /var/mysql/data/mysql-relay-bin.000006
Testing mysql connection and privileges.. done.
Testing mysqlbinlog output.. done.
Cleaning up test file(s).. done.
Thu Jan 8 18:05:43 2015 - [info] Slaves settings check done.
Thu Jan 8 18:05:43 2015 - [info]
192.168.1.2 (current master)
+--192.168.1.3
+--192.168.1.4
Thu Jan 8 18:05:43 2015 - [info] Checking replication health on 192.168.1.3..
Thu Jan 8 18:05:43 2015 - [info] ok.
Thu Jan 8 18:05:43 2015 - [info] Checking replication health on 192.168.1.4..
Thu Jan 8 18:05:43 2015 - [info] ok.
Thu Jan 8 18:05:43 2015 - [info] Checking master_ip_failover_script status:
Thu Jan 8 18:05:43 2015 - [info] /usr/local/mha/scripts/master_ip_failover --command=status --ssh_user=root --orig_master_host=192.168.1.2 --orig_master_ip=192.168.1.2 --orig_master_port=3306
IN SCRIPT TEST====/sbin/ifconfig eth0:1 down==/sbin/ifconfig eth0:1 192.168.1.100;/sbin/arping -I eth0 -c 3 -s 192.168.1.100 192.168.1.1 >/dev/null 2>&1===
Checking the Status of the script.. OK
Thu Jan 8 18:05:46 2015 - [info] OK.
Thu Jan 8 18:05:46 2015 - [warning] shutdown_script is not defined.
Thu Jan 8 18:05:46 2015 - [info] Got exit code 0 (Not master dead).
MySQL Replication Health is OK.
启动MHA
[root@manager ~]# masterha_manager --conf=/usr/local/mha/mha.conf &
查看MHA
[root@manager ~]# masterha_check_status --conf=/usr/local/mha/mha.conf
mha (pid:2686) is running(0:PING_OK), master:192.168.1.2
停止MHA
[root@manager ~]# masterha_stop --conf=/usr/local/mha/mha.conf
验证MHA:
[root@db1 ~]# ip a #现在有VIP
[root@db1 ~]# service mysqld stop #mysql停止,在这时候VIP会做转移
Shutting down MySQL... SUCCESS!
[root@db1 ~]# ip a #已经没有VIP
IP已经转移到db2,并且在db3上 mysql> show slave status\G 查看 Master_Server_Id: 2
本文出自 “金戈铁马行飞燕” 博客,请务必保留此出处http://bbotte.blog.51cto.com/6205307/1600894
原文地址:http://bbotte.blog.51cto.com/6205307/1600894