Ubuntu搭建Mysql+Keepalived高可用的實現(雙主熱備)
Mysql5.5雙機熱備
實現方案
安裝兩臺Mysql
安裝Mysql5.5
sudo apt-get update apt-get install aptitude aptitude install mysql-server-5.5 或 sudo apt-cache search mariadb-server apt-get install -y mariadb-server-5.5
卸載
sudo apt-get remove mysql-* dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P
配置權限
vim /etc/mysql/my.cnf #bind-address = 127.0.0.1 mysql -u root -p grant all on *.* to root@'%' identified by 'root' with grant option; flush privileges;
配置兩臺Mysql主主同步
配置節點1
vim /etc/mysql/my.cnf
server-id = 1 #節點ID log_bin = mysql-bin.log #日志 binlog_format = "ROW" #日志格式 auto_increment_increment = 2 #自增ID間隔(=節點數,防止ID沖突) auto_increment_offset = 1 #自增ID起始值(節點ID) binlog_ignore_db=mysql #不同步的數據庫 binlog_ignore_db=information_schema binlog_ignore_db=performance_schema
重啟mysql
service mysql restart mysql -u root -p
記錄節點1的binlog日志位置
show master status; mysql-bin.000001 245 mysql,information_schema,performance_schema
配置節點2
vim /etc/mysql/my.cnf
server-id = 2 log_bin = mysql-bin.log relay_log = mysql-relay-bin.log #中繼日志 log_slave_updates = ON #中繼日志執行後,變化計入日志 read_only = 0 binlog_format = "ROW" auto_increment_increment = 2 auto_increment_offset = 2 binlog_ignore_db=mysql binlog_ignore_db=information_schema binlog_ignore_db=performance_schema replicate_ignore_db=mysql replicate_ignore_db=information_schema replicate_ignore_db=performance_schema
配置主從
mysql -u root -p CHANGE MASTER TO MASTER_HOST='192.168.1.21', MASTER_USER='root', MASTER_PASSWORD='root', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=245; #開啟同步 start slave #查看同步狀態 Slave_IO_Running和Slave_SQL_Running需要均為Yes show slave status;
記錄節點2的binlog日志位置
show master status; mysql-bin.000001 1029 mysql,information_schema,performance_schema
配置主主(節點1)
vim /etc/mysql/my.cnf
relay_log = mysql-relay-bin.log log_slave_updates = ON read_only = 0 replicate_ignore_db=mysql replicate_ignore_db=information_schema replicate_ignore_db=performance_schema
開啟同步
mysql -u root -p CHANGE MASTER TO MASTER_HOST='192.168.1.20', MASTER_USER='root', MASTER_PASSWORD='root', MASTER_LOG_FILE='mysql-bin.000001', MASTER_LOG_POS=1029; #開啟同步 start slave #查看同步狀態 Slave_IO_Running和Slave_SQL_Running需要均為Yes show slave status;
異常處理
Could not initialize master info structure, more error messages can be found in the MySQL error log
解決:reset slave
安裝配置Keepalived
安裝Keepalived
#依賴 sudo apt-get install -y libssl-dev sudo apt-get install -y openssl sudo apt-get install -y libpopt-dev sudo apt-get install -y libnl-dev libnl-3-dev libnl-genl-3.dev apt-get install daemon apt-get install libc-dev apt-get install libnfnetlink-dev apt-get install libnl-genl-3.dev #安裝 apt-get install keepalived #編譯安裝 cd /usr/local wget https://www.keepalived.org/software/keepalived-2.2.2.tar.gz tar -zxvf keepalived-2.2.2.tar.gz mv keepalived-2.2.2 keepalived ./configure --prefix=/usr/local/keepalived sudo make && make install #開啟日志 sudo vim /etc/rsyslog.d/50-default.conf *.=info;*.=notice;*.=warn;\ auth,authpriv.none;\ cron,daemon.none;\ mail,news.none -/var/log/messages sudo service rsyslog restart tail -f /var/log/messages sudo mkdir /etc/sysconfig sudo cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ sudo cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ sudo cp /usr/local/keepalived/sbin/keepalived /sbin/ sudo mkdir /etc/keepalived sudo cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
配置節點信息
節點1 192.168.1.21
vim /etc/keepalived/keepalived.conf
global_defs { router_id MYSQL_HA #當前節點名 } vrrp_instance VI_1 { state BACKUP #兩臺配置節點均為BACKUP interface eth0 #綁定虛擬IP的網絡接口 virtual_router_id 51 #VRRP組名,兩個節點的設置必須一樣,以指明各個節點屬於同一VRRP組 priority 101 #節點的優先級,另一臺優先級改低一點 advert_int 1 #組播信息發送間隔,兩個節點設置必須一樣 nopreempt #不搶占,隻在優先級高的機器上設置即可,優先級低的機器不設置 authentication { #設置驗證信息,兩個節點必須一致 auth_type PASS auth_pass 123456 } virtual_ipaddress { #指定虛擬IP,兩個節點設置必須一樣 192.168.1.111 } } virtual_server 192.168.1.111 3306 { #linux虛擬服務器(LVS)配置 delay_loop 2 #每個2秒檢查一次real_server狀態 lb_algo wrr #LVS調度算法,rr|wrr|lc|wlc|lblc|sh|dh lb_kind DR #LVS集群模式 ,NAT|DR|TUN persistence_timeout 60 #會話保持時間 protocol TCP #使用的協議是TCP還是UDP real_server 192.168.1.21 3306 { weight 3 #權重 notify_down /usr/local/bin/mysql.sh #檢測到服務down後執行的腳本 TCP_CHECK { connect_timeout 10 #連接超時時間 nb_get_retry 3 #重連次數 delay_before_retry 3 #重連間隔時間 connect_port 3306 #健康檢查端口 } } }
節點2 192.168.1.20
vim /etc/keepalived/keepalived.conf
global_defs { router_id MYSQL_HA #當前節點名 } vrrp_instance VI_1 { state BACKUP #兩臺配置節點均為BACKUP interface eth0 #綁定虛擬IP的網絡接口 virtual_router_id 51 #VRRP組名,兩個節點的設置必須一樣,以指明各個節點屬於同一VRRP組 priority 100 #節點的優先級,另一臺優先級改低一點 advert_int 1 #組播信息發送間隔,兩個節點設置必須一樣 nopreempt #不搶占,隻在優先級高的機器上設置即可,優先級低的機器不設置 authentication { #設置驗證信息,兩個節點必須一致 auth_type PASS auth_pass 123456 } virtual_ipaddress { #指定虛擬IP,兩個節點設置必須一樣 192.168.1.111 } } virtual_server 192.168.1.111 3306 { #linux虛擬服務器(LVS)配置 delay_loop 2 #每個2秒檢查一次real_server狀態 lb_algo wrr #LVS調度算法,rr|wrr|lc|wlc|lblc|sh|dh lb_kind DR #LVS集群模式 ,NAT|DR|TUN persistence_timeout 60 #會話保持時間 protocol TCP #使用的協議是TCP還是UDP real_server 192.168.1.20 3306 { weight 3 #權重 notify_down /usr/local/bin/mysql.sh #檢測到服務down後執行的腳本 TCP_CHECK { connect_timeout 10 #連接超時時間 nb_get_retry 3 #重連次數 delay_before_retry 3 #重連間隔時間 connect_port 3306 #健康檢查端口 } } }
編寫異常處理腳本
vim /usr/local/bin/mysql.sh
#!/bin/sh killall keepalived
分配權限
chmod +x /usr/local/bin/mysql.sh
###測試
重啟keepalived
service keepalived restart
查看日志
tail -f /var/log/messages
查看虛擬IP
ip addr #或ip a 或ifconfig #主節點會有虛擬IP eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:9e:17:53:e5 brd ff:ff:ff:ff:ff:ff inet 192.168.1.21/24 brd 192.168.1.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.1.111/32 scope global eth0 valid_lft forever preferred_lft forever
關閉主節點的mysql服務
service mysql stop
日志信息
#主節點 Aug 10 15:00:30 i-7jaope92 Keepalived_healthcheckers[4949]: TCP connection to [192.168.1.20]:3306 failed !!! Aug 10 15:00:30 i-7jaope92 Keepalived_healthcheckers[4949]: Removing service [192.168.1.20]:3306 from VS [192.168.1.111]:3306 Aug 10 15:00:30 i-7jaope92 Keepalived_healthcheckers[4949]: Executing [/usr/local/bin/mysql.sh] for service [192.168.1.20]:3306 in VS [192.168.1.111]:3306 Aug 10 15:00:30 i-7jaope92 Keepalived_healthcheckers[4949]: Lost quorum 1-0=1 > 0 for VS [192.168.1.111]:3306 Aug 10 15:00:30 i-7jaope92 Keepalived_vrrp[4950]: VRRP_Instance(VI_1) sending 0 priority Aug 10 15:00:30 i-7jaope92 kernel: [100918.976041] IPVS: __ip_vs_del_service: enter #從節點 Aug 10 15:00:31 i-6gxo6kx7 Keepalived_vrrp[718]: VRRP_Instance(VI_1) Transition to MASTER STATE Aug 10 15:00:32 i-6gxo6kx7 Keepalived_vrrp[718]: VRRP_Instance(VI_1) Entering MASTER STATE
虛擬IP從主節點漂移到從節點
ip a eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:9e:e7:26:5c brd ff:ff:ff:ff:ff:ff inet 192.168.1.20/24 brd 192.168.1.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.1.111/32 scope global eth0 valid_lft forever preferred_lft forever
Mysql連接測試
mysql -h 192.168.1.111 -u root -p
到此這篇關於Ubuntu搭建Mysql+Keepalived高可用的實現(雙主熱備)的文章就介紹到這瞭,更多相關Mysql+Keepalived高可用內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- MySQL8.0.18配置多主一從
- MySQL主從配置及haproxy和keepalived搭建過程解析
- MySQL數據庫⾼可⽤HA實現小結
- mysql主從復制的實現步驟
- Docker容器實現MySQL多源復制場景分析