您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)如何理解基于keepalived的MySQL主主復(fù)制,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。
系統(tǒng)環(huán)境:Linux red hat 6.3
mysql版本:mysql 5.6.34
mater1節(jié)點(diǎn)ip:192.168.140.222 主機(jī)名:red1
mater2節(jié)點(diǎn)ip:192.168.140.224 主機(jī)名:red2
一、分別在兩個(gè)節(jié)點(diǎn)安裝mysql數(shù)據(jù)庫(kù)。
二、1節(jié)點(diǎn)修改/etc/my.cnf參數(shù)
server_id =1 //標(biāo)志,節(jié)點(diǎn)之間不能重復(fù)
log-bin=/var/lib/mysql/xbinlog/binlog //開(kāi)啟binlog,并保存在位置/var/lib/mysql/xbinlog/下
binlog-format=ROW //binlog的模式是row模式
log_slave_updates=1 //開(kāi)啟log-slave-updates參數(shù)后,從庫(kù)從主庫(kù)復(fù)制的數(shù)據(jù)會(huì)寫(xiě)入log-bin日志文件里
sync_binlog=1 //當(dāng)每進(jìn)行1次事務(wù)提交之后,MySQL將進(jìn)行一次fsync之類(lèi)的磁盤(pán)同步指令來(lái)將binlog_cache中的數(shù)據(jù)強(qiáng)制寫(xiě)入磁盤(pán)。
innodb_flush_log_at_trx_commit=1 //代表每1次事務(wù)提交或事務(wù)外的指令都需要把日志寫(xiě)入(flush)硬盤(pán)
slave-parallel-workers=2 //設(shè)定從服務(wù)器的SQL線程數(shù)位2個(gè);0表示關(guān)閉多線程復(fù)制功能
replicate-do-db=replicate //同步的數(shù)據(jù)庫(kù)是replicate
并且給/var/lib/mysql/xbinlog/目錄賦予mysql:mysql權(quán)限,否則會(huì)起不來(lái) chown mysql:mysql /var/lib/mysql/xbinlog/
重啟mysql服務(wù)
2節(jié)點(diǎn)修改/etc/my.cnf參數(shù)
server_id =2 //這個(gè)參數(shù)要保證2個(gè)節(jié)點(diǎn)不一致
log_bin=/opt/mysql/data/ybinlog/binlog
binlog-format=ROW
log_slave_updates=1
sync_binlog=1
innodb_flush_log_at_trx_commit=1
slave-parallel-workers=2
replicate-do-db=replicate
并且給/var/lib/mysql/ybinlog/目錄賦予mysql:mysql權(quán)限,否則會(huì)起不來(lái) chown mysql:mysql /var/lib/mysql/ybinlog/
重啟mysql服務(wù)
三、在1節(jié)點(diǎn):
mysql> grant replication slave on *.* to 'replicate'@'192.168.140.224' identified by 'replicate';
Query OK, 0 rows affected (0.00 sec)
mysql> flush tables with read lock;
Query OK, 0 rows affected (0.00 sec)
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000002 | 337 | | | |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)
mysql> unlock tables;
Query OK, 0 rows affected (0.00 sec)
在2節(jié)點(diǎn)
mysql> change master to master_host='192.168.140.222',master_user='replicate',master_password='replicate',master_log_file='binlog.000002',master_log_pos=337;
Query OK, 0 rows affected, 2 warnings (0.00 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> show slave status\G //查看Slave_IO_Running 和 Slave_SQL_Running兩項(xiàng)是不是yes狀態(tài)
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 | 120 | | | |
+---------------+----------+--------------+------------------+-------------------+
在1節(jié)點(diǎn)
mysql> change master to master_host='192.168.140.224',master_user='replicate', master_password='replicate',master_log_file='binlog.000001',master_log_pos=120;
Query OK, 0 rows affected, 2 warnings (0.01 sec)
mysql> start slave;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> show slave status\G //查看Slave_IO_Running 和 Slave_SQL_Running兩項(xiàng)是不是yes狀態(tài)
四、測(cè)試,在2個(gè)節(jié)點(diǎn)都插入數(shù)據(jù),然后select驗(yàn)證一下
五、下載keepalived
http://www.keepalived.org/software/keepalived-1.2.13.tar.gz
解壓:tar xvf keepalived-1.2.13.tar.gz
cd keepalived-1.2.13
初始化: ./configure --disable-fwmark
編譯安裝:make && make install
keepalived的配置前提
1、本機(jī)的主機(jī)名與hosts中定義的主機(jī)保持一致。
2、各節(jié)點(diǎn)要能互相解析主機(jī)名,也就是說(shuō)hosts文件中要有本機(jī)的和對(duì)方的ip和對(duì)應(yīng)的主機(jī)名
3、各個(gè)節(jié)點(diǎn)的時(shí)間必須同步
關(guān)閉selinux并清空防火墻規(guī)則
[root@red1 ~]# setenforce 0
[root@red1 ~]# iptables -F
配置keepalived
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/sbin/keepalived /usr/bin/
cp /software/keepalived/keepalived-1.2.13/keepalived/etc/init.d/keepalived.init /etc/init.d
mkdir -p /etc/keepalived
cp /software/keepalived/keepalived-1.2.13/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
節(jié)點(diǎn)1上配置
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { //全局配置段
notification_email { //定義故障通知郵箱
root@localhost //郵箱地址,可以多個(gè)
}
notification_email_from root@localhost //發(fā)件人地址
smtp_server localhost //郵件服務(wù)器地址
smtp_connect_timeout 30 //聯(lián)系郵件服務(wù)器的超時(shí)時(shí)長(zhǎng)
router_id red1 //當(dāng)前物理設(shè)備在網(wǎng)絡(luò)中的唯一ID標(biāo)識(shí)
}
vrrp_instance VI_1 { //實(shí)例名在配置文件中不能重復(fù)
state MASTER //MASTER或BACKUP,定義當(dāng)前節(jié)點(diǎn)默認(rèn)為主或從節(jié)點(diǎn)
interface eth0
virtual_router_id 51 //當(dāng)前虛擬路由的標(biāo)識(shí)符,必須全局唯一
priority 100 //定義當(dāng)前實(shí)例的優(yōu)先級(jí),備機(jī)要小于主機(jī)
advert_int 1 //狀態(tài)通告周期(秒)---用于主節(jié)點(diǎn)向BACKUP節(jié)點(diǎn)通告心跳信息
authentication { /消息認(rèn)證配置
auth_type PASS //認(rèn)證方式(MD5或PASS簡(jiǎn)單字符認(rèn)證)
auth_pass 1111 //認(rèn)證密碼,所有同一虛擬實(shí)例節(jié)點(diǎn)的必須保持一致
}
virtual_ipaddress { //自動(dòng)配置在interface上的(vip)虛擬IP地址,可以是多個(gè)
192.168.140.66 //虛擬ip
}
}
virtual_server 192.168.140.66 { //虛擬ip
delay_loop 6 //RealServer的健康狀態(tài)探查的時(shí)間間隔
lb_algo rr //負(fù)載均衡的調(diào)度算法(Scheduler)
lb_kind DR //LVS類(lèi)型。支持NAT/DR/TUN
persistence_timeout 50 //持久連接超時(shí)時(shí)間
protocol TCP //只能適用TCP,若適用UDP則使用ops參數(shù)
real_server 192.168.140.222 { //1節(jié)點(diǎn)ip
weight 1 權(quán)重
TCP_CHECK {
connect_timeout 3 健康狀態(tài)監(jiān)測(cè)超時(shí)時(shí)間
nb_get_retry 3 number of get retry
delay_before_retry 3 每次重試之前延遲3秒
# connect_port 3306
}
}
}
節(jié)點(diǎn)2上配置
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from root@localhost
smtp_server localhost
smtp_connect_timeout 30
router_id red2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.140.66
}
}
virtual_server 192.168.140.66 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.140.224{
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
#connect_port 3306
}
}
}
開(kāi)啟服務(wù) service keepalived start
查看狀態(tài) service keepalived status
查看VIP是否在本節(jié)點(diǎn) ip add //VIP:192.168.140.66會(huì)在節(jié)點(diǎn)1上,因?yàn)?節(jié)點(diǎn)的優(yōu)先級(jí)高。
六、測(cè)試keepalived
此時(shí)在1節(jié)點(diǎn)和2節(jié)點(diǎn)上執(zhí)行ip add,會(huì)發(fā)現(xiàn)VIP 192.168.140.66會(huì)飄在1節(jié)點(diǎn)上。
然后在1節(jié)點(diǎn)上執(zhí)行service keepalived stop,然后在2節(jié)點(diǎn)上執(zhí)行ip add,會(huì)發(fā)現(xiàn)vip飄到了2節(jié)點(diǎn)。
然后在1節(jié)點(diǎn)service keepalived start,會(huì)發(fā)現(xiàn)vip又回到了1節(jié)點(diǎn)。
到此,測(cè)試成功
關(guān)于如何理解基于keepalived的MySQL主主復(fù)制就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(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)容。