環境描述:
OS:CentOS6.5_X64
MASTER:192.168.0.202
BACKUP:192.168.0.203
VIP:192.168.0.204mysql
一、配置兩臺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 restartlinux
#先查看下log bin日誌和pos值位置算法
master配置以下:sql
[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; #啓動同步bash
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;ide
#主主同步配置完畢,查看同步狀態Slave_IO和Slave_SQL是YES說明主主同步成功。oop
在master插入數據測試下:測試
在backup查看是否同步成功:spa
能夠看到已經成功同步過去,一樣在backup插入到user表數據,同樣同步過去,雙主就作成功了。
二、配置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 zxvf keepalived-1.2.7.tar.gz
[root@master ~]# cd keepalived-1.2.7
[root@master ~]#./configure --prefix=/usr/local/keepalived
make && make install
#將keepalived配置成系統服務
[root@master ~]# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
[root@master ~]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@master ~]# mkdir /etc/keepalived/
[root@master ~]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/
[root@master ~]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
[root@master ~]# vi /etc/keepalived/keepalived.conf
! Configuration File forkeepalived
global_defs {
notification_email {
test@sina.com
}
notification_email_from admin@test.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.0.204
}
}
virtual_server 192.168.0.204 3306 {
delay_loop 2
#lb_algo rr #LVS算法,用不到,咱們就關閉了
#lb_kind DR #LVS模式,若是不關閉,備用服務器不能經過VIP鏈接主MySQL
persistence_timeout 50 #同一IP的鏈接60秒內被分配到同一臺真實服務器
protocol TCP
real_server 192.168.0.202 3306 { #檢測本地mysql,backup也要寫檢測本地mysql
weight 3
notify_down /usr/local/keepalived/mysql.sh #當mysq服down時,執行此腳本,殺死keepalived實現切換
TCP_CHECK {
connect_timeout 3 #鏈接超時
nb_get_retry 3 #重試次數
delay_before_retry 3 #重試間隔時間
}
}
[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
#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;
三、測試高可用性
一、經過Mysql客戶端經過VIP鏈接,看是否鏈接成功。
二、中止master這臺mysql服務,是否能正常切換過去,可經過ip addr命令來查看VIP在哪臺服務器上。
三、可經過查看/var/log/messges日誌,看出主備切換過程四、master服務器故障恢復後,是否主動搶佔資源,成爲活動服務器。