您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)Ubuntu搭建Mysql+Keepalived高可用如何實(shí)現(xiàn),小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
安裝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
配置權(quán)限
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;
配置兩臺(tái)Mysql主主同步
配置節(jié)點(diǎn)1
vim /etc/mysql/my.cnf
server-id = 1 #節(jié)點(diǎn)ID log_bin = mysql-bin.log #日志 binlog_format = "ROW" #日志格式 auto_increment_increment = 2 #自增ID間隔(=節(jié)點(diǎn)數(shù),防止ID沖突) auto_increment_offset = 1 #自增ID起始值(節(jié)點(diǎn)ID) binlog_ignore_db=mysql #不同步的數(shù)據(jù)庫(kù) binlog_ignore_db=information_schema binlog_ignore_db=performance_schema
重啟mysql
service mysql restart mysql -u root -p
記錄節(jié)點(diǎn)1的binlog日志位置
show master status; mysql-bin.000001 245 mysql,information_schema,performance_schema
配置節(jié)點(diǎn)2
vim /etc/mysql/my.cnf
server-id = 2 log_bin = mysql-bin.log relay_log = mysql-relay-bin.log #中繼日志 log_slave_updates = ON #中繼日志執(zhí)行后,變化計(jì)入日志 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 #查看同步狀態(tài) Slave_IO_Running和Slave_SQL_Running需要均為Yes show slave status;
記錄節(jié)點(diǎn)2的binlog日志位置
show master status; mysql-bin.000001 1029 mysql,information_schema,performance_schema
配置主主(節(jié)點(diǎn)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 #查看同步狀態(tài) 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/
配置節(jié)點(diǎn)信息
節(jié)點(diǎn)1 192.168.1.21
vim /etc/keepalived/keepalived.conf
global_defs { router_id MYSQL_HA #當(dāng)前節(jié)點(diǎn)名 } vrrp_instance VI_1 { state BACKUP #兩臺(tái)配置節(jié)點(diǎn)均為BACKUP interface eth0 #綁定虛擬IP的網(wǎng)絡(luò)接口 virtual_router_id 51 #VRRP組名,兩個(gè)節(jié)點(diǎn)的設(shè)置必須一樣,以指明各個(gè)節(jié)點(diǎn)屬于同一VRRP組 priority 101 #節(jié)點(diǎn)的優(yōu)先級(jí),另一臺(tái)優(yōu)先級(jí)改低一點(diǎn) advert_int 1 #組播信息發(fā)送間隔,兩個(gè)節(jié)點(diǎn)設(shè)置必須一樣 nopreempt #不搶占,只在優(yōu)先級(jí)高的機(jī)器上設(shè)置即可,優(yōu)先級(jí)低的機(jī)器不設(shè)置 authentication { #設(shè)置驗(yàn)證信息,兩個(gè)節(jié)點(diǎn)必須一致 auth_type PASS auth_pass 123456 } virtual_ipaddress { #指定虛擬IP,兩個(gè)節(jié)點(diǎn)設(shè)置必須一樣 192.168.1.111 } } virtual_server 192.168.1.111 3306 { #linux虛擬服務(wù)器(LVS)配置 delay_loop 2 #每個(gè)2秒檢查一次real_server狀態(tài) lb_algo wrr #LVS調(diào)度算法,rr|wrr|lc|wlc|lblc|sh|dh lb_kind DR #LVS集群模式 ,NAT|DR|TUN persistence_timeout 60 #會(huì)話保持時(shí)間 protocol TCP #使用的協(xié)議是TCP還是UDP real_server 192.168.1.21 3306 { weight 3 #權(quán)重 notify_down /usr/local/bin/mysql.sh #檢測(cè)到服務(wù)down后執(zhí)行的腳本 TCP_CHECK { connect_timeout 10 #連接超時(shí)時(shí)間 nb_get_retry 3 #重連次數(shù) delay_before_retry 3 #重連間隔時(shí)間 connect_port 3306 #健康檢查端口 } } }
節(jié)點(diǎn)2 192.168.1.20
vim /etc/keepalived/keepalived.conf
global_defs { router_id MYSQL_HA #當(dāng)前節(jié)點(diǎn)名 } vrrp_instance VI_1 { state BACKUP #兩臺(tái)配置節(jié)點(diǎn)均為BACKUP interface eth0 #綁定虛擬IP的網(wǎng)絡(luò)接口 virtual_router_id 51 #VRRP組名,兩個(gè)節(jié)點(diǎn)的設(shè)置必須一樣,以指明各個(gè)節(jié)點(diǎn)屬于同一VRRP組 priority 100 #節(jié)點(diǎn)的優(yōu)先級(jí),另一臺(tái)優(yōu)先級(jí)改低一點(diǎn) advert_int 1 #組播信息發(fā)送間隔,兩個(gè)節(jié)點(diǎn)設(shè)置必須一樣 nopreempt #不搶占,只在優(yōu)先級(jí)高的機(jī)器上設(shè)置即可,優(yōu)先級(jí)低的機(jī)器不設(shè)置 authentication { #設(shè)置驗(yàn)證信息,兩個(gè)節(jié)點(diǎn)必須一致 auth_type PASS auth_pass 123456 } virtual_ipaddress { #指定虛擬IP,兩個(gè)節(jié)點(diǎn)設(shè)置必須一樣 192.168.1.111 } } virtual_server 192.168.1.111 3306 { #linux虛擬服務(wù)器(LVS)配置 delay_loop 2 #每個(gè)2秒檢查一次real_server狀態(tài) lb_algo wrr #LVS調(diào)度算法,rr|wrr|lc|wlc|lblc|sh|dh lb_kind DR #LVS集群模式 ,NAT|DR|TUN persistence_timeout 60 #會(huì)話保持時(shí)間 protocol TCP #使用的協(xié)議是TCP還是UDP real_server 192.168.1.20 3306 { weight 3 #權(quán)重 notify_down /usr/local/bin/mysql.sh #檢測(cè)到服務(wù)down后執(zhí)行的腳本 TCP_CHECK { connect_timeout 10 #連接超時(shí)時(shí)間 nb_get_retry 3 #重連次數(shù) delay_before_retry 3 #重連間隔時(shí)間 connect_port 3306 #健康檢查端口 } } }
編寫異常處理腳本
vim /usr/local/bin/mysql.sh
#!/bin/sh killall keepalived
分配權(quán)限
chmod +x /usr/local/bin/mysql.sh
###測(cè)試
重啟keepalived
service keepalived restart
查看日志
tail -f /var/log/messages
查看虛擬IP
ip addr #或ip a 或ifconfig #主節(jié)點(diǎn)會(huì)有虛擬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
關(guān)閉主節(jié)點(diǎn)的mysql服務(wù)
service mysql stop
日志信息
#主節(jié)點(diǎn) 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 #從節(jié)點(diǎn) 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從主節(jié)點(diǎn)漂移到從節(jié)點(diǎn)
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連接測(cè)試
mysql -h 192.168.1.111 -u root -p
關(guān)于“Ubuntu搭建Mysql+Keepalived高可用如何實(shí)現(xiàn)”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。