您好,登錄后才能下訂單哦!
環境描述:
OS:CentOS6.5_X64
MySQL-VIP:192.168.1.200
MySQL-master1:192.168.1.201
MySQL-master2:192.168.1.202
1、配置兩臺Mysql主主同步
[root@master ~]# yum install mysql-server mysql -y [root@master ~]# service mysqld start [root@master ~]# mysqladmin -u root password 123.com [root@master ~]# vi /etc/my.cnf #開啟二進制日志,設置id [mysqld] server-id = 1 #backup這臺設置2 log-bin = mysql-bin binlog-ignore-db = mysql,information_schema #忽略寫入binlog日志的庫 auto-increment-increment = 2 #字段變化增量值 auto-increment-offset = 1 #初始字段ID為1 slave-skip-errors = all #忽略所有復制產生的錯誤 [root@master ~]# service mysqld restart
如果是Centos7,則卸載mariadb,安裝mysql,參考文章:https://blog.51cto.com/sf1314/2073389
安裝mysql,參考文章:
# mysql -uroot -p'密碼'
先查看下log bin日志和pos值位置
master配置如下:
[root@ master ~]# mysql -u root -p123.com mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.0.%' IDENTIFIED BY 'replication'; mysql> flush privileges; mysql> change master to -> master_host='192.168.0.203', -> master_user='replication', -> master_password='replication', -> master_log_file='mysql-bin.000002', -> master_log_pos=106; #對端狀態顯示的值 mysql> start slave; #啟動同步
backup配置如下:
[root@backup ~]# mysql -u root -p123.com mysql> GRANT REPLICATION SLAVE ON *.* TO 'replication'@'192.168.0.%' IDENTIFIED BY 'replication'; mysql> flush privileges; mysql> change master to -> master_host='192.168.0.202', -> master_user='replication', -> master_password='replication', -> master_log_file='mysql-bin.000002', -> master_log_pos=106; mysql> start slave;
#主主同步配置完畢,查看同步狀態Slave_IO和Slave_SQL是YES說明主主同步成功。
在master插入數據測試下:
在backup查看是否同步成功:
可以看到已經成功同步過去,同樣在backup插入到user表數據,一樣同步過去,雙主就做成功了。
2、配置keepalived實現熱備
[root@backup ~]# yum install -y pcre-devel openssl-devel popt-devel #安裝依賴包
[root@master ~]# wget http://www.keepalived.org/software/keepalived-1.2.7.tar.gz [root@master ~]# tar -xf keepalived-1.2.7.tar.gz -C /usr/local/src/ [root@master ~]# cd /usr/local/ [root@master ~]# ln -sv /usr/local/src/keepalived-1.2.7/ keepalived #根據實際情況修改 [root@master ~]# cd keepalived [root@master ~]#./configure --prefix=/usr/local/keepalived make && make install
#將keepalived配置成系統服務
[root@master ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/ chmod +x /etc/init.d/keepalived [root@master ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/ [root@master ~]# chkconfig --add keepalived [root@master ~]# chkconfig keepalived on [root@master ~]# mkdir /etc/keepalived [root@master ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/ [root@master ~]# cp /usr/local/sbin/keepalived /usr/sbin/ #或者ln -s /usr/local/sbin/keepalived /usr/sbin/
配置keepalived
我們自己在新建一個配置文件,默認情況下keepalived啟動時會去/etc/keepalived目錄下找配置文件
[root@master ~]# vi /etc/keepalived/keepalived.conf global_defs { notification_email { 862572301@qq.com } notification_email_from 862572301@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MYSQL_HA #標識,雙主相同 } vrrp_instance VI_1 { state BACKUP #兩臺配置此處均是BACKUP interface eth0 virtual_router_id 51 #主備相同 priority 100 #優先級,另一臺backup改為90 advert_int 1 nopreempt #不搶占,只在優先級高master的機器上設置即可,優先級低backup的機器不設置 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.1.200 } } virtual_server 192.168.1.200 3306 { delay_loop 2 #每個2秒檢查一次real_server狀態 #lb_algo wrr #LVS算法,用不到,我們就關閉了 #lb_kind DR #LVS模式,如果不關閉,備用服務器不能通過VIP連接主MySQL persistence_timeout 60 #會話保持時間,同一IP的連接60秒內被分配到同一臺真實服務器 protocol TCP real_server 192.168.1.201 3306 { #檢測本地mysql,backup也要寫檢測本地mysql weight 3 notify_down /usr/local/keepalived/mysql.sh #當mysq服down時,執行此腳本,殺死keepalived實現切換 TCP_CHECK { connect_timeout 10 #連接超時時間 nb_get_retry 3 #重連次數 delay_before_retry 3 #重連間隔時間 connect_port 3306 #健康檢查端口 } } }
編寫檢測服務down后所要執行的腳本 [root@master ~]# vi /usr/local/keepalived/mysql.sh #!/bin/bash pkill keepalived [root@master ~]# chmod +x /usr/local/keepalived/mysql.sh [root@master ~]# /etc/init.d/keepalived start
注:此腳本是上面配置文件notify_down選項所用到的,keepalived使用notify_down選項來檢查real_server 的服務狀態,當發現real_server服務故障時,便觸發此腳本;我們可以看到,腳本就一個命令,通過pkill keepalived強制殺死keepalived進程,從而實現了MySQL故障自動轉移。另外,我們不用擔心兩個MySQL會同時提供數據更新操作, 因為每臺MySQL上的keepalived的配置里面只有本機MySQL的IP+VIP,而不是兩臺MySQL的IP+VIP
啟動keepalived
[root@master ~]# /usr/local/keepalived/sbin/keepalived –D 或者/etc/init.d/keepalived start [root@master ~]# ps -aux | grep keepalived
測試
找一臺局域網PC,然后去ping MySQL的VIP,這時候MySQL的VIP是可以ping的通的
停止MySQL服務,看keepalived健康檢查程序是否會觸發我們編寫的腳本
#backup服務器只修改priority為90、nopreempt不設置、real_server設置本地IP。
#授權兩臺Mysql服務器允許root遠程登錄,用于在其他服務器登陸測試!
mysql> grant all on *.* to'root'@'192.168.0.%' identified by '123.com';
mysql> flush privileges;
3、測試高可用性
1、通過Mysql客戶端通過VIP連接,看是否連接成功。
2、停止master這臺mysql服務,是否能正常切換過去,可通過ip addr命令來查看VIP在哪臺服務器上。
3、可通過查看/var/log/messges日志,看出主備切換過程
4、master服務器故障恢復后,是否主動搶占資源,成為活動服務器。
附:keepalived-1.2.7 keepalived實現服務高可用 http://down.51cto.com/data/2440924
---------------------------------------------------------------------------------------------
針對網卡做bond的做VIP: https://blog.51cto.com/sf1314/2073519
[root@master ~]# vi /etc/keepalived/keepalived.conf global_defs { notification_email { 862572301@qq.com } notification_email_from 862572301@qq.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id MYSQL_HA #標識,雙主相同 } vrrp_instance VI_1 { state BACKUP #兩臺配置此處均是BACKUP interface bond0.101 #------->這邊指定配置的聚合網卡bond0.101 virtual_router_id 51 #主備相同 priority 100 #優先級,另一臺backup改為90 advert_int 1 nopreempt #不搶占,只在優先級高master的機器上設置即可,優先級低backup的機器不設置 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 10.104.101.13/24 #------->這邊設置虛擬的VIP地址 } } virtual_server 10.104.101.13/24 3306 { #--------->指定虛擬VIP地址的配置信息 delay_loop 2 #每個2秒檢查一次real_server狀態 #lb_algo wrr #LVS算法,用不到,我們就關閉了 #lb_kind DR #LVS模式,如果不關閉,備用服務器不能通過VIP連接主MySQL persistence_timeout 60 #會話保持時間,同一IP的連接60秒內被分配到同一臺真實服務器 protocol TCP real_server 10.104.101.12 3306 { #10.104.101.12指本地配置的IP地址,檢測本地mysql,backup也要寫檢測本地mysql weight 3 notify_down /usr/local/keepalived/mysql.sh #當mysq服down時,執行此腳本,殺死keepalived實現切換 TCP_CHECK { connect_timeout 10 #連接超時時間 nb_get_retry 3 #重連次數 delay_before_retry 3 #重連間隔時間 connect_port 3306 #健康檢查端口 } } }
附:修改mysql賬戶下的replication的密碼可以參考本文章:https://blog.51cto.com/sf1314/2094562
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。