MHA項目網站
https://code.google.com/p/mysql-master-ha/
一.環境介紹
1.主機部署
manager機:10.10.54.154
master機:10.10.54.156
slave1機:10.10.54.155(備用master)
slave2機:10.10.54.157
2.大致步驟
A.首先用ssh-keygen實現四台主機之間相互免密鑰登錄
B.安裝MHAmha4mysql-node,mha4mysql-manager 軟件包
C.建立master,slave1,slave2之間主從復制
D.管理機manager上配置MHA文件
E.masterha_check_ssh工具驗證ssh信任登錄是否成功
F.masterha_check_repl工具驗證mysql復制是否成功
G.啟動MHA manager,並監控日志文件
H.測試master(156)宕機後,是否會自動切換
3.說明:下面中括號中的主機名說明了當前操作是在哪台機子上進行的
二.首先用ssh-keygen實現四台主機之間相互免密鑰登錄
[manager機]
shell> ssh-keygen -t rsa -b 2048
shell> scp-copy-id [email protected]
shell> scp-copy-id [email protected]
shell> scp-copy-id [email protected]
在另外三台機子重復此步驟,使四台機子中的任何兩台之間可以免密碼登錄
三.安裝MHAmha4mysql-node,mha4mysql-manager 軟件包
1.四台主機上安裝MHAmha4mysql-node
1 2 3 4 5[manager,master,slave1,slave2]
shell>
yum update
shell>
yum -y install perl-DBD-MySQL
ncftp
shell>
wget http://mysql-master-ha.googlecode.com/files/mha4mysql-node-0.53-0.noarch.rpm
sehll>
rpm -ivh mha4mysql-node-0.53-0.noarch.rpm
2.在manager機子上安裝mha4mysql-manager
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[manager]
shell>
yum install perl
shell>
yum install cpan
shell>
rpm -ivh mha4mysql-manager-0.53-0.el6.noarch.rpm
error:
perl(Config::Tiny)
is needed by mha4mysql-manager-0.53-0.noarch
perl(Log::Dispatch)
is needed by mha4mysql-manager-0.53-0.noarch
perl(Log::Dispatch::File)
is needed by mha4mysql-manager-0.53-0.noarch
perl(Log::Dispatch::Screen)
is needed by mha4mysql-manager-0.53-0.noarch
perl(Parallel::ForkManager)
is needed by mha4mysql-manager-0.53-0.noarch
perl(Time::HiRes)
is needed by mha4mysql-manager-0.53-0.noarch
[solution]
shell>
wget ftp://ftp.muug.mb.ca/mirror/centos/5.10/os/x86_64/CentOS/perl-5.8.8-41.el5.x86_64.rpm
shell>
wget ftp://ftp.muug.mb.ca/mirror/centos/6.5/os/x86_64/Packages/compat-db43-4.3.29-15.el6.x86_64.rpm
shell>
wget http://downloads.naulinux.ru/pub/NauLinux/6x/i386/sites/School/RPMS/perl-Log-Dispatch-2.27-1.el6.noarch.rpm
shell>
wget http://dl.fedoraproject.org/pub/epel/6/i386/perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm
shell>
wget http://dl.fedoraproject.org/pub/epel/6/i386/perl-Mail-Sender-0.8.16-3.el6.noarch.rpm
shell>
wget http://dl.fedoraproject.org/pub/epel/6/i386/perl-Mail-Sendmail-0.79-12.el6.noarch.rpm
shell>
wget http://mirror.centos.org/centos/6/os/x86_64/Packages/perl-Time-HiRes-1.9721-136.el6.x86_64.rpm
shell>
rpm -ivh perl-Parallel-ForkManager-0.7.9-1.el6.noarch.rpm perl-Log-Dispatch-2.27-1.el6.noarch.rpm perl-Mail-Sender-0.8.16-3.el6.noarch.rpm perl-Mail-Sendmail-0.79-12.el6.noarch.rpm perl-Time-HiRes-1.9721-136.el6.x86_64.rpm
shell>
rpm -ivh mha4mysql-manager-0.53-0.el6.noarch.rpm
四.建立master,slave1,slave2之間主從復制
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[master:156]
1.shell>
vim /etc/my.cnf
#server-id
改為1
server-id=1
log-bin=mysql-bin
binlog_format=mixed
#授權操作
2.mysql>
GRANT ALL PRIVILEGES ON *.* TO 'rep'@'10.10.54.%' IDENTIFIED
BY 'rep123';
mysql>
flush privileges;
3.mysql>
show master status;
[slave1,slave2]
4.change
master操作
mysql>
change master to
master_host='10.10.54.156',
master_port=3306,
master_user='rep',
master_password='rep123',
master_log_file='mysql-bin.000001',
master_log_pos=112;
注意:slave1機子上也要授權,因為這個是備用master
[slave1:155]
5.mysql> GRANT ALL PRIVILEGES ON *.* TO 'rep'@'10.10.54.%' IDENTIFIED BY 'rep123';
[master,slave1,slave2]
6.查看主從復制是否成功的一些命令
mysql> start slave;
mysql> stop slave;
mysql> reset slave;
mysql> show slave status\G;
五.所有主機上設置復制權限帳號
mysql> GRANT ALL PRIVILEGES ON *.* TO 'mha_rep'@'10.10.2.10' IDENTIFIED BY '123456';
六.manager上配置MHA文件,管理各個節點
[manager:154]
shell> mkdir -p /masterha/app1
shell> mkdir /etc/masterha
shell> vim /etc/masterha/app1.cnf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
[server
default]
user=mha_rep ##mysql管理用戶名
password=123456
manager_workdir=/masterha/app1 #目錄可以任意指定
manager_log=/masterha/app1/manager.log
remote_workdir=/masterha/app1
ssh_user=root #ssh免密鑰登錄的帳號名
repl_user=rep #mysql復制帳號,用來在主從機之間同步二進制日志等
repl_password=rep123
ping_interval=1 #ping間隔時間,用來檢測master是否正常
[server1]
hostname=10.10.54.155
#ssh_port=9999
master_binlog_dir=/data/ndb #mysql數據庫目錄
candidate_master=1 #master機宕掉後,優先啟用這台作為新master
[server2]
hostname=10.10.54.156
#ssh_port=9999
master_binlog_dir=/data/ndb
candidate_master=1
[server3]
hostname=10.10.54.157
#ssh_port=9999
master_binlog_dir=/data/ndb
no_master=1 #設置no_master=1使主機不能成為新master
七.驗證ssh信任登陸和mysql主從復制是否成功
1.masterha_check_ssh 驗證ssh信任登陸
1
2
3
4
[manager:154]
shell>
masterha_check_ssh --conf=/etc/masterha/app1.cnf
Sun
Mar 2 17:45:38 2014 - [debug] ok.
Sun
Mar 2 17:45:38 2014 - [info] All SSH connection tests passed successfully.
2.masterha_check_repl 驗證mysql復制是否成功
1
2
3
4
5
6
7
8
9
10
11
[manager:154]
shell>
masterha_check_repl --conf=/etc/masterha/app1.cnf
---------------------------------------------------------
Sun
Mar 2 13:16:57 2014 - [info] Slaves settings check done.
Sun
Mar 2 13:16:57 2014 - [info]
10.10.54.156
(current master)
+--10.10.54.155
+--10.10.54.157
...
MySQL
Replication Health is OK.
---------------------------------------------------------------
八.啟動MHA manager,並監控日志文件
1
2
3
4
5
6
7
8
9
10
[manager:154]
shell> nohup masterha_manager
--conf=/etc/masterha/app1.cnf
> /tmp/mha_manager.log
2>&1
shell> tail -f /masterha/app1/manager.log
---------------------------------------------------------------
10.10.54.156
(current master)
+--10.10.54.155
+--10.10.54.157
...
Sun
Mar 2 13:09:25 2014 - [info] Ping(SELECT) succeeded, waiting until MySQL
doesn't respond..
-----------------------------------------------------------------
監控的manager.log文件表明MHA運行良好,正在 "waiting until MySQL
doesn't respond"
九.測試master(156)宕機後,是否會自動切換
1.測試自動切換是否成功
當掉master機子
shell> /etc/init.d/myqld stop
當掉master後,manager上的監控文件/masterha/app1/manager.log顯示錯誤信息,表示不能自動切換:
[error]
-----------------------------------------------------------
Sun Mar 2 13:13:46 2014 - [error][/usr/share/perl5/vendor_perl/MHA/ManagerUtil.pm, ln178] Got ERROR: Use of uninitialized value $msg in scalar chomp at /usr/share/perl5/vendor_perl/MHA/ManagerConst.pm line 90.
-----------------------------------------------------------
解決這個錯誤是在文件/usr/share/perl5/vendor_perl/MHA/ManagerConst.pm 第90行(chomp $msg)前加入一行:
1
$msg
= "" unless($msg);
好了,錯誤解決了,下面我們再次重復上面步驟:
master上mysql服務:shell> /etc/init.d/mysqld stop
再次查看manager機子上監控文件內容
shell> tail -f tail -f /masterha/app1/manager.log
日志文件顯示:
-----------------------------------------------------------
----- Failover Report -----
app1: MySQL Master failover 10.10.54.156 to 10.10.54.155 succeeded
Master 10.10.54.156 is down!
Check MHA Manager logs at mycentos4:/masterha/app1/manager.log for details.
Started automated(non-interactive) failover.
The latest slave 10.10.54.155(10.10.54.155:3306) has all relay logs for recovery.
Selected 10.10.54.155 as a new master.
10.10.54.155: OK: Applying all logs succeeded.
10.10.54.157: This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
10.10.54.157: OK: Applying all logs succeeded. Slave started, replicating from 10.10.54.155.
10.10.54.155: Resetting slave info succeeded.
Master failover to 10.10.54.155(10.10.54.155:3306) completed successfully.
--------------------------------------------------------
2.切換成功後,檢查replication狀態
[master:156]
shell> /etc/init.d/mysqld start
[manager:154]
shell> masterha_check_repl --conf=/etc/masterha/app1.cnf
--------------------------------------------------------------
Sun Mar 2 13:22:11 2014 - [info] Slaves settings check done.
Sun Mar 2 13:22:11 2014 - [info]
10.10.54.155 (current master)
+--10.10.54.156
+--10.10.54.157
...
MySQL Replication Health is OK.
---------------------------------------------------------------
上面的"10.10.54.155 (current master)" 這句表明master成功切換到155機子上
十.上一步測試之後,新master機為155,宕掉155機子,再次測試故障轉移
1.啟動管理節點
shell> nohup masterha_manager --conf=/etc/masterha/app1.cnf > /tmp/mha_manager.log 2>&1
2.啟動日志檢測,然後當掉新master(155),然後查看監控文件變化
shell> tail -f /masterha/app1/manager.log
3.當掉155機子(即新的master)
shell> /etc/init.d/mysqld stop
4.查看manager主機上的監控文件變化
[error][/usr/share/perl5/vendor_perl/MHA/MasterFailover.pm, ln295] Last failover was done at 2014/03/02 13:02:47. Current time is too early to do failover again. If you want to do failover, manually remove /masterha/app1/app1.failover.complete and run this
script again.
錯誤解決辦法
1.日志文件提示切換master過快,需要刪除/masterha/app1/app1.failover.complete
1.刪除app1.failover.complete
shell> rm /masterha/app1/app1.failover.complete
5.重新測試:
master轉移成功,重新轉為156機子
--------------------------------------------------------
Master 10.10.54.155 is down!
Check MHA Manager logs at mycentos4:/masterha/app1/manager.log for details.
Started automated(non-interactive) failover.
The latest slave 10.10.54.156(10.10.54.156:3306) has all relay logs for recovery.
Selected 10.10.54.156 as a new master.
10.10.54.156: OK: Applying all logs succeeded.
10.10.54.157: This host has the latest relay log events.
Generating relay diff files from the latest slave succeeded.
10.10.54.157: OK: Applying all logs succeeded. Slave started, replicating from 10.10.54.156.
10.10.54.156: Resetting slave info succeeded.
Master failover to 10.10.54.156(10.10.54.156:3306) completed successfully.
-----------------------------------------------------------
//附:故障轉移後,用命令恢復原來的master
[manager:154]
1.在舊master上執行
mysql> reset master;
mysql> change master to master_host='10.10.54.155', master_port=3306, master_user='rep', master_password='rep123', master_log_file='mysql-bin.000031', master_log_pos=112;
mysql> start slave; #暫時先把舊master變為從
2.然後在manager節點上:
[manager:154]
shell> masterha_master_switch --master_state=alive --conf=/etc/masterha/app1.cnf
##master成功切換回