首先在搭建前分析,想用keepalived實現mysql高可用,那麼先的對keepalived+lvs跟mysql主主複製有所瞭解。由於mysql+keepalived架構是在keepalived+lvs跟mysql主主複製的基礎上實現的lvs+keepalived跟mysql主主複製,在前面的博文中已經介紹過了這裏不囉嗦了。mysql
lvs+keepalived連接:http://duyunlong.blog.51cto.com/1054716/1118447,linux
mysql主主複製連接:http://duyunlong.blog.51cto.com/1054716/1306841sql
咱們的目標是,兩臺mysql服務器若是其中有一臺mysql服務器掛掉後,另一臺能立馬接替其進行工做。所以咱們就必須保證兩臺mysql數據庫的數據徹底同樣,並且當掛掉的那一臺從新啓動的話,再也不會被客戶端繼被訪問,而是會充當備機跟如今工做的mysql進行數據同步,一直到提供服務的那臺掛掉後再接替其工做。如此周而復始的實現了mysql的高可用。在一般狀況下實現這種模式的keepalived無疑是最好的選擇。由於有虛擬IP的緣由,若是有一臺mysql掛掉了,keepalived會從服務器羣中剔除,而客戶端訪問會被切換到另一臺接替其工做的機器上。數據庫
在搭建的時候一些注意事項,mysql主主複製跟lvs+keepalived搭建的注意事情前面博文已經提升這裏就再也不囉嗦。在搭建完畢後,由於arp的緣由不可以實現自由切換。這就是要注意的重點。vim
vim /etc/sysctl.conf #.............前面部分省去 #######如下是文件末尾添加的部分###### net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2 ##這4行的主要做用是本地arp不做迴應##
添加完後保存退出,並執行:sysctl -p使其生效
bash
[root@mysql-ha1 ~]# sysctl -p net.ipv4.ip_forward = 0 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296 net.ipv4.conf.all.send_redirects = 0 net.ipv4.conf.default.send_redirects = 0 net.ipv4.conf.eth0.send_redirects = 0 net.ipv4.conf.eth0.send_redirects = 0 net.ipv4.conf.all.arp_ignore = 1 net.ipv4.conf.all.arp_announce = 2 net.ipv4.conf.lo.arp_ignore = 1 net.ipv4.conf.lo.arp_announce = 2
下面具體操做,首先有兩臺mysql機器已經搭建好了mysql的主主複製服務器
vip:192.168.5.55架構
mysql-ha1:192.168.5.234ssh
mysql-ha2:192.168.5.155tcp
在兩臺mysql機器上都安裝ipvsadm
,keepalived,首先在mysql-ha1(192.168.5.234)安裝
[root@mysql-ha1 src]# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz [root@mysql-ha1 src]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.26.tar.gz [root@mysql-ha1 src]# yum -y install kernel-devel make gcc openssl-devel libnl* popt* [root@mysql-ha1 src]# ln -s /usr/src/kernels/2.6.32-220.13.1.el6.x86_64/ /usr/src/linux [root@mysql-ha1 src]# tar -zxvf ipvsadm-1.26.tar.gz [root@mysql-ha1 src]# cd ipvsadm-1.26/ [root@mysql-ha1 ipvsadm-1.26]# make [root@mysql-ha1 ipvsadm-1.26]# make install [root@mysql-ha1 ipvsadm-1.26]# cd .. [root@mysql-ha1 src]# tar -zxvf keepalived-1.2.2.tar.gz [root@mysql-ha1 src]# cd keepalived-1.2.2/ [root@mysql-ha1 keepalived-1.2.2]# ./configure --with-kernel-dir=/usr/src/kernels/2.6.32-358.2.1.el6.x86_64/ [root@mysql-ha1 keepalived-1.2.2]# make && make install [root@mysql-ha1 keepalived-1.2.2]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ [root@mysql-ha1 keepalived-1.2.2]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ [root@mysql-ha1 keepalived-1.2.2]# mkdir /etc/keepalived [root@mysql-ha1 keepalived-1.2.2]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ [root@mysql-ha1 keepalived-1.2.2]# cp /usr/local/sbin/keepalived /usr/sbin/
在mysql-ha2(192.168.5.155)也一樣進行安裝
而後咱們編輯keepalived配置文件,下面是mysql-ha1(192.168.5.234)上的配置文件內容
[root@mysql-ha1 ~]# >/etc/keepalived/keepalived.conf [root@mysql-ha1 ~]# vim /etc/keepalived/keepalived.conf #! Configuration File for keepalived global_defs { router_id mysql-ha1 #修改成本身的主機名 } ##################第一部分################### vrrp_instance VI_1 { state BACKUP #都修改爲BACKUP interface eth0 virtual_router_id 60 #默認51 主從都修改成60 priority 100 #在mysql-ha2上LVS上修改爲80 advert_int 1 nopreempt #不搶佔資源,意思就是它活了以後也不會再把主搶回來 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.5.55 } } ##################第二部分################### virtual_server 192.168.5.55 3306 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.5.234 3306 { weight 1 notify_down /usr/local/mysql/bin/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 connect_port 3306 } } }
這裏須要注意的是,notify_down
/usr/local/mysql/bin/mysql
.sh
這個選項,這個是是在keepalived檢測不到mysql的時候要執行的腳本,從上面的配置文件來看real服務器只有本機。那麼,keeaplived若是啓動,客戶端也只是訪問本機的mysql。nopreempt
這個選下也得注意,這個是不搶佔資源在優先級高的機器上配置就能夠。
看下這個腳本的內容:
[root@mysql-ha1 ~]# vim /usr/local/mysql/bin/mysql.sh #!/bin/bash pkill keepalived [root@mysql-ha1 ~]# chmod +x /usr/local/mysql/bin/mysql.sh
腳本內容就一條命令:pkill keepalived,主要做用是若是本機的mysql掛掉了,那麼同時會殺死本機的keepalived,這樣另一臺就會接替他工做,虛擬IP也會被另外一臺接管,若是不殺死keepalived虛擬IP不會被另外一臺接管,mysql訪問也就不會切換過去。
測試下若是,mysql服務器掛掉,腳本是否能夠殺死keepalived
[root@mysql-ha1 ~]# ps aux | grep keepalived root 2518 0.0 0.0 41796 936 ? Ss 19:35 0:00 keepalived -D root 2519 0.0 0.1 43900 2152 ? S 19:35 0:00 keepalived -D root 2520 0.0 0.0 43900 1572 ? S 19:35 0:00 keepalived -D root 2610 0.0 0.0 103240 860 pts/0 S+ 23:07 0:00 grep keepalived [root@mysql-ha1 ~]# netstat -ntlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2237/mysqld tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1041/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1429/qpidd tcp 0 0 :::22 :::* LISTEN 1041/sshd tcp 0 0 ::1:25 :::* LISTEN 1416/master [root@mysql-ha1 ~]# service mysqld stop Shutting down MySQL... SUCCESS! [root@mysql-ha1 ~]# ps aux | grep keepalived root 2640 0.0 0.0 103240 856 pts/0 S+ 23:07 0:00 grep keepalived [root@mysql-ha1 ~]# netstat -ntlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1041/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1429/qpidd tcp 0 0 :::22 :::* LISTEN 1041/sshd tcp 0 0 ::1:25 :::* LISTEN 1416/master [root@mysql-ha1 ~]#
能夠看到當mysqld服務器掛掉後,腳本生效同時殺死本機keepalived使得mysql故障轉移。這時另外一臺keepalived會接替它工做。
一樣看下mysql-ha2(192.168.5.155)上keepalived配置文件
[root@mysql-ha2 ~]# vim /etc/keepalived/keepalived.conf #! Configuration File for keepalived global_defs { router_id mysql-ha1 #修改成本身的主機名 } ##################第一部分################### vrrp_instance VI_1 { state BACKUP #都修改爲BACKUP interface eth1 virtual_router_id 60 #默認51 主從都修改成60 priority 80 #在mysql-ha1上LVS上修改爲100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.5.55 } } ##################第二部分################### virtual_server 192.168.5.55 3306 { delay_loop 6 lb_algo wrr lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP real_server 192.168.5.155 3306 { weight 1 notify_down /usr/local/mysql/bin/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 connect_port 3306 } } }
配置完畢後能夠查看下keealived有沒有檢測到本機的mysql。(啓動的時候先啓動mysql在啓動keepalived,要不mysql沒起來啓動keepalived會被腳本殺死。)
先看下mysql-ha1(192.168.5.234)
[root@mysql-ha1 ~]# service mysqld start Starting MySQL.. SUCCESS! [root@mysql-ha1 ~]# /etc/init.d/keepalived start 正在啓動 keepalived: [root@mysql-ha1 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.5.55:mysql wrr persistent 50 -> 192.168.5.234:mysql Local 1 0 0
而後再看下mysql-ha1(192.168.5.155)
[root@mysql-ha2 ~]# service mysqld start Starting MySQL.. SUCCESS! [root@mysql-ha2 ~]# /etc/init.d/keepalived start 正在啓動 keepalived: [肯定] [root@mysql-ha2 ~]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.5.55:3306 wrr persistent 50 -> 192.168.5.155:3306 Local 1 0 0
測試只開啓mysql-ha1(192.168.5.234)mysql服務跟keepalived用客戶端連接虛擬ip
[root@mysql-ha1 ~]# ps aux | grep keepalived root 2977 0.0 0.0 41796 936 ? Ss 23:14 0:00 keepalived -D root 2978 0.0 0.1 43900 2156 ? S 23:14 0:00 keepalived -D root 2979 0.0 0.0 43900 1576 ? S 23:14 0:00 keepalived -D root 2982 0.0 0.0 103240 860 pts/0 S+ 23:16 0:00 grep keepalived [root@mysql-ha1 ~]# netstat -ntlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 2917/mysqld tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1041/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1416/master tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1429/qpidd tcp 0 0 :::22 :::* LISTEN 1041/sshd tcp 0 0 ::1:25 :::* LISTEN 1416/master
mysql-ha2(192.168.5.155)
[root@mysql-ha2 ~]# ps aux | grep keepalived root 10576 0.0 0.0 5980 756 pts/4 S+ 15:24 0:00 grep keepalived [root@mysql-ha2 ~]# netstat -ntlp Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 4776/sshd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1101/master tcp 0 0 0.0.0.0:5672 0.0.0.0:* LISTEN 1114/qpidd tcp 0 0 :::22 :::* LISTEN 4776/sshd tcp 0 0 ::1:25 :::* LISTEN 1101/master
而後用客戶端:192.168.5.10鏈接
C:\Users\Administrator>mysql -uduyunlong -p123456 -h192.168.5.55 Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 33 Server version: 5.5.22-log Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the buffer. mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.02 sec) mysql>
而後把mysql-ha1(192.168.5.234)mysql服務關掉,開啓mysql-ha2(192.168.5.155)上的mysql服務跟keepalived。而後不退出mysql,繼續查看數據庫:
mysql-ha1(192.168.5.234)
[root@mysql-ha1 ~]# service mysqld stop Shutting down MySQL... SUCCESS!
mysql-ha2(192.168.5.155)
[root@mysql-ha2 ~]# service mysqld start Starting MySQL.. SUCCESS! [root@mysql-ha2 ~]# /etc/init.d/keepalived start 正在啓動 keepalived: [肯定] [root@mysql-ha2 ~]#
客戶端:192.168.5.10
mysql> show databases; ERROR 2006 (HY000): MySQL server has gone away No connection. Trying to reconnect... Connection id: 4 Current database: *** NONE *** +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | test | +--------------------+ 4 rows in set (0.01 sec) mysql>
切換很快,大概在2到3秒之間!