Mysql高能夠之MHA搭建

Mysql高能夠之MHA搭建
php


系統:CentOS6.5_X86_64
Mysql:MySQL-5.6.17-1.el6.x86_64

監控機(Manager):192.168.1.101
主庫(Master):192.168.1.102
備主(Candicate Master):192.168.1.103
從庫(Slave):192.168.1.104
虛擬IP(VIP):192.168.1.105


1、安裝MySQL數據庫(主、備、從)
#yum install -y MySQL-server
#yum install -y MySQL-client
#yum install -y MySQL-shared-compat (能夠解決一些兼容性,能夠不裝)
html




2、主備從數據同步設置(雙主一從模式)
要點:負責在主、備、從服務器傳輸各類修改動做的媒介是主服務器的二進制變動日誌,這個日誌記載着須要傳輸給從服務器的各類修改動做。所以,主服務器必須激活二進制日誌功能。從服務器必須具有足以讓它鏈接主服務器並請求主服務器把二進制變動日誌傳輸給它的權限。

2.1.(主、備、從)修改my.cnf文件
主庫:
在[mysql]標籤下添加信息
#vim /etc/my.conf
erver_id=1
log-bin=master1-bin
log-bin-index=master1-bin.index
relay_log=mysql-relay-bin
relay_log_index=mysql-relay-bin.index
log_slave_updates= 1
sync_binlog=1
max_binlog_size= 1G
expire_logs_days = 15

備主:
在[mysql]標籤下添加信息
#vim /etc/my.conf
server_id=2
log-bin=master2-bin
log-bin-index=master2-bin.index
relay_log=mysql-relay-bin
relay_log_index=mysql-relay-bin.index
log_slave_updates= 1
max_binlog_size= 1G
expire_logs_days = 15

從庫:
在[mysql]標籤下添加信息
#vim /etc/my.conf
server_id=3
log-bin=slave-bin
log-bin-index=slave-bin.index
relay_log=mysql-relay-bin
relay_log_index=mysql-relay-bin.index
log_slave_updates= 1
sync_binlog=1
max_binlog_size= 1G
expire_logs_days = 15
read_only = 1

server_id是必須的,並且惟一。slave沒有必要開啓二進制日誌,可是在一些狀況下,必須設置,例如:若是slave爲其它slave的master,必須設置bin_log。這裏開啓了二進制日誌並設置了顯示的命名(默認名稱爲hostname,可是,若是hostname改變則會出現問題)。
relay_log配置中繼日誌,log_slave_updates表示slave將複製事件寫進本身的二進制日誌。
有些人開啓了slave的二進制日誌,卻沒有設置log_slave_updates,而後查看slave的數據是否改變,這是一種錯誤的配置。因此,儘可能使用read_only,它防止改變數據(除了特殊的線程)。


2.2.(主、備、從)啓動MySQL
#service mysql start

#/etc/init.d/mysql start


2.3.主庫建立數據複製專用帳號
Master的數據庫中創建一個備份賬戶:每一個slave使用標準的MySQL用戶名和密碼鏈接master。進行復制操做的用戶會授予REPLICATION SLAVE權限。用戶名的密碼都會存儲在文本文件master.info中。

主庫爲備主和從庫建立數據複製專用帳號
mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO mstbk@ '192.168.1.103' IDENTIFIED BY '1234';
mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO mstbk@ '192.168.1.104' IDENTIFIED BY '1234';
mysql> FLUSH PRIVILEGES;

創建一個賬戶mstbk,而且只能容許從192.168.1.103和192.168.1.104這兩個地址上來登錄,密碼是1234。
(若是由於mysql版本新舊密碼算法不一樣,能夠設置:
set password for 'mstbk'@'192.168.1.103'=old_password('1234'))
node



2.4.備庫建立數據複製專用帳號
備主爲主庫建立數據複製專用帳號
mysql> GRANT REPLICATION SLAVE,RELOAD,SUPER ON *.* TO canbk@ '192.168.1.102' IDENTIFIED BY '1234';
mysql> FLUSH PRIVILEGES;


2.5.(主、備、從)執行change master操做
讓備庫、從庫鏈接主庫,並開始重作master二進制日誌中的事件。不該該用配置文件進行該操做,而應該使用CHANGE MASTER TO語句,該語句能夠徹底取代對配置文件的修改,並且它能夠爲slave指定不一樣的master,而不須要中止服務器。

1)主庫:
主庫查看當前日誌獲得file和position信息
mysql> SHOW MASTER STATUS;

2)備主:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.102',MASTER_USER='mstbk',MASTER_PASSWORD='1234',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=0;
其中MASTER_LOG_FILE和MASTER_LOG_POS是主庫查詢獲得的。

啓動同步
mysql> START SLAVE;

查看同步信息
mysql> SHOW SLAVE STATUS\G;

備庫查看當前日誌獲得file和position信息
mysql> SHOW MASTER STATUS;

3)從庫:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.102',MASTER_USER='mstbk',MASTER_PASSWORD='1234',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=0;
其中MASTER_LOG_FILE和MASTER_LOG_POS是主庫查詢獲得的。

啓動同步
mysql> START SLAVE;

查看同步信息
mysql> SHOW SLAVE STATUS\G;

4)主庫:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.103',MASTER_USER='canbk',MASTER_PASSWORD='1234',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=0;
其中MASTER_LOG_FILE和MASTER_LOG_POS是備主庫查詢獲得的。

啓動同步
mysql> START SLAVE;

查看同步信息
mysql> SHOW SLAVE STATUS\G;


注意:
查看同步信息時只有Slave_IO_Running和Slave_SQL_Running爲Yes狀態代表數據同步是正常的。


2.6.驗證
主庫:
在主庫中建立數據庫mytest
mysql> CREATE DATABASE mytest;
查看建立結果
mysql> SHOW DATABASES;

備主:
查看建立結果
mysql> SHOW DATABASES;

從庫:
查看建立結果
mysql> SHOW DATABASES;

若是在備庫和從庫中都有mytest生成,說明主從已經正常了。
mysql




3、配置SSH無密碼登陸(監控端、主、備、從)
監控端:
# ssh-keygen -t rsa
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.102
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.103
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.104


主庫:
# ssh-keygen -t rsa
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.101
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.103
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.104

備主:
# ssh-keygen -t rsa
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.101
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.102
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.104

從庫:
# ssh-keygen -t rsa
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.101
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.102
# ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.1.103

注意:
相互之間第一次登陸時須要手動輸入一次密碼,以後就能夠免密碼登陸了。



4、在(監控端、主、備、從)上安裝MHA-Node
在四個Node上安裝mha4mysql-node以及perl-DBD依賴包;
http://www.chocolee.cn/download/mha/

監控端:
#yum –y install perl-DBD-MySQL
#rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm

主庫:
#yum –y install perl-DBD-MySQL
#rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm

備主:
#yum –y install perl-DBD-MySQL
#rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm

從庫:
#yum –y install perl-DBD-MySQL
#rpm -ivh mha4mysql-node-0.54-0.el6.noarch.rpm



5、在監控端安裝MHA-MANAGER
1.安裝mha4mysql-manager及相關軟件
在http://www.chocolee.cn/download/mha/下載mha4mysql-manager

#yum –y install perl-Config-Tiny perl-Log-Dispatch perl-Parallel-ForkManager
#rpm -ivh mha4mysql-manager-0.55-0.el6.noarch.rpm

注意:
須要注意的是mha-manager和mha-node版本必須一致

2.設置MHA配置文件
#mkdir /etc/masterha/
#mkdir /var/log/masterha/app1/
#mkdir /var/log/masterha/

#vim /etc/masterha/app1.cnf
[server default]   
# mysql user and password   
user=root
password=12345678
ssh_user=root

# working directory on the manager
manager_workdir=/var/log/masterha/app1
manager_log=/var/log/masterha/app1/app1.log

# working directory on MySQL servers
remote_workdir=/var/log/masterha/app1
master_binlog_dir=/var/lib/mysql/
check_repl_delay=0
master_ip_failover_script=/etc/masterha/script/master_ip_failover
master_ip_online_change_script=/etc/masterha/script/master_ip_online_change_script


[server1]
hostname=192.168.1.102
candidate_master=1     
[server2]
hostname=192.168.1.103
candidate_master=1     
[server3]
hostname=192.168.1.104
no_master=1


3.編輯master_ip_failover和master_ip_online_change_script主備切換腳本
#mkdir /etc/masterha/script/

3.1設置自動failover時候的切換腳本
#!/usr/bin/env perl
use strict;
use warnings FATAL => 'all';
use Getopt::Long;
my (
    $command, $ssh_user, $orig_master_host, $orig_master_ip,
    $orig_master_port, $new_master_host, $new_master_ip, $new_master_port,
    $orig_master_user,$orig_master_password,$new_master_user,$new_master_password,
);
my $vip = '192.168.1.105'; # Virtual IP
my $gateway = '192.168.1.1';#Gateway IP
my $interface = 'eth0'
my $key = "1";
my $ssh_start_vip = "/sbin/ifconfig $interface:$key $vip;/sbin/arping -I $interface -c 3 -s $vip $gateway >/dev/null 2>&1";
my $ssh_stop_vip = "/sbin/ifconfig $interface:$key down";
my $ssh_user="root";

GetOptions(
    'command=s' => \$command,
    'ssh_user=s' => \$ssh_user,
    'orig_master_host=s' => \$orig_master_host,
    'orig_master_ip=s' => \$orig_master_ip,
    'orig_master_port=i' => \$orig_master_port,
    'new_master_host=s' => \$new_master_host,
    'new_master_ip=s' => \$new_master_ip,
    'new_master_port=i' => \$new_master_port,
    'orig_master_user=s' => \$orig_master_user,
    'orig_master_password=s' => \$orig_master_password,
    'new_master_user=s' => \$new_master_user,
    'new_master_password=s' => \$new_master_password,
);
exit &main();
sub main {
    print "\n\nIN SCRIPT TEST====$ssh_stop_vip==$ssh_start_vip===\n\n";
    if ( $command eq "stop" || $command eq "stopssh" ) {
        # $orig_master_host, $orig_master_ip, $orig_master_port are passed.
        # If you manage master ip address at global catalog database,
        # invalidate orig_master_ip here.
        my $exit_code = 1;
        eval {
            print "Disabling the VIP on old master: $orig_master_host \n";
            &stop_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn "Got Error: $@\n";
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "start" ) {
        # all arguments are passed.
        # If you manage master ip address at global catalog database,
        # activate new_master_ip here.
        # You can also grant write access (create user, set read_only=0, etc) here.
        my $exit_code = 10;
        eval {
            print "Enabling the VIP - $vip on the new master - $new_master_host \n";
            &start_vip();
            $exit_code = 0;
        };
        if ($@) {
            warn $@;
            exit $exit_code;
        }
        exit $exit_code;
    }
    elsif ( $command eq "status" ) {
        print "Checking the Status of the script.. OK \n";
        `ssh $ssh_user\@cluster1 \" $ssh_start_vip \"`;
        exit 0;
    }
    else {
        &usage();
        exit 1;
    }
}
# A simple system call that enable the VIP on the new master
sub start_vip() {
    `ssh $ssh_user\@$new_master_host \" $ssh_start_vip \"`;
}
# A simple system call that disable the VIP on the old_master
sub stop_vip() {
    `ssh $ssh_user\@$orig_master_host \" $ssh_stop_vip \"`;
}
sub usage {
    print
    "Usage: master_ip_failover --command=start|stop|stopssh|status --orig_master_host=host --orig_master_ip=ip --orig_master_port=port --new_master_host=host --new_master_ip=ip --new_master_port=port\n";
}


3.2設置手動切換時候的切換腳本
拷貝master_ip_failover重命名爲master_ip_online_change_script
#cd /etc/masterha/script/
#cp master_ip_failover master_ip_online_change_script

3.3設置備主爲read_only
當前是主主互備構架,且主庫工做正常,則設置candicate主機192.168.1.103以及其餘從機均爲read_only,從庫已經在my.cnf配置文件中配置了。
查看數據庫的read_only狀態
mysql> SHOW VARIABLES LIKE 'read_only';
若是未開啓(ON狀態)則開啓:
mysql> SET GLOBAL read_only=1;

3.4設置(主、備、從)relay_log_purge
關閉(主、備、從)relay_log_purge即MySQL的自動清理日誌功能;
查看數據庫是否的relay_log_purge狀態
mysql> SHOW VARIABLES LIKE 'relay_log_purge';
若是未關閉(OFF狀態)則關閉:
mysql> SET GLOBAL relay_log_purge=0;

3.5管理relay log文件
關閉relay_log_purge後必須對relay log文件進行有效的管理;使用定時任務來定時清理relay log.
編輯定時任務
#crontab -e
0 5 * * * app /usr/bin/purge_relay_logs --user=root --password=12345678 --disable_relay_log_purge >> /var/log/masterha/purge_relay_logs.log 2>&1
注意:這裏的用戶名密碼是MySQL的,若是root沒有密碼及不須要給出--password=參數項

重啓定時任務服務
#service crond restart

3.6監控端開啓MHA-MANAGER監控服務
# masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf
推薦使用後臺進程模式運營:
# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf < /dev/null > /var/log/masterha/app1/app1.log 2>&1 &



6、檢查設置
1.檢查SSH無密碼登陸
在監控執行如下命令:
#masterha_check_ssh --conf=/etc/masterha/app1.cnf
執行後的結果中出現‘All SSH connection tests passed successfully.’說明SSH無密碼登陸配置成功。

2.檢查主備從數據複製狀態
#masterha_check_repl --conf=/etc/masterha/app1.cnf
執行後將給出是否OK的結果。

3.檢查manager運營狀態:
# masterha_check_status --conf=/etc/masterha/app1.cnf

4.手動切換檢測
情景一:手工對dead master進行切換
# masterha_master_switch --master_state=dead --conf=/etc/masterha/app1.cnf --dead_master_host=192.168.1.102 --new_master_host=192.168.1.103
若是,MHA manager檢測到沒有dead的server,將報錯,並結束failover:
Thu May 17 17:53:33 2012 - [info] Dead Servers:
Thu May 17 17:53:33 2012 - [error][/usr/lib/perl5/site_perl/5.8.8/MHA/MasterFailover.pm, ln181] None of server is dead. Stop failover.

情景二:當前master在正常運行狀況的切換:linux

執行前要中止監聽(manager)服務的運行
# masterha_stop --conf=/etc/masterha/app1.cnf
而後執行切換算法

#masterha_master_switch --conf=/etc/masterha/app1.cnf --master_state=alive --new_master_host=192.168.1.103
切換後,會給出切換結果;若是成功這是備主192.168.1.103稱爲了實際上的主機,而原來的主機192.168.1.102將被隔離。

5.自動切換
關閉主庫MySQL服務後,將發生切換
注意:要保證切換且MHA是正常的,MHA-MANAGER監控服務是運行的。


7、切換後恢復雙主一從的正常模式
情景:主庫出現異常後發生了切換操做,當主庫修好後須要恢復到雙主一從的正常模式;

注意:
1)發生主備切換後,MHA-MANAGER監控服務會自動停掉,且在/var/log/masterha/app1/下面生成app1.failover.complete,若再次發生切換須要刪除app1.failover.complete文件;
2)發生主備切換後,/etc/masterha/app1.cnf中異常的服務器信息將被自動刪除,恢復時須要將被刪除的服務器信息添加到配置文件中;

恢復步驟:
1.主庫上的操做(192.168.1.102):
1.1正常啓動主庫
#service mysqld start;

1.2設置主庫爲read_only
查看數據庫的read_only狀態
mysql> SHOW VARIABLES LIKE 'read_only';
若是未開啓(ON狀態)則開啓:
mysql> SET GLOBAL read_only=1;

1.3設置主庫relay_log_purge
關閉主庫relay_log_purge即MySQL的自動清理日誌功能;
查看數據庫是否的relay_log_purge狀態
mysql> SHOW VARIABLES LIKE 'relay_log_purge';
若是未關閉(OFF狀態)則關閉:
mysql> SET GLOBAL relay_log_purge=0;

1.4啓用管理relay log文件的定時任務
編輯定時任務
#crontab -e
0 5 * * * app /usr/bin/purge_relay_logs --user=root --password=12345678 --disable_relay_log_purge >> /var/log/masterha/purge_relay_logs.log 2>&1

重啓定時任務
#service crond restart

1.5恢復主庫爲備主的從庫
主庫查看同步信息是否正常
mysql> SHOW SLAVE STATUS\G;

1.6若是沒有了同步信息,則從新創建:
1)查看備庫日誌信息:
備庫查看當前日誌獲得file和position信息
mysql> SHOW MASTER STATUS;

2)主庫(192.168.1.102):
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.103',MASTER_USER='canbk',MASTER_PASSWORD='1234',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=0;
其中MASTER_LOG_FILE和MASTER_LOG_POS是備主庫查詢獲得的。

3)啓動同步
mysql> START SLAVE;

4)查看同步信息
mysql> SHOW SLAVE STATUS\G;


2.備主上的操做(192.168.1.103):
這是的備主實際上已經成了主庫對外提供服務器了,但咱們仍然從邏輯上稱呼它備主;

2.1恢復備庫爲主的從庫
主庫查看同步信息是否正常
mysql> SHOW SLAVE STATUS\G;

2.2若是沒有了同步信息,則從新創建:
1)備主(192.168.1.103):
主庫查看當前日誌獲得file和position信息
mysql> SHOW MASTER STATUS;

2)備主:
mysql> CHANGE MASTER TO MASTER_HOST='192.168.1.102',MASTER_USER='mstbk',MASTER_PASSWORD='1234',MASTER_LOG_FILE='mysql-bin.000001',MASTER_LOG_POS=0;
其中MASTER_LOG_FILE和MASTER_LOG_POS是主庫查詢獲得的。

3)啓動同步
mysql> START SLAVE;

4)查看同步信息
mysql> SHOW SLAVE STATUS\G;

3.修復/etc/masterha/app1.cnf文件
將刪除的主庫信息加入配置文件
[server1]
hostname=192.168.1.102
candidate_master=1

4.刪除app1.failover.complete
#rm  /var/log/masterha/app1/app1.failover.complete

5.檢查主備從數據複製狀態
#masterha_check_repl --conf=/etc/masterha/app1.cnf

6.開啓MHA-MANAGER監控服務
# masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf
推薦使用後臺進程模式運營:
# nohup masterha_manager --conf=/etc/masterha/app1.cnf --remove_dead_master_conf < /dev/null > /var/log/masterha/app1/app1.log 2>&1 &


參考:
http://www.cnblogs.com/jirglt/p/3549047.html
http://www.techsiteanalytics.com/index.php/2013/08/14/6428
http://blog.chinaunix.net/uid-14010457-id-3481261.html
http://www.cnblogs.com/yuanermen/p/3726572.html
http://blog.itpub.net/88305/viewspace-730135
http://www.linuxidc.com/Linux/2014-11/109460p2.htmsql

相關文章
相關標籤/搜索