004.MySQL雙主+Keepalived高可用
一 基礎環境
主機名 |
系統版本 |
MySQL版本 |
主機IP |
Master01 |
CentOS 6.8 |
MySQL 5.6 |
172.24.8.10 |
Master02 |
CentOS 6.8 |
MySQL 5.6 |
172.24.8.11 |
VIP |
172.24.8.12 |
二 實際部署
2.1 安裝MySQL
1 [root@Master01 ~]# yum list installed | grep mysql#檢視是否存在其他MySQL元件 2 [root@Master01 ~]# yum -y remove mysql-libs.x86_64#為避免衝突引發報錯,解除安裝已存在的元件
1 [root@Master01 study]#yum localinstall mysql-commu* -y 2 [root@Master01 ~]# chkconfig --list | grep mysqld#檢視MySQL是否加入啟動項 3 [root@Master01 ~]# chkconfig mysqld on
2.2 初始化MySQL
1 [root@Master01 ~]# service mysqld start 2 [root@Master01 ~]# mysql_secure_installation 3 [root@Master01 ~]# service iptables stop 4 [root@Master01 ~]# chkconfig iptables off 5 [root@Master01 ~]# vi /etc/sysconfig/selinux 6 SELINUX=disabled
注意:以上操作在Master02主機上也需要執行。
安裝參考:http://www.cnblogs.com/007sx/p/7083143.html
2.3 master01 my.cf配置
1 [root@Master01 ~]# vi /etc/my.cnf 2 [mysqld] 3 …… 4 server-id=1#設定主伺服器master的id 5 log-bin=mysql-bin#配置二進位制變更日誌命名格式 6 replicate-wild-ignore-table=mysql.% 7 replicate-wild-ignore-table=test.% 8 replicate-wild-ignore-table=information_schema.%
注意:
master開啟二進位制日誌後預設記錄所有庫所有表的操作,可以通過配置來指定只記錄指定的資料庫甚至指定的表的操作,具體在mysql配置檔案的[mysqld]可新增修改如下選項:
方法一:
1 # 不同步哪些資料庫 2 binlog-ignore-db = mysql 3 binlog-ignore-db = test 4 binlog-ignore-db = information_schema 5 # 只同步哪些資料庫,除此之外,其他不同步 6 binlog-do-db = mysqltest
方法二:
建議採用以下方式配置過濾:
1 replicate-wild-ignore-table=mysql.%#從庫配置不同步表 2 replicate-wild-do-table=test.%#從庫配置需要同步的表
提示:不要在主庫使用binlog-ignore-db和binlog-do-db,也不要在從庫使用replicate-wild-ignore和replicate-wild-do-table。
2.4 建立賬號
1 [root@Master01 ~]# mysql -uroot -p 2 Enter password: 3 mysql> grant replication slave on *.* to 'repl_user'@'172.24.8.11' identified by 'x12345678'; 4 mysql> grant all privileges on *.* to 'root'@'172.24.8.%'identified by 'x120952576' with grant option; 5 mysql> flush privileges; 6 [root@Master01 ~]# service mysqld restart 7 [root@Master01 ~]# mysql -uroot -p 8 Enter password: 9 mysql> show master status;
master01:
file:mysql-bin.000001
position:120
2.5 smaster02 my.cf配置配置
1 [root@Master02 ~]# vi /etc/my.cnf 2 [mysqld] 3 server-id=2#設定主伺服器master的id 4 log-bin=mysql-bin#配置二進位制變更日誌命名格式 5 replicate-wild-ignore-table=mysql.% 6 replicate-wild-ignore-table=test.% 7 replicate-wild-ignore-table=information_schema.% 8 read_only=1
2.6 建立賬號
1 [root@Master02 ~]# mysql -uroot -p 2 Enter password: 3 mysql> grant replication slave on *.* to 'repl_user'@'172.24.8.10' identified by 'x12345678'; 4 mysql> grant all privileges on *.* to 'root'@'172.24.8.%'identified by 'x120952576' with grant option; 5 mysql> flush privileges; 6 [root@Master02 ~]# service mysqld restart 7 [root@Master02 ~]# mysql -uroot -p 8 Enter password: 9 mysql> show master status;
master02:
file:mysql-bin.000001
position:120
三 啟動主從
3.1 手動同步
如果Master01和Master02已經存在資料,則在開啟主備複製之前,需要將Master01和Master02手動同步一次(/var/lib/mysql整個目錄打包tar.gz),具體方法略。
注意:本實驗都是重新安裝的MySQL,可直接啟動同步。
3.2 啟動Master01的slave功能
1 [root@Master01 ~]# service mysqld restart 2 [root@Master01 ~]# mysql -uroot -p 3 Enter password: 4 mysql> change master to master_host='172.24.8.11', 5 master_user='repl_user', 6 master_password='x12345678', 7 master_log_file='mysql-bin.000001', 8 master_port=3306, 9 master_log_pos=120; 10 mysql> start slave; 11 mysql> show slave status\G#檢視slave狀態
3.3 啟動Master02的slave功能
1 [root@Master02 ~]# service mysqld restart 2 [root@Master02 ~]# mysql -uroot -p 3 Enter password: 4 mysql> change master to master_host='172.24.8.10', 5 master_user='repl_user', 6 master_password='x12345678', 7 master_log_file='mysql-bin.000001', 8 master_log_pos=120; 9 mysql> start slave; 10 mysql> show slave status\G#檢視slave狀態
提示:
slave的I/O和SQL執行緒都已經開始執行,而且Seconds_Behind_Master不再是NULL。日誌的位置增加了,意味著一些事件被獲取並執行了。如果你在master上進行修改,你可以在slave上看到各種日誌檔案的位置的變化,同樣,你也可以看到資料庫中資料的變化。
四 安裝Keepalived
4.1 下載
1 [root@Master01 ~]# wget http://www.keepalived.org/software/keepalived-1.3.6.tar.gz 2 [root@Master01 ~]# tar -zvxf keepalived-1.3.6.tar.gz -C /tmp/ 3 [root@Master01 ~]# cd /tmp/keepalived-1.3.6 4 [root@Master01 keepalived-1.3.6]# ./configure --prefix=/usr/local/keepalived/ --sysconf=/etc --with-init=SYSV 5 #注:(upstart|systemd|SYSV|SUSE|openrc) #根據你的系統選擇對應的啟動方式 6 [root@Master01 keepalived-1.3.6]# make && make install 7 [root@Master01 ~]# ln -s /usr/local/keepalived/sbin/keepalived /sbin 8 [root@Master01 ~]# chmod u+x /etc/init.d/keepalived 9 [root@Master01 ~]# chkconfig --add keepalived 10 [root@Master01 ~]# chkconfig --level 35 keepalived on
注意:Master02上也需要如上操作。
若出現以下提示,需要執行:yum -y install openssl-devel。
提示:也可採用yum install -y keepalived安裝(個人不建議)。
4.2 Master01配置Keepalived
預設情況下keepalived啟動時會去/etc/keepalived目錄下找配置檔案。
1 [root@Master01 ~]# vim /etc/keepalived/keepalived.conf 2 ! Configuration File for keepalived 3 global_defs { 4notification_email { [email protected] 6 #表示keepalived在發生諸如切換操作時傳送Email給哪些地址,郵件地址可以多個,每行一個。 7} 8notification_email_from [email protected] 9smtp_server 172.24.8.10 10smtp_connect_timeout 30 11router_id LVS_DEVEL 12 } 13 vrrp_instance VI_1 { 14state BACKUP 15interface eth0 16virtual_router_id 51 17 #這裡設定VRID,這裡非常重要,相同的VRID為一個組,他將決定多播的MAC地址 18priority 100 19advert_int 1 20nopreempt 21 #不搶佔,只在優先順序高的機器上設定即可,優先順序低的機器不設定 22authentication { 23auth_type PASS 24auth_pass 1111 25} 26 track_script { 27check_mysqld#執行定義的監控指令碼 28 } 29virtual_ipaddress { 30172.24.8.12 31} 32 } 33 vrrp_script check_mysqld { 34script"/etc/keepalived/mysqlcheck/keepalived_check_mysql.sh " 35interval 2 36 }
4.3 建立檢測指令碼
1 [root@Master01 ~]# mkdir -p /etc/keepalived/mysqlcheck/ 2 [root@Master01 ~]# vi /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh 3 #!/bin/bash 4 MYSQL=/usr/bin/mysql 5 MYSQL_HOST=localhost 6 MYSQL_USER=root 7 MYSQL_PASSWORD=x120952576 8 CHECK_TIME=3 9 #mysqlis workingMYSQL_OK is 1 , mysql down MYSQL_OK is 0 10 MYSQL_OK=1 11 function check_mysql_helth (){ 12 $MYSQL -h $MYSQL_HOST -u $MYSQL_USER -p${MYSQL_PASSWORD} -e "show status;" >/dev/null 2>&1 13 if [ $? = 0 ] ;then 14MYSQL_OK=1 15 else 16MYSQL_OK=0 17 fi 18 return $MYSQL_OK 19 } 20 while [ $CHECK_TIME -ne 0 ] 21 do 22let "CHECK_TIME -= 1" 23check_mysql_helth 24if [ $MYSQL_OK = 1 ] ; then 25CHECK_TIME=0 26exit 0 27fi 28if [ $MYSQL_OK -eq 0 ] &&[ $CHECK_TIME -eq 0 ] 29then 30/etc/init.d/keepalived stop 31exit 1 32fi 33sleep 1 34 done 35 [root@Master01 ~]# chmod u+x /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh 36 [root@Master01 ~]# service keepalived start
4.4 Master02配置Keepalived
1 [root@Master01 ~]# scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/
參考Master01配置,去掉nopreempt選項,priority設定比Master01低即可。
4.5 建立檢測指令碼
1 [root@Master02 ~]# mkdir -p /etc/keepalived/mysqlcheck/ 2 [root@Master01 ~]# scp /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh [email protected]:/etc/keepalived/mysqlcheck/ 3 [root@Master02 ~]# chmod u+x /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh 4 [root@Master02 ~]# service keepalived start
五 功能測試
5.1 測試vip
1 [root@test ~]# mysql -uroot -h172.24.8.12 -p
1 mysql> show variables like "%hostname%"; 2 mysql> show variables like "%server_id%";
5.2 同步測試
1 [root@test ~]# mysql -uroot -p 2 Enter password: 3 mysql> create database mysqltest; 4 mysql> use mysqltest; 5 mysql> create table user(id int(5),name char(10)); 6 mysql> insert into user values (00001,'zhangsan'); 7 在Slave從伺服器上進行驗證: 8 [root@Master02 ~]# mysql -uroot -p 9 Enter password: 10 mysql> show databases; 11 mysql> select * from mysqltest.user;
5.3 測試Keepalived切換
1 [root@Master01 ~]# service mysqld stop#停止Master01的MySQL 2 [root@Master01 ~]# tail -f /var/log/messages#觀察Master01的日誌
1 [root@Master02 ~]# tail -f /var/log/messages#觀察Master02的日誌
1 [root@Client ~]# mysql -uroot -h172.24.8.12 -px120952576#客戶端連線VIP
注意:已經成功切換,在切換過程中可能中斷幾秒。