這篇文章將為大家詳細(xì)講解有關(guān)如何理解基于keepalived的MySQL主主復(fù)制,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。
創(chuàng)新互聯(lián)是一家專業(yè)提供興化企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、H5場景定制、小程序制作等業(yè)務(wù)。10年已為興化眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。系統(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
一、分別在兩個節(jié)點(diǎn)安裝mysql數(shù)據(jù)庫。
二、1節(jié)點(diǎn)修改/etc/my.cnf參數(shù)
server_id =1 //標(biāo)志,節(jié)點(diǎn)之間不能重復(fù)
log-bin=/var/lib/mysql/xbinlog/binlog //開啟binlog,并保存在位置/var/lib/mysql/xbinlog/下
binlog-format=ROW //binlog的模式是row模式
log_slave_updates=1 //開啟log-slave-updates參數(shù)后,從庫從主庫復(fù)制的數(shù)據(jù)會寫入log-bin日志文件里
sync_binlog=1 //當(dāng)每進(jìn)行1次事務(wù)提交之后,MySQL將進(jìn)行一次fsync之類的磁盤同步指令來將binlog_cache中的數(shù)據(jù)強(qiáng)制寫入磁盤。
innodb_flush_log_at_trx_commit=1 //代表每1次事務(wù)提交或事務(wù)外的指令都需要把日志寫入(flush)硬盤
slave-parallel-workers=2 //設(shè)定從服務(wù)器的SQL線程數(shù)位2個;0表示關(guān)閉多線程復(fù)制功能
replicate-do-db=replicate //同步的數(shù)據(jù)庫是replicate
并且給/var/lib/mysql/xbinlog/目錄賦予mysql:mysql權(quán)限,否則會起不來 chown mysql:mysql /var/lib/mysql/xbinlog/
重啟mysql服務(wù)
2節(jié)點(diǎn)修改/etc/my.cnf參數(shù)
server_id =2 //這個參數(shù)要保證2個節(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)限,否則會起不來 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)
四、測試,在2個節(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ī)名,也就是說hosts文件中要有本機(jī)的和對方的ip和對應(yīng)的主機(jī)名
3、各個節(jié)點(diǎn)的時間必須同步
關(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 //郵箱地址,可以多個
}
notification_email_from root@localhost //發(fā)件人地址
smtp_server localhost //郵件服務(wù)器地址
smtp_connect_timeout 30 //聯(lián)系郵件服務(wù)器的超時時長
router_id red1 //當(dāng)前物理設(shè)備在網(wǎng)絡(luò)中的唯一ID標(biāo)識
}
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)識符,必須全局唯一
priority 100 //定義當(dāng)前實(shí)例的優(yōu)先級,備機(jī)要小于主機(jī)
advert_int 1 //狀態(tài)通告周期(秒)---用于主節(jié)點(diǎn)向BACKUP節(jié)點(diǎn)通告心跳信息
authentication { /消息認(rèn)證配置
auth_type PASS //認(rèn)證方式(MD5或PASS簡單字符認(rèn)證)
auth_pass 1111 //認(rèn)證密碼,所有同一虛擬實(shí)例節(jié)點(diǎn)的必須保持一致
}
virtual_ipaddress { //自動配置在interface上的(vip)虛擬IP地址,可以是多個
192.168.140.66 //虛擬ip
}
}
virtual_server 192.168.140.66 { //虛擬ip
delay_loop 6 //RealServer的健康狀態(tài)探查的時間間隔
lb_algo rr //負(fù)載均衡的調(diào)度算法(Scheduler)
lb_kind DR //LVS類型。支持NAT/DR/TUN
persistence_timeout 50 //持久連接超時時間
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)測超時時間
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
}
}
}
開啟服務(wù) service keepalived start
查看狀態(tài) service keepalived status
查看VIP是否在本節(jié)點(diǎn) ip add //VIP:192.168.140.66會在節(jié)點(diǎn)1上,因?yàn)?節(jié)點(diǎn)的優(yōu)先級高。
六、測試keepalived
此時在1節(jié)點(diǎn)和2節(jié)點(diǎn)上執(zhí)行ip add,會發(fā)現(xiàn)VIP 192.168.140.66會飄在1節(jié)點(diǎn)上。
然后在1節(jié)點(diǎn)上執(zhí)行service keepalived stop,然后在2節(jié)點(diǎn)上執(zhí)行ip add,會發(fā)現(xiàn)vip飄到了2節(jié)點(diǎn)。
然后在1節(jié)點(diǎn)service keepalived start,會發(fā)現(xiàn)vip又回到了1節(jié)點(diǎn)。
到此,測試成功
關(guān)于如何理解基于keepalived的MySQL主主復(fù)制就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
網(wǎng)站標(biāo)題:如何理解基于keepalived的MySQL主主復(fù)制-創(chuàng)新互聯(lián)
路徑分享:http://www.rwnh.cn/article32/ccihpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站、網(wǎng)站改版、定制開發(fā)、網(wǎng)站策劃、App設(shè)計(jì)、移動網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容