MySQL 高可用性之 Keepalived 雙主熱備
轉載補充:
通常說的「雙機熱備」是指兩臺機器都在執行,但並不是兩臺機器都同時在提供服務。當提供服務的一臺出現故障的時候,另外一臺會馬上自動接管並且提供服務,而且切換的時間非常短。
Keepalived的工作原理是VRRP(Virtual Router Redundancy Protocol)虛擬路由冗餘協議。在VRRP中有兩組重要的概念:VRRP路由器和虛擬路由器,主控路由器和備份路由器。
VRRP路由器是指執行VRRP的路由器,是物理實體,虛擬路由器是指VRRP協議建立的,是邏輯概念。一組VRRP路由器協同工作,共同構成一臺虛擬路由器。 Vrrp中存在著一種選舉機制,用以選出提供服務的路由即主控路由,其他的則成了備份路由。當主控路由失效後,備份路由中會重新選舉出一個主控路由,來繼續工作,來保障不間斷服務。
環境描述:
- OS:CentOS6.5_X64
- MASTER:192.168.0.202
- BACKUP:192.168.0.203
- VIP:192.168.0.204
1、配置兩臺Mysql主主同步
[root@masterr ~]# yum install mysql-server mysql -y [root@masterr ~]# service mysqld start [root@masterr ~]# mysqladmin -u root password 123.com [root@masterr ~]# vi /etc/my.cnf #開啟二進位制日誌,設定id [mysqld] server-id = 1 #backup這臺設定2 log-bin = mysql-bin binlog-ignore-db = mysql,information_schema #忽略寫入binlog日誌的庫 auto-increment-increment = 2 #欄位變化增量值 auto-increment-offset = 1 #初始欄位ID為1 slave-skip-errors = all #忽略所有複製產生的錯誤 [root@masterr ~]# service mysqld restart
#先檢視下log bin日誌和pos值位置
master配置如下:
[root@ master ~]# mysql -u root -p123.com mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.0.%' IDENTIFIED BY 'replication'; mysql> flush privileges; mysql> change master to -> master_host='192.168.0.203', -> master_user='replication', -> master_password='replication', -> master_log_file='mysql-bin.000002', -> master_log_pos=106; #對端狀態顯示的值 mysql> start slave; #啟動同步
backup配置如下:
[root@backup ~]# mysql -u root -p123.com mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.0.%' IDENTIFIED BY 'replication'; mysql> flush privileges; mysql> change master to -> master_host='192.168.0.202', -> master_user='replication', -> master_password='replication', -> master_log_file='mysql-bin.000002', -> master_log_pos=106; mysql> start slave;
#主主同步配置完畢,檢視同步狀態Slave_IO和Slave_SQL是YES說明主主同步成功。
在master插入資料測試下:
在backup檢視是否同步成功:
可以看到已經成功同步過去,同樣在backup插入到user表資料,一樣同步過去,雙主就做成功了。
2、配置keepalived實現熱備
[root@backup ~]# yum install -y pcre-devel openssl-devel popt-devel #安裝依賴包
[root@masterr ~]# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz [root@masterr ~]# tar zxvf keepalived-1.2.7.tar.gz [root@masterr ~]# cd keepalived-1.2.7 [root@masterr ~]#./configure --prefix=/usr/local/keepalived make
#將keepalived配置成系統服務
[root@masterr ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ [root@masterr ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ [root@masterr ~]# mkdir /etc/keepalived/ [root@masterr ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ [root@masterr ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@masterr ~]# vi /etc/keepalived/keepalived.conf ! Configuration File forkeepalived global_defs { notification_email { test@sina.com } notification_email_from admin@test.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MYSQL_HA #標識,雙主相同 } vrrp_instance VI_1 { state BACKUP #兩臺都設定BACKUP interface eth0 virtual_router_id 51 #主備相同 priority 100 #優先順序,backup設定90 advert_int 1 nopreempt #不主動搶佔資源,只在master這臺優先順序高的設定,backup不設定 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.0.204 } } virtual_server 192.168.0.204 3306 { delay_loop 2 #lb_algo rr #LVS演算法,用不到,我們就關閉了 #lb_kind DR #LVS模式,如果不關閉,備用伺服器不能通過VIP連線主MySQL persistence_timeout 50 #同一IP的連線60秒內被分配到同一臺真實伺服器 protocol TCP real_server 192.168.0.202 3306 { #檢測本地mysql,backup也要寫檢測本地mysql weight 3 notify_down /usr/local/keepalived/mysql.sh #當mysq服down時,執行此指令碼,殺死keepalived實現切換 TCP_CHECK { connect_timeout 3 #連線超時 nb_get_retry 3 #重試次數 delay_before_retry 3 #重試間隔時間 } }
[root@masterr ~]# vi /usr/local/keepalived/mysql.sh #!/bin/bash pkill keepalived [root@masterr ~]# chmod +x /usr/local/keepalived/mysql.sh [root@masterr ~]# /etc/init.d/keepalived start
#backup伺服器只修改priority為90、nopreempt不設定、real_server設定本地IP。
#授權兩臺Mysql伺服器允許root遠端登入,用於在其他伺服器登陸測試!
mysql> grant all on *.* to'root'@'192.168.0.%' identified by '123.com'; mysql> flush privileges;
三、測試高可用性
1、通過Mysql客戶端通過VIP連線,看是否連線成功。
2、停止master這臺mysql服務,是否能正常切換過去,可通過ip addr命令來檢視VIP在哪臺伺服器上。
3、可通過檢視/var/log/messges日誌,看出主備切換過程
4、master伺服器故障恢復後,是否主動搶佔資源,成為活動伺服器。
相關文章
- MySQL 高可用性—keepalived+mysql雙主MySql
- Keepalived雙機熱備實現高可用
- Keepalived - Keepalived 實現 tomcat雙機熱備Tomcat
- MySQL叢集搭建(6)-雙主+keepalived高可用MySql
- 使用Keepalived實現Nginx的自動重啟及雙主熱備高可用Nginx
- Redis高可用方案:使用Keepalived實現主備雙活Redis
- 使用Keepalived實現Nginx的雙機主備高可用Nginx
- MySQL 主從複製,雙機熱備MySql
- 你需要了解的高可用方案之使用keepalived搭建雙機熱備一覽
- MySQL主主模式+Keepalived高可用MySql模式
- linux上mysql MM(雙主)及keepalived搭建LinuxMySql
- MySQL進階:主主複製+Keepalived高可用MySql
- MySQL高可用架構之Keepalived+主從架構部署MySql架構
- mysql5.6主主複製及keepalived 高可用MySql
- keepalived 主備使用
- mysql高可用之keepalivedMySql
- 防火牆的雙機熱備1:主備、負載方式防火牆負載
- MySQL雙機互備熱備自動切換KVMySql
- MYSQL5.7.22 原始碼安裝 主從搭建 + KEEPALIVED高可用MySql原始碼
- mysql高可用衡搭建(Keepalived)MySql
- 怎麼在linux完成MySQL主從熱備?LinuxMySql
- 高可用服務之Keepalived利用指令碼實現服務的可用性檢測指令碼
- keepalived+MySQL實現高可用MySql
- 9、雙機熱備
- 基於MySQL雙主複製架構下部署LVS+KeepAlived負載均衡MySql架構負載
- MySQL高可用架構:mysql+keepalived實現MySql架構
- MySQL 主備MySql
- 生產環境部署Nginx伺服器雙機熱備部署-keepalived(多種模式教程)Nginx伺服器模式
- MySQL雙主雙從配置MySql
- MySQL主從配置及haproxy和keepalived搭建MySql
- MySQL高可用(二)主備延時如何解決?MySql
- mysql雙主雙從 搭建配置MySql
- GitHub 的 MySQL 高可用性實踐分享GithubMySql
- 計算機網路之策略路由與雙機熱備計算機網路路由
- MySQL備份與主備配置MySql
- Mysql雙主雙從高可用叢集的搭建且與MyCat進行整合MySql
- MYSQL8+CENTOS7.6 主從+keepalived搭建總結MySqlCentOS
- 解讀MySQL雙主複製的主備資料一致性GPMySql
- Keepalived 高可用