一,mysql-mha環境準備
1.1 實驗環境:
主機名 | IP地址(NAT) | 描述 |
---|---|---|
mysql-db01 | eth0:192.168.0.51 | 系統:CentOS6.5(6.x均可以) 安裝:mysql5.6 |
mysql-db02 | eth0:192.168.0.52 | 系統:CentOS6.5(6.x均可以) 安裝:mysql5.6 |
mysql-db03 | eth0:192.168.0.53 | 系統:CentOS6.5(6.x均可以) 安裝:mysql5.6 |
1.2 軟件包
1) mha管理節點安裝包:css
mha4mysql-manager-0.56-0.el6.noarch.rpmnode
mha4mysql-manager-0.56.tar.gzmysql
2) mha node節點安裝包:linux
mha4mysql-node-0.56-0.el6.noarch.rpmnginx
mha4mysql-node-0.56.tar.gzsql
3) mysql中間件:docker
Atlas-2.2.1.el6.x86_64.rpmshell
4) mysql源碼安裝包數據庫
mysql-5.6.17-linux-glibc2.5-x86_64.tarvim
1.3 主機名映射
[root@mysql-db01 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.0.51 mysql-db01 192.168.0.52 mysql-db02 192.168.0.53 mysql-db03
1.4 關閉selinux和iptables
[root@mysql-db01 ~]# vim /etc/sysconfig/selinux [root@mysql-db01 ~]# cat /etc/sysconfig/selinux | grep -v "#" SELINUX=disabled SELINUXTYPE=targeted [root@mysql-db01 ~]# setenforce 0 [root@mysql-db01 ~]# service iptables stop iptables: Setting chains to policy ACCEPT: filter [ OK ] iptables: Flushing firewall rules: [ OK ] iptables: Unloading modules: [ OK ] [root@mysql-db01 ~]# chkconfig iptables off
二,簡介
2.1 做者簡介
姓名:鬆信嘉範
MySQL/Linux專家
2001年索尼公司入職
2001年開始使用oracle
2004年開始使用MySQL
2006年9月-2010年8月MySQL從事顧問
2010年-2012年DeNA
2012年至今Facebook
2.2 軟件簡介
- MHA(Master High Availability)目前在MySQL高可用方面是一個相對成熟的解決方案,是一套優秀的做爲MySQL高可用性環境下故障切換和主從提高的高可用軟件。在MySQL故障切換過程當中,MHA能作到0~30秒以內自動完成數據庫的故障切換操做,而且在進行故障切換過程當中,MHA能最大程度上保證數據庫的一致性,以達到真正意義上的高可用。
- MHA由兩部分組成:MHA Manager(管理節點)和MHA Node(數據節點)。MHA Manager能夠獨立部署在一臺獨立的機器上管理多個Master-Slave集羣,也能夠部署在一臺Slave上。當Master出現故障時,它能夠自動將最新數據的Slave提高爲新的Master,而後將全部其餘的Slave從新指向新的Master。整個故障轉移過程對應程序是徹底透明的。
2.3 工做流程
- 從宕機崩潰的master保存二進制日誌事件(binlog events);
- 識別含有最新更新的slave;
- 應用差別的中繼日誌(relay log)到其餘的slave;
- 應用從master保存的二進制日誌事件(binlog events);
- 提高一個slave爲新的master;
- 使其餘的slave鏈接新的master進行復制;
2.4 MHA架構圖
2.5 MHA工具介紹
MHA軟件由兩部分組成,Manager工具包和Node工具包,具體的說明以下:
#Manager工具包主要包括如下幾個工具: masterha_check_ssh #檢查MHA的SSH配置情況 masterha_check_repl #檢查MySQL複製情況 masterha_check_status #檢測當前MHA運行狀態 masterha_master_monitor #檢測master是否宕機 masterha_manger #啓動MHA masterha_master_switch #控制故障轉移(自動或者手動) masterha_conf_host #添加或刪除配置的server信息 masterha_secondary_check #試圖創建TCP鏈接從遠程服務器 masterha_stop #中止MHA #Node工具包主要包括如下幾個工具: save_binary_logs #保存和複製master的二進制日誌 apply_diff_relay_logs #識別差別的中繼日誌事件 filter_mysqlbinlog #去除沒必要要的ROLLBACK事件 purge_relay_logs #清除中繼日誌
三,mysql環境準備
3.1 環境檢查
- mysql-db01
#系統版本 [root@mysql-db01 bin]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@mysql-db01 bin]# uname -r 2.6.32-431.el6.x86_64 [root@mysql-db01 bin]# hostname -I 192.168.0.51
- mysql-db02
#系統版本 [root@mysql-db02 ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@mysql-db02 ~]# uname -r 2.6.32-431.el6.x86_64 [root@mysql-db02 ~]# hostname -I 192.168.0.52
- mysql-db03
#系統版本 [root@mysql-db03 ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@mysql-db03 ~]# uname -r 2.6.32-431.el6.x86_64 [root@mysql-db03 ~]# hostname -I 192.168.0.53
3.2 安裝mysql
3.2.1 安裝包準備
[root@mysql-db01 ~]# ls anaconda-ks.cfg mha4mysql-manager-0.56.tar.gz rpm install.log mha4mysql-node-0.56.tar.gz install.log.syslog mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz [root@mysql-db01 ~]# ll mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz -rw-r--r--. 1 root root 305102088 Sep 3 21:33 mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz
3.2.2 安裝(3臺都裝)
[root@mysql-db01 ~]# yum -y install ncurses-devel [root@mysql-db01 ~]# yum -y install libaio [root@mysql-db01 ~]# tar xf mysql-5.6.17-linux-glibc2.5-x86_64.tar.gz -C /usr/local/ [root@mysql-db01 ~]# ln -s /usr/local/mysql-5.6.17-linux-glibc2.5-x86_64 /usr/local/mysql [root@mysql-db01 ~]# useradd mysql -s /sbin/nologin -M [root@mysql-db01 ~]# /usr/local/mysql/scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data/ [root@mysql-db01 ~]# /bin/cp /usr/local/mysql/support-files/my-default.cnf /etc/my.cnf [root@mysql-db01 ~]# /bin/cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld [root@mysql-db01 ~]# ln -s /usr/local/mysql/bin/* /usr/local/bin/ [root@mysql-db01 ~]# which mysqladmin /usr/local/bin/mysqladmin
3.2.3 加入開機自啓動並啓動mysql
[root@mysql-db01 ~]# chkconfig mysqld on [root@mysql-db01 ~]# chkconfig mysqld --list mysqld 0:off 1:off 2:on 3:on 4:on 5:on 6:off [root@mysql-db01 ~]# /etc/init.d/mysqld start Starting MySQL. SUCCESS!
3.2.4 配置密碼
[root@mysql-db01 ~]# mysqladmin -uroot password '123123'
四,配置基於GTID的主從複製
4.1 先決條件
- 主庫和從庫都要開啓binlog
- 主庫和從庫server-id不一樣
- 要有主從複製用戶
4.2 主庫操做(mysql-db01)
4.2.1 修改配置文件
#修改主庫配置文件/etc/my.cnf [root@mysql-db01 mysql]# cat /etc/my.cnf [client] socket = /usr/local/mysql/data/mysql.sock [mysqld] lower_case_table_names = 1 default-storage-engine = InnoDB port = 3306 datadir = /usr/local/mysql/data character-set-server = utf8 socket = /usr/local/mysql/data/mysql.sock log_bin = mysql-bin #開啓binlog日誌 server_id = 1 #設置server_id innodb_buffer_pool_size = 200M slave-parallel-workers = 8 thread_cache_size = 600 back_log = 600 slave_net_timeout = 60 max_binlog_size = 512M key_buffer_size = 8M query_cache_size = 64M join_buffer_size = 2M sort_buffer_size = 2M query_cache_type = 1 thread_stack = 192K #重啓動MySQL服務 [root@mysql-db01 mysql]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
4.2.2 登錄MySQL刪除沒必要要的用戶並建立主從複製用戶
1)刪除沒必要要的用戶
mysql> select user,host from mysql.user; +------+------------+ | user | host | +------+------------+ | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | | | mysql-db01 | | root | mysql-db01 | +------+------------+ 6 rows in set (0.00 sec) mysql> drop user root@'127.0.0.1'; Query OK, 0 rows affected (0.00 sec) mysql> drop user root@'::1'; Query OK, 0 rows affected (0.00 sec) mysql> drop user ' '@'localhost'; Query OK, 0 rows affected (0.00 sec) mysql> drop user ' '@'mysql-db01'; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +------+------------+ | user | host | +------+------------+ | root | localhost | | root | mysql-db01 | +------+------------+ 2 rows in set (0.00 sec)
2)建立主從複製用戶
mysql> grant replication slave on *.* to rep@'192.168.0.%' identified by '123123'; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user; +------+-------------+ | user | host | +------+-------------+ | rep | 192.168.0.% | | root | localhost | | root | mysql-db01 | +------+-------------+ 3 rows in set (0.00 sec) mysql> show grants for rep@'192.168.0.%'; +--------------------------------------------------------------------------------------------------------------------------+ | Grants for rep@192.168.0.% | +--------------------------------------------------------------------------------------------------------------------------+ | GRANT REPLICATION SLAVE ON *.* TO 'rep'@'192.168.0.%' IDENTIFIED BY PASSWORD '*E56A114692FE0DE073F9A1DD68A00EEB9703F3F1' | +--------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec)
4.3 從庫操做(mysql-db02和mysql-db03)
4.3.1 修改配置文件
#修改mysql-db02配置文件(和mysql-db01配置文件一致) #只須要修改server-id = 5選項 [root@mysql-db02 ~]# cat /etc/my.cnf [client] socket = /usr/local/mysql/data/mysql.sock [mysqld] lower_case_table_names = 1 default-storage-engine = InnoDB port = 3306 datadir = /usr/local/mysql/data character-set-server = utf8 socket = /usr/local/mysql/data/mysql.sock log_bin = mysql-bin #從binlog也要打開 server_id = 5 #僅需修改此項 innodb_buffer_pool_size = 200M slave-parallel-workers = 8 thread_cache_size = 600 back_log = 600 slave_net_timeout = 60 max_binlog_size = 512M key_buffer_size = 8M query_cache_size = 64M join_buffer_size = 2M sort_buffer_size = 2M query_cache_type = 1 thread_stack = 192K [root@mysql-db02 ~]# /etc/init.d/mysqld restart #重啓mysql Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! #修改mysql-db03配置文件(和mysql-db01配置文件一致) #只須要修改server-id = 10選項 [root@mysql-db03 ~]# cat /etc/my.cnf [client] socket = /usr/local/mysql/data/mysql.sock [mysqld] lower_case_table_names = 1 default-storage-engine = InnoDB port = 3306 datadir = /usr/local/mysql/data character-set-server = utf8 socket = /usr/local/mysql/data/mysql.sock log_bin = mysql-bin #從binlog也要打開 server_id = 10 #只需修改此項 innodb_buffer_pool_size = 200M slave-parallel-workers = 8 thread_cache_size = 600 back_log = 600 slave_net_timeout = 60 max_binlog_size = 512M key_buffer_size = 8M query_cache_size = 64M join_buffer_size = 2M sort_buffer_size = 2M query_cache_type = 1 thread_stack = 192K [root@mysql-db03 ~]# /etc/init.d/mysqld restart #重啓mysql Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
特別提示:
在以往若是是基於binlog日誌的主從複製,則必需要記住主庫的master狀態信息。
可是在MySQL5.6版本里多了一個Gtid的功能,能夠自動記錄主從複製位置點的信息,並在日誌中輸出出來。
4.4 開啓GTID
#沒開啓以前先看一下GTID狀態 mysql> show global variables like '%gtid%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | enforce_gtid_consistency | OFF | | gtid_executed | | | gtid_mode | OFF | | gtid_owned | | | gtid_purged | | +--------------------------+-------+ 5 rows in set (0.00 sec)
編輯mysql配置文件(主庫從庫都須要修改)
mysql-db01,mysql-db02,mysql-db03都須要加入上圖的上行代碼
修改完配置文件之後重啓動數據庫
[root@mysql-db01 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@mysql-db02 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! [root@mysql-db03 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
再次查看GTID狀態
[root@mysql-db01 ~]# mysql -uroot -p123123
Warning: Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 1 Server version: 5.6.17-log MySQL Community Server (GPL) Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> show global variables like '%gtid%'; +--------------------------+-------+ | Variable_name | Value | +--------------------------+-------+ | enforce_gtid_consistency | ON | #執行GTID一致 | gtid_executed | | | gtid_mode | ON | #開啓GTID模塊 | gtid_owned | | | gtid_purged | | +--------------------------+-------+ 5 rows in set (0.00 sec) mysql>
再次提示:
主庫從庫都必需要開啓GTID,不然在作主從複製的時候就會報錯.
4.5 配置主從複製(mysql-db02,mysql-db03)
mysql> change master to \
-> master_host='192.168.0.51',\ #主庫IP -> master_user='rep',\ #主庫複製用戶 -> master_password='123123',\ #主庫複製用密碼 -> master_auto_position=1; #GTID位置點(自動追蹤須要同步的position) Query OK, 0 rows affected, 2 warnings (0.00 sec)
4.6 開啓從庫的主從複製功能(mysql-db02,mysql-db03)
mysql> start slave; #開啓主從同步功能 Query OK, 0 rows affected, 1 warning (0.01 sec) mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.51 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 151 Relay_Log_File: mysql-db02-relay-bin.000002 Relay_Log_Pos: 361 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes #此項yes表明成功 Slave_SQL_Running: Yes #此項yes表明成功 Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: #....如下省略若干行....
兩個從庫mysql-db02和mysql-db03都執行以上步驟。
4.7 什麼是GTID
- GTID(Global Transaction)全局事務標識符:是一個惟一的標識符,它建立並與源服務器(主)上提交的每一個事務相關聯。此標識符不只對其發起的服務器是惟一的,並且在給定複製設置中的全部服務器上都是惟一的。全部交易和全部GTID之間都有1對1的映射。
- GTID其實是由UUID+TID組成的。其中UUID是一個MySQL實例的惟一標識。TID表明了該實例上已經提交的事務數量,而且隨着事務提交單調遞增。
- 下面是一個GTID的具體形式:
3E11FA47-71CA-11E1-9E33-C80AA9429562:23
4.8 GTID的新特性
(1)支持多線程複製:事實上是針對每一個database開啓相應的獨立線程,即每一個庫有一個單獨的(sql thread)
(2)支持啓用GTID,在配置主從複製,傳統的方式裏,你須要找到binlog和POS點,而後change master to 指向。在mysql5.6裏,無須再知道binlog和POS點,只須要知道master的IP/端口/帳號密碼便可,由於同步複製是自動的,MySQL經過內部機制GTID自動找點同步。
(3)基於Row複製只保存改變的列,大大節省磁盤空間,網絡,內存等
(4)支持把Master和Slave的相關信息記錄在Table中;原來是記錄在文件裏,如今則記錄在表裏,加強可用性
(5)支持延遲複製
4.9 開啓方法
#mysql配置文件: [mysqld] gtid_mode=ON enforce_gtid_consistency #查看 show global variables like ‘%gtid%’;
4.10 從庫設置(mysql-db02,mysql-db03)
#登錄從庫 [root@mysql-db02 ~]# mysql -uroot -p123123 #臨時禁用自動刪除relay log功能 mysql> set global relay_log_purge = 0; Query OK, 0 rows affected (0.00 sec) #設置只讀 mysql> set global read_only=1; Query OK, 0 rows affected (0.00 sec)
編輯配置文件/etc/my.cnf
修改完配置文件,別忘了重啓動mysql服務
root@mysql-db02 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS! root@mysql-db03 ~]# /etc/init.d/mysqld restart Shutting down MySQL.. SUCCESS! Starting MySQL. SUCCESS!
五,部署MHA
5.1 環境準備(全部節點mysql-db01,mysql-db02,mysql-db03)
#光盤安裝依賴包 [root@mysql-db01 ~]# yum -y install perl-DBD-MySQL #安裝mha4mysql-node-0.56-0.el6.noarch.rpm [root@mysql-db01 rpm]# rpm -ivh mha4mysql-node-0.56-0.el6.noarch.rpm Preparing... ########################################### [100%] 1:mha4mysql-node ########################################### [100%] [root@mysql-db01 ~]# mysql -uroot -p123123 mysql> grant all privileges on *.* to mha@'192.168.0.%' identified by '123123'; Query OK, 0 rows affected (0.00 sec) mysql> select user,host from mysql.user where user='mha'; +------+-------------+ | user | host | +------+-------------+ | mha | 192.168.0.% | #主庫上建立從庫會自動複製 +------+-------------+ 1 row in set (0.00 sec) #特別提示:3臺MySQL都須要安裝mha4mysql-node-0.56-0.el6.noarch.rpm
5.2 部署管理節點(mha-manager)
5.2.1 在mysql-db03上部署管理節點
#使用阿里雲源+epel源 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo wget -O /etc/yum.repos.d/epel-6.repo http://mirrors.aliyun.com/repo/epel-6.repo #安裝manager依賴包(須要公網源) [root@mysql-db03 ~]# yum -y install perl-Config-Tiny epel-release perl-Log-Dispatch perl-Parallel-ForkManager perl-Time-HiRes #安裝manager包 [root@mysql-db03 rpm]# rpm -ivh mha4mysql-manager-0.56-0.el6.noarch.rpm Preparing... ########################################### [100%] 1:mha4mysql-manager ########################################### [100%]
5.2.2 編輯配置文件
#建立配置文件目錄 [root@mysql-db03 ~]# mkdir -p /etc/mha #建立日誌目錄 [root@mysql-db03 ~]# mkdir -p /var/log/mha/mha1 #建立配置文件(默認沒有) [root@mysql-db03 ~]# cd /etc/mha/ [root@mysql-db03 mha]# ls [root@mysql-db03 mha]# vim /etc/mha/mha1.cnf [root@mysql-db03 mha]# cat /etc/mha/mha1.cnf [server default] manager_log=/var/log/mha/mha1/manager #manager管理日誌存放路徑 manager_workdir=/var/log/mha/mha1 #manager管理日誌的目錄路徑 master_binlog_dir=/usr/local/mysql/data #binlog日誌的存放路徑 user=mha #管理帳戶 password=123123 #管理帳戶密碼 ping_interval=2 #存活檢查的間隔時間 repl_user=rep #主從複製的受權帳戶 repl_password=123123 #主從複製的受權帳戶密碼 ssh_user=root #用於ssh鏈接的帳戶 [server1] hostname=192.168.0.51 port=3306 [server2] #candidate_master=1 #此條暫時註釋掉(後面解釋) #check_repl_delay=0 #此條暫時註釋掉(後面解釋) hostname=192.168.0.52 port=3306 [server3] hostname=192.168.0.53 port=3306 #**特別提示:** #以上配置文件內容裏每行的最後不要留有空格,所以,不能複製的呦
特別說明:
參數:candidate_master=1
解釋:設置爲候選master,若是設置該參數之後,發生主從切換之後會將此從庫提高爲主庫,即便這個主庫不是集羣中事件最新的slave
參數:check_repl_delay=0
解釋:默認狀況下若是一個slave落後master 100M的relay logs 的話,MHA將不會選擇該slave做爲一個新的master,由於對於這個slave的恢復須要花費很長時間,經過設置check_repl_delay=0,MHA觸發切換在選擇一個新的master的時候將會忽略複製延時,這個參數對於設置了candidate_master=1的主機很是有用,由於這個候選主在切換的過程當中必定是新的master
5.3 配置ssh信任(全部節點mysql-db01,mysql-db02,mysql-db03)
#建立密鑰對 [root@mysql-db03 ~]# ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa >/dev/null 2>&1 #發送mysql-db03公鑰,包括本身 [root@mysql-db03 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.0.51 [root@mysql-db03 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.0.52 [root@mysql-db03 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.0.53 #發送mysql-db02公鑰,包括本身 [root@mysql-db02 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.0.51 [root@mysql-db02 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.0.52 [root@mysql-db02 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.0.53 #發送mysql-db01公鑰,包括本身 [root@mysql-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.0.51 [root@mysql-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.0.52 [root@mysql-db01 ~]# ssh-copy-id -i /root/.ssh/id_dsa.pub root@192.168.0.53
5.4 啓動測試
5.4.1 ssh檢查檢測
[root@mysql-db03 ~]# masterha_check_ssh --conf=/etc/mha/mha1.cnf #ssh檢查命令 Tue Sep 5 03:01:38 2017 - [warning] Global configuration file /etc/masterha_default.cnf not found. Skipping. Tue Sep 5 03:01:38 2017 - [info] Reading application default configuration from /etc/mha/mha1.cnf.. Tue Sep 5 03:01:38 2017 - [info] Reading server configuration from /etc/mha/mha1.cnf.. Tue Sep 5 03:01:38 2017 - [info] Starting SSH connection tests.. ..中間省略若干行.. Tue Sep 5 03:01:40 2017 - [debug] Connecting via SSH from root@192.168.0.53(192.168.0.53:22) to root@192.168.0.52(192.168.0.52:22).. Tue Sep 5 03:01:40 2017 - [debug] ok. Tue Sep 5 03:01:40 2017 - [info] All SSH connection tests passed successfully. #出現這個就表示成功
5.4.2 主從複製檢測
(1)錯誤的主從複製檢測
[root@mysql-db03 ~]# masterha_check_repl --conf=/etc/mha/mha1.cnf
若是不出意外,同窗們的檢測結果都會是下面的樣子
所以在mysql-db02和mysql-db03上添加主從複製的用戶便可。
grant replication slave on *.* to rep@'192.168.0.%' identified by '123123';
再次檢查以下圖所示:
5.5 啓動MHA
#啓動 [root@mysql-db03 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 & [root@mysql-db03 ~]# ps -ef | grep perl | grep -v grep root 4961 4690 0 06:33 pts/2 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover #說明: nohup:啓動命令 --conf:指定配置文件位置 --remove_dead_master_conf:若是有master down了,就去掉配置文件裏該master的部分。
5.6 進行mha自動切換master的測試
初始狀態:
(1)登錄mysql-db02(192.168.0.53)查看信息狀態
#登錄數據庫mysql-db02(192.168.0.53) [root@mysql-db03 ~]# mysql -uroot -p123123 mysql> show slave status\G *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.51 #這是主庫IP地址 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000003 Read_Master_Log_Pos: 1656 Relay_Log_File: mysql-db02-relay-bin.000004 Relay_Log_Pos: 1796 Relay_Master_Log_File: mysql-bin.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes ..如下省略若干內容..
(2)停掉mysql-db01(192.168.0.51)上的MySQL服務
[root@mysql-db01 ~]# /etc/init.d/mysqld stop Shutting down MySQL..... SUCCESS!
(3)查看mysql-db03上的MySQL從庫同步狀態
[root@mysql-db03 ~]# mysql -uroot -p123123 -e 'show slave status\G' Warning: Using a password on the command line interface can be insecure. *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.52 #如今的主庫IP Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000006 #binlog日誌 Read_Master_Log_Pos: 777 #binlog日誌位置 Relay_Log_File: mysql-db03-relay-bin.000002 Relay_Log_Pos: 408 Relay_Master_Log_File: mysql-bin.000006 Slave_IO_Running: Yes Slave_SQL_Running: Yes ..如下省略若干內容..
(4)查看mysql-db02上的MySQL,主庫同步狀態。
(5)查看mysql-db03上的mha進程狀態
[root@mysql-db03 ~]# ps -ef | grep perl | grep -v grep #查詢發現mha進程已經沒了 [root@mysql-db03 ~]#
(6)查看mha配置文件信息
說明:
看成爲主庫的mysql-db01上的MySQL宕機之後,mha經過檢測發現mysql-db01宕機,那麼會將binlog日誌最全的從庫馬上提高爲主庫,而其餘的從庫會指向新的主庫進行再次同步。
此處須要進行簡單的mha日誌記錄的講解:/var/log/mha/mha1/manager
5.7 進行mha的故障還原測試
因爲mysql-db01的MySQL服務宕機,所以mha將mysql-db02提高爲了主庫。所以,咱們須要將宕機的mysql-db01的MySQL服務啓動,而後做爲主庫mysql-db02的從庫。
初始狀態:
(1)將故障宕機的mysql-db01的MySQL服務啓動並受權進行從同步
[root@mysql-db01 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@mysql-db01 ~]# mysql -uroot -p123123
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.52', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='123123'; mysql> start slave; mysql> show slave status\G #查看從同步狀態
(2)將mha配置文件裏缺失的部分補全
[root@mysql-db03 ~]# cat /etc/mha/mha1.cnf [server default] manager_log=/var/log/mha/mha1/manager manager_workdir=/var/log/mha/mha1 master_binlog_dir=/usr/local/mysql/data password=123123 ping_interval=2 repl_password=123123 repl_user=rep ssh_user=root user=mha [server1] hostname=192.168.0.51 port=3306 [server2] hostname=12.168.0.52 port=3306 [server3] hostname=192.168.0.53 port=3306
(3)啓動mha進程
[root@mysql-db03 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 & [root@mysql-db03 ~]# ps -ef | grep perl | grep -v grep root 5226 4690 0 09:42 pts/2 00:00:00 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover
(4)停掉mysql-db02上的MySQL服務
[root@mysql-db02 ~]# /etc/init.d/mysqld stop Shutting down MySQL..... SUCCESS!
(5)查看mysql-db03上的主從同步狀態:
[root@mysql-db03 ~]# mysql -uroot -p123123 -e 'show slave status\G' Warning: Using a password on the command line interface can be insecure. *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 192.168.0.51 #此時的主庫IP切換回了mysql-db01 Master_User: rep Master_Port: 3306 Connect_Retry: 60 Master_Log_File: mysql-bin.000005 Read_Master_Log_Pos: 231 Relay_Log_File: mysql-db03-relay-bin.000002 Relay_Log_Pos: 361 Relay_Master_Log_File: mysql-bin.000005 Slave_IO_Running: Yes Slave_SQL_Running: Yes ..如下省略若干行..
(6)啓動mysql-db02上的MySQL服務
[root@mysql-db02 ~]# /etc/init.d/mysqld start
Starting MySQL. SUCCESS!
[root@mysql-db02 ~]# mysql -uroot -p123123
mysql> CHANGE MASTER TO MASTER_HOST='192.168.0.51', MASTER_PORT=3306, MASTER_AUTO_POSITION=1, MASTER_USER='rep', MASTER_PASSWORD='123123'; mysql> start slave; mysql> show slave status\G #查看從同步狀態
(7)再次補全mha配置文件後,啓動mha進程
[root@mysql-db03 ~]# cat /etc/mha/mha1.cnf [server default] manager_log=/var/log/mha/mha1/manager manager_workdir=/var/log/mha/mha1 master_binlog_dir=/usr/local/mysql/data password=123123 ping_interval=2 repl_password=123123 repl_user=rep ssh_user=root user=mha [server1] hostname=192.168.0.51 port=3306 [server2] hostname=192.168.0.52 port=3306 [server3] #andidate_master=1 #check_repl_delay=0 hostname=192.168.0.53 port=3306 [root@mysql-db03 ~]# nohup masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover < /dev/null > /var/log/mha/mha1/manager.log 2>&1 & [root@mysql-db03 ~]# ps -ef | grep perl | grep -v grep root 5226 4690 0 09:42 pts/2 00:00:01 perl /usr/bin/masterha_manager --conf=/etc/mha/mha1.cnf --remove_dead_master_conf --ignore_last_failover
此時的初始狀態還原爲下圖:
六,MHA參數驗證明踐(同窗們操做)
mha配置文件內容以下:
附錄:源碼安裝mha的方法
node節點的源碼安裝方法:
[root@mysql-db01 ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Params-Validate perl-CPAN perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker [root@mysql-db01 ~]# tar xf mha4mysql-node-0.56.tar.gz -C /usr/src/ [root@mysql-db01 ~]# cd /usr/src/mha4mysql-node-0.56/ [root@mysql-db01 mha4mysql-node-0.56]# perl Makefile.PL [root@mysql-db01 mha4mysql-node-0.56]# make && make install
manager節點的源碼安裝方法:
[root@mysql-db01 ~]# yum -y install perl-DBD-MySQL perl-Config-Tiny perl-Params-Validate perl-CPAN perl-devel perl-ExtUtils-CBuilder perl-ExtUtils-MakeMaker [root@mysql-db01 ~]# tar xf mha4mysql-manager-0.56.tar.gz -C /usr/src/ [root@mysql-db01 ~]# cd /usr/src/mha4mysql-manager-0.56/ [root@mysql-db01 mha4mysql-manager-0.56]# perl Makefile.PL [root@mysql-db01 mha4mysql-manager-0.56]# make && make install