MySQL5.7 利用keepalived來實現mysql雙主高可用方案的詳細過程
伺服器準備
Keepalived:192.168.13.15
Keepalived:192.168.13.16
Mysql-m1: 192.168.13.15
Mysql-m2: 192.168.13.16
1,在m1、m2上準備mysql5.7環境
2,在m1上部署keepalived
(1)yum源安裝:yuminstall keepalived –y,只是版本低一些是1.2.13
(2)原始碼安裝:
# 下載最新版本:1.2.20 # 解壓縮安裝 tar -xvf keepalived-1.2.20.tar.gz cd keepalived-1.2.20 yum install openssl* -y ./configure make make install |
設定開機啟動項:
cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ ln -s /usr/local/sbin/keepalived /usr/sbin/ chmod +x /etc/init.d/keepalived chkconfig --add keepalived chkconfig keepalived on |
新增keepalived.conf配置檔案:
mkdir /etc/keepalived vim /etc/keepalived/keepalived.conf global_defs { notification_email { } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MySQL-ha } vrrp_instance VI_1 { state BACKUP # 2 severs keep the same value. interface eth0 virtual_router_id 51 priority 100 # priority, the another set to 90 advert_int 1 nopreempt #don't race to control, set the highst priorty mysql servers only. authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.13.14 } } virtual_server 192.168.13.14 3317 { delay_loop 2 # check the real_server status for every 2 seconds. lb_algo wrr #LVS arithmetic lb_kind DR #LVS model persistence_timeout 60 #k protocol TCP real_server 192.168.13.15 3317 { weight 3 notify_down /usr/local/mysql/bin/mysql.sh # run the scripts if mysql is down. TCP_CHECK { connect_timeout 10 #timeout nb_get_retry 3 #conect times to try to connect delay_before_retry 3 #interval of retry connect_port 3317 # check mysql port } } } |
Keepalived預設的日誌在/var/log/messages裡面,如果要設定單獨的日誌路徑,然後通過如下命令啟動/usr/local/keepalived/sbin/keepalived -d -D -S 0 的方式來啟動keepalived,或者修改/etc/sysconfig/keepalived,然後用service來啟動。
service方式設定日誌路徑:
# 1 修改keepalived啟動方式 [[email protected]_lvdi_dbm1_13_15 mysql]# vim /etc/sysconfig/keepalived KEEPALIVED_OPTIONS="-D -d -S 0" # 2 設定路徑 /etc/syslog.conf [[email protected]_lvdi_dbm1_13_15 mysql]# vim /etc/syslog.conf # Log all kernel messages to the console. # Logging much else clutters up the screen. #kern.* /dev/console # Log anything (except mail) of level info or higher. # Don't log private authentication messages! *.info;mail.none;authpriv.none;cron.none /var/log/messages # The authpriv file has restricted access. authpriv.* /var/log/secure # Log all the mail messages in one place. mail.* -/var/log/maillog # Log cron stuff cron.* /var/log/cron # Everybody gets emergency messages *.emerg * # Save news errors of level crit and higher in a special file. uucp,news.crit /var/log/spooler # Save boot messages also to boot.log local7.* /var/log/boot.log # keepalived -S 0 local0.* /var/log/keepalived.log |
3,在m2上部署keepalived
步驟和在m1上一模一樣,只是keepalived.conf有所不同,如下黃色背景的部分配置資訊:
[[email protected]_lvdi_dbm1_13_16 ~]# more /etc/keepalived/keepalived.conf #vim /etc/keepalived/keepalived.conf global_defs { notification_email { } notification_email_from [email protected] smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MySQL-ha } vrrp_instance VI_1 { state BACKUP # 2 severs keep the same value. interface eth0 virtual_router_id 51 priority 90 # priority, m2 is set to 90 advert_int 1 #nopreempt #don't race to control, set the highst priorty mysql servers only. authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.13.14 } } virtual_server 192.168.13.14 3317 { delay_loop 2 # check the real_server status for every 2 seconds. lb_algo wrr #LVS arithmetic lb_kind DR #LVS model persistence_timeout 60 #k protocol TCP real_server 192.168.13.16 3317 { weight 3 notify_down /usr/local/mysql/bin/mysql.sh # run the scripts if mysql is down. TCP_CHECK { connect_timeout 10 # nb_get_retry 3 #conect times to try to connect delay_before_retry 3 #interval of retry connect_port 3317 # check mysql port } } } |
4,遠端登入驗證資料庫的vip
使用遠端登入驗證,能通過vip來連線資料庫,對資料進行操作處理:
# 1,建立個測試帳號: GRANT SELECT, INSERT, UPDATE, DELETE ON `test`.* TO 'kt'@'1%' IDENTIFIED BY 'zhita26ywx18'; # 2,遠端通過vip登入 [[email protected]_lvdi_dbm1_13_16 keepalived]# mysql -h192.168.13.14 -P3317 -ukt --password="zhita26ywx18" mysql: [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 1315 Server version: 5.7.11-log MySQL Community Server (GPL) Copyright (c) 2000, 2016, 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> use business_db Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed mysql> insert into t2 select 10; Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from t2; +----+ | a | +----+ | 1 | | 3 | | 4 | | 3 | | 10 | +----+ 5 rows in set (0.00 sec) mysql> |
5,準備驗證vip自動切換的思路
為了測試驗證的準確和可觀性,暫時停止m1、m2的slave功能,並且在m1、m2上建立特殊的表來標示區分m1和m2,這樣在通過vip進去的時候,能及時準確的知道vip指向哪個mysql例項。
# 1,m1上 mysql> stop slave; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> create table test.m select "m1-db" as m1; # 準備m1表標識m1庫 Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from business_db.m1; +-------+ | m1 | +-------+ | m1-db | +-------+ 1 row in set (0.00 sec) mysql> #2,m2上 mysql> stop slave; Query OK, 0 rows affected (0.00 sec) mysql> create table test.m select "m2-db" as m2; # 準備m2表標識m2庫 Query OK, 1 row affected (0.00 sec) Records: 1 Duplicates: 0 Warnings: 0 mysql> select * from business_db.m2; +-------+ | m2 | +-------+ | m2-db | +-------+ 1 row in set (0.00 sec) mysql> |
6,開始驗證
按照順序,依次測試,可以測試出vip的遷移規律,可以通過如下來判斷mysql服務和keepalived服務是否已經關閉:
# 關閉mysql服務 [[email protected]_lvdi_dbm1_13_16 ~]# service mysqld stop Shutting down MySQL.... [ OK ] [[email protected]_lvdi_dbm1_13_16 ~]# # 啟動mysql服務 [[email protected]_lvdi_dbm1_13_16 ~]# service mysqld start Starting MySQL.. [ OK ] [[email protected]_lvdi_dbm1_13_16 ~]# # 判斷mysql服務是否關閉,為0關閉 [[email protected]_lvdi_dbm1_13_16 ~]# ps -eaf|grep mysqld |grep -v grep |wc |awk '{print $1}' 0 # 判斷keepalived是否關閉,為0關閉 [[email protected]_lvdi_dbm1_13_16 ~]# ps -eaf|grep keepalived |grep -v grep |wc |awk '{print $1}' 0 [[email protected]_lvdi_dbm1_13_16 ~]# |
6.1 ,m1、m2都在啟動著keepalived,那麼此時vip指向預設的m1例項
# 通過vip查詢test.m表的標識資料來判斷vip繫結在哪個mysql例項所在的伺服器上 [[email protected] ~]# mysql –h10.254.13.14 -P3317 -ukt --password="zhita26ywx18" -e "select * from test.m"; Warning: Using a password on the command line interface can be insecure. +-------+ | m1 | +-------+ | m1-db | +-------+ [[email protected] ~]# |
結論:啟動m1、m2上mysql例項keepalived例項,此時,vip指向m1。
6.2,停止m1上的mysql例項,m2上的mysql例項和keepalived都啟動著
# 通過vip查詢test.m表的標識資料來判斷vip繫結在哪個mysql例項所在的伺服器上 [[email protected] ~]# mysql –h10.254.13.14 -P3317 -ukt --password="zhita26ywx18" -e "select * from test.m"; Warning: Using a password on the command line interface can be insecure. +-------+ | m2 | +-------+ | m2-db | +-------+ [[email protected] ~]# |
結論:停止m1,原繫結在m1上的vip被釋放了,通過ip addr也可以看到已經釋放;然後vip啟動切換到m2,通過vip自動訪問上mysql例項,可以看到指向的是m2上的mysql例項,所以此時,vip指向m2。
6.3,再次啟動已經停止的m1上的例項和keepalived
# 通過vip查詢test.m表的標識資料來判斷vip繫結在哪個mysql例項所在的伺服器上 [[email protected] ~]# mysql –h10.254.13.14 -P3317 -ukt --password="zhita26ywx18" -e "select * from test.m"; Warning: Using a password on the command line interface can be insecure. +-------+ | m2 | +-------+ | m2-db | +-------+ [[email protected] ~]# |
結論:再次啟動m1後,發現vip還是繫結在m2上,表明如果當前vip所在的mysql例項沒有down,則vip不會自動切換到別的mysql例項上,哪怕你啟動了別的優先順序高的keepalived服務繫結的mysql例項,主要原因是因為我們設定的不搶佔的規則。
6.4,停止m2上的mysql例項
# 通過vip查詢test.m表的標識資料來判斷vip繫結在哪個mysql例項所在的伺服器上 [[email protected] ~]# mysql –h10.254.13.14 -P3317 -ukt --password="zhita26ywx18" -e "select * from test.m"; Warning: Using a password on the command line interface can be insecure. +-------+ | m1 | +-------+ | m1-db | +-------+ [[email protected] ~]# |
結論:停止m2上的mysql例項,則繫結在m2上的vip自動釋放了,然後vip會切換到m1上的mysql例項伺服器。
6.5 總結
從6.1到6.4的測試來看,keepalived基本滿足了我們的ha服務,自動切換功能基本滿足了,mysql down後能釋放vip,切換vip到另外一臺備用的mysql例項上面。
7,檢視版本號
[[email protected]_lvdi_dbm1_13_15 ~]# keepalived -v Keepalived v1.2.20 (05/08,2016) Copyright (C) 2001-2016 Alexandre Cassen, <[email protected]> Build options: KRNL_2_6 WITH_LVS HAVE_IPVS_SYNCD WITH_VRRP HAVE_VRRP_VMAC WITHOUT_ADDR_GEN_MODE WITHOUT_SNMP WITHOUT_SNMP_KEEPALIVED WITHOUT_SNMP_CHECKER WITHOUT_SNMP_RFC WITHOUT_SNMP_RFCV2 WITHOUT_SNMP_RFCV3 WITHOUT_LIBNL WITH_VRRP_AUTH WITH_SO_MARK WITHOUT_LIBIPTC WITHOUT_LIBIPSET WITHOUT_IPV4_DEVCONF WITHOUT_IF_H_LINK_H_COLLISION [[email protected]_lvdi_dbm1_13_15 ~]# |
8,問題記錄1
to the PKG_CONFIG_PATH environment variable
No package 'libiptc' found
checking for iptc_init in -liptc... no
checking for kernel version... 2.6.18
checking for IPVS syncd support... yes
checking for kernel macvlan support... yes
checking for kernel inet6_addr_gen_modesupport... no
checking whether SO_MARK is declared... no
configure: error: No SO_MARK declaration inheaders
解決方法2:
能ping通,但是telnet不通vip的3306埠
./configure --prefix=/usr/local/keepalived--disable-fwmark
9,問題記錄2
[[email protected] ~]# mysql -h192.168.121.181-P3306 -ukt --password="zhita26ywx18" -e "select * fromtest.m";
Warning: Using a password on the commandline interface can be insecure.
ERROR 2003 (HY000): Can't connect to MySQLserver on '192.168.121.181' (111)
[[email protected] ~]#
[[email protected] ~]#
[[email protected] ~]# ping 192.168.121.181
PING 192.168.121.181 (192.168.121.181)56(84) bytes of data.
64 bytes from 192.168.121.181: icmp_seq=1ttl=64 time=0.316 ms
64 bytes from 192.168.121.181: icmp_seq=2ttl=64 time=0.330 ms
^C
--- 192.168.121.181 ping statistics ---
2 packets transmitted, 2 received, 0%packet loss, time 1713ms
rtt min/avg/max/mdev = 0.316/0.323/0.330/0.007ms
[[email protected] ~]# telnet 192.168.121.1813306
Trying 192.168.121.181...
telnet: connect to address 192.168.121.181:Connection refused
[[email protected] ~]# telnet 192.168.121.1813307
Trying 192.168.121.181...
Connected to 192.168.121.181.
Escape character is '^]'.
N
5.6.12-log[¦~jv5A)Fg-TPqkDOB<F);.mysql_native_passwordXshell
!
#08S01Got packets out of orderConnection closed by foreignhost.[[email protected] ~]#
看到m2上面的mysql埠是3307,但是統一的vip的資料庫埠是3306,所以telnet不通3306,可以telnet3307,這裡有一個折中方案,採用iptables埠轉發下,將m2的3306埠轉到3307。
加一個埠對映:
[[email protected]_serv_121_12 keepalived]#iptables -t nat -A PREROUTING -p tcp --dport 3306 -j REDIRECT --to-port 3307
[[email protected]_serv_121_12 keepalived]#
再驗證,可以連線上m2了:
[[email protected] ~]# telnet 192.168.121.1813306
Trying 192.168.121.181...
Connected to 192.168.121.181.
Escape character is '^]'.
N
5.6.12-log꧞)P6\PXK-P1A2uz'P<xl7mysql_native_passwordXshell
!
#08S01Got packets out of orderConnection closed by foreignhost.[[email protected] ~]#
[[email protected] ~]# mysql -h192.168.121.181-P3306 -ukt --password="zhita26ywx18" -e 'select * from test.m';
Warning: Using a password on the commandline interface can be insecure.
+-------+
| m2 |
+-------+
| m2-db |
+-------+
[[email protected] ~]#
相關推薦
MySQL5.7 利用keepalived來實現mysql雙主高可用方案的詳細過程
伺服器準備Keepalived:192.168.13.15Keepalived:192.168.13.16Mysql-m1: 192.168.13.15Mysql-m2: 192.168.13.161,在m1、m2上準備mysql5.7環境2,在m1上部署keepalived
MySQL集群(四)之keepalived實現mysql雙主高可用
健康檢查 重連 lose 搶占 pro 資源 交換機 state nec 前面大家介紹了主從、主主復制以及他們的中間件mysql-proxy的使用,這一篇給大家介紹的是keepalived的搭建與使用! 一、keepalived簡介 1.1、keepalived介紹
mysql雙機熱備以及使用keepalived實現mysql雙主高可用
根據蒐集的資料安裝測試並在安裝測試過程中整理的文件,部分參考文件在相應位置有標記。如有不足希望不吝賜教。 mysql雙機熱備的方式有兩種: 主-從伺服器雙機熱備 主-主伺服器雙機熱備 下文以主-主伺服器雙機熱備為例,主-從配置類似。 兩臺centos7,ip分別為:
MYSQL雙主高可用方案部署實例
tro dmi admin route pts service firewall oop sql king01與king02互為master-slave[root@king01 ~]# mysql -uroot -pabcd.1234mysql> show slave
基於keepalived搭建mysql雙主高可用
目錄 概述 環境準備 keepalived搭建 mysql搭建 mysql雙主搭建 mysql雙主高可用搭建 概述 傳統(不借助中介軟體)的資料庫主從搭建,如果主節點掛掉了,從節點只能讀取無法寫
keepalived+haproxy實現mysql負載均衡高可用
環境準備 作業系統CentOS 6.9 Haproxy+keepalived(h1) 192.168.20.135 Haproxy+keepalived(h2) 192.168.20.136
Keepalived+MySQL雙主高可用配置
1.安裝環境: 伺服器IPOSMySQL同步使用者名稱/密碼VIP master1192.168.0.110CentOS
MySQL-雙主高可用
主伺服器開啟binlog日誌 [mysqld]log-bin=masterlog-bin-index=masterserver-id=11.全備:[[email protected] data]# mysqldump -u root -p123 --all-databases > /tmp/a
MySQL雙主高可用架構之MMM實戰
MMM簡介: MMM即Master-Master Replication Manager for MySQL(mysql主主複製管理器),是關於mysql主主複製配置的監控、故障轉移和管理的一套可伸縮的指令碼套件(在任何時候只有一個節點可以被寫入),這個套件也能基於標準
mysql通過keepalived建立雙主高可用
參考url1:http://blog.51cto.com/lizhenliang/1362313 參考url2:https://blog.csdn.net/huoyuanshen/article/details/68483188 主要了解keepalived的注意事項部分。
使用MMM實現MySQL雙主複製高可用
一、MMM簡介 1. 概述 MMM(Master-Master replication manager for MySQL)是一套支援雙主故障切換和雙主日常管理的指令碼程式。MMM使用Perl語言開發,主要用來監控和管理MySQL Master-Maste
[轉] Haproxy、Keepalived雙主高可用負載均衡
配置過程 virtual dev gnu 文本 tpch margin amp queue http://blog.chinaunix.net/uid-25266990-id-3989321.html 在測試了Nginx+Keepalived的負載均衡後,也對Haprox
006.MySQL雙主-Master02可用配置
1 [[email protected] ~]# vim /etc/keepalived/keepalived.conf 2 ! Configuration File for keepalived 3 global_defs { 4 notification_email
005.MySQL雙主-Master01可用配置
1 [[email protected] ~]# vim /etc/keepalived/keepalived.conf 2 ! Configuration File for keepalived 3 global_defs { 4 notification_email
MySQL叢集架構篇:MHA+MySQL-PROXY+LVS實現MySQL叢集架構高可用/高效能-技術流ken
MHA簡介 MHA可以自動化實現主伺服器故障轉移,這樣就可以快速將從伺服器晉級為主伺服器(通常在10-30s),而不影響複製的一致性,不需要花錢買更多的新伺服器,不會有效能損耗,容易安裝,不必更改現有的部署環境,適用於任何儲存引擎。 MHA提供線上主伺服器切換,改變先正執行的主伺服器到另外一臺上,這個過程只
MHA實現Mysql半同步高可用
一、MHA介紹 1、MHA兩部分組成 MHA 由兩部分組成: MHA Manager(管理節點)和 MHA Node(資料節點)。 MHA Manager可以單獨部署在一臺獨立的機器上管理多個 master-slave 叢集,也可以部署在一臺 slave 節點上。 2、
案例二(構建雙主高可用HAProxy負載均衡系統)
fresh htm nec ont unix outer index ces title 在案例一介紹HAProxy高可用負載均衡集群架構中,雖然通過Keepalived實現了HAProxy的高可用,但是嚴重浪費了服務器資源,因為在一主一備的Keepalived環境中,
MySQL叢集常見高可用方案(轉)
版權宣告:本文為博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/ZYC88888/article/details/81287951 1. 概述 我們在考慮MySQL資料庫的高可用的架構時,主要要考慮如下幾方面: 如果資料庫發生了宕機
LVS + Keepalived + Nginx基於DR模式構建高可用方案
在大型網站中一般服務端會做叢集,同時利用負載均衡器做負載均衡。這樣有利於將大量的請求分散到各個伺服器上,提升網站的響應速度。當然為了解決單點故障的問題,還會做熱備份方案。這裡演示利用LVS做負載均衡器,同時利用Keepalived保證其高可用,基於LVS的DR模式構建Nginx叢集。 1、環境準備 各個軟體及
常見的Mysql十款高可用方案
#### 簡介 > 我們在考慮MySQL資料庫的高可用架構時,主要考慮如下幾方面: > > 如果資料庫發生了宕機或者意外中斷等故障,能儘快恢復資料庫的可用性,儘可能的減少停機時間,保證業務不會因為資料庫的故障而中斷。 > > 用作備份、只讀副本等功能的非主節點的資料應該和主節點的資料實時或者最終保持一致。