您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關如何理解基于keepalived的MySQL主主復制,文章內容質量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關知識有一定的了解。
系統環境:Linux red hat 6.3
mysql版本:mysql 5.6.34
mater1節點ip:192.168.140.222 主機名:red1
mater2節點ip:192.168.140.224 主機名:red2
一、分別在兩個節點安裝mysql數據庫。
二、1節點修改/etc/my.cnf參數
server_id =1 //標志,節點之間不能重復
log-bin=/var/lib/mysql/xbinlog/binlog //開啟binlog,并保存在位置/var/lib/mysql/xbinlog/下
binlog-format=ROW //binlog的模式是row模式
log_slave_updates=1 //開啟log-slave-updates參數后,從庫從主庫復制的數據會寫入log-bin日志文件里
sync_binlog=1 //當每進行1次事務提交之后,MySQL將進行一次fsync之類的磁盤同步指令來將binlog_cache中的數據強制寫入磁盤。
innodb_flush_log_at_trx_commit=1 //代表每1次事務提交或事務外的指令都需要把日志寫入(flush)硬盤
slave-parallel-workers=2 //設定從服務器的SQL線程數位2個;0表示關閉多線程復制功能
replicate-do-db=replicate //同步的數據庫是replicate
并且給/var/lib/mysql/xbinlog/目錄賦予mysql:mysql權限,否則會起不來 chown mysql:mysql /var/lib/mysql/xbinlog/
重啟mysql服務
2節點修改/etc/my.cnf參數
server_id =2 //這個參數要保證2個節點不一致
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權限,否則會起不來 chown mysql:mysql /var/lib/mysql/ybinlog/
重啟mysql服務
三、在1節點:
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節點
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兩項是不是yes狀態
mysql> show master status;
+---------------+----------+--------------+------------------+-------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 | 120 | | | |
+---------------+----------+--------------+------------------+-------------------+
在1節點
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兩項是不是yes狀態
四、測試,在2個節點都插入數據,然后select驗證一下
五、下載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、本機的主機名與hosts中定義的主機保持一致。
2、各節點要能互相解析主機名,也就是說hosts文件中要有本機的和對方的ip和對應的主機名
3、各個節點的時間必須同步
關閉selinux并清空防火墻規則
[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/
節點1上配置
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs { //全局配置段
notification_email { //定義故障通知郵箱
root@localhost //郵箱地址,可以多個
}
notification_email_from root@localhost //發件人地址
smtp_server localhost //郵件服務器地址
smtp_connect_timeout 30 //聯系郵件服務器的超時時長
router_id red1 //當前物理設備在網絡中的唯一ID標識
}
vrrp_instance VI_1 { //實例名在配置文件中不能重復
state MASTER //MASTER或BACKUP,定義當前節點默認為主或從節點
interface eth0
virtual_router_id 51 //當前虛擬路由的標識符,必須全局唯一
priority 100 //定義當前實例的優先級,備機要小于主機
advert_int 1 //狀態通告周期(秒)---用于主節點向BACKUP節點通告心跳信息
authentication { /消息認證配置
auth_type PASS //認證方式(MD5或PASS簡單字符認證)
auth_pass 1111 //認證密碼,所有同一虛擬實例節點的必須保持一致
}
virtual_ipaddress { //自動配置在interface上的(vip)虛擬IP地址,可以是多個
192.168.140.66 //虛擬ip
}
}
virtual_server 192.168.140.66 { //虛擬ip
delay_loop 6 //RealServer的健康狀態探查的時間間隔
lb_algo rr //負載均衡的調度算法(Scheduler)
lb_kind DR //LVS類型。支持NAT/DR/TUN
persistence_timeout 50 //持久連接超時時間
protocol TCP //只能適用TCP,若適用UDP則使用ops參數
real_server 192.168.140.222 { //1節點ip
weight 1 權重
TCP_CHECK {
connect_timeout 3 健康狀態監測超時時間
nb_get_retry 3 number of get retry
delay_before_retry 3 每次重試之前延遲3秒
# connect_port 3306
}
}
}
節點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
}
}
}
開啟服務 service keepalived start
查看狀態 service keepalived status
查看VIP是否在本節點 ip add //VIP:192.168.140.66會在節點1上,因為1節點的優先級高。
六、測試keepalived
此時在1節點和2節點上執行ip add,會發現VIP 192.168.140.66會飄在1節點上。
然后在1節點上執行service keepalived stop,然后在2節點上執行ip add,會發現vip飄到了2節點。
然后在1節點service keepalived start,會發現vip又回到了1節點。
到此,測試成功
關于如何理解基于keepalived的MySQL主主復制就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。