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
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
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.%
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。html
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;
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
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:mysql
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狀態
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狀態
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
1 [root@Master01 ~]# vim /etc/keepalived/keepalived.conf 2 ! Configuration File for keepalived 3 global_defs { 4 notification_email { 5 x120952576@126.com 6 #表示keepalived在發生諸如切換操做時發送Email給哪些地址,郵件地址能夠多個,每行一個。 7 } 8 notification_email_from Alexandre.Cassen@firewall.loc 9 smtp_server 172.24.8.10 10 smtp_connect_timeout 30 11 router_id LVS_DEVEL 12 } 13 vrrp_instance VI_1 { 14 state BACKUP 15 interface eth0 16 virtual_router_id 51 17 #這裏設置VRID,這裏很是重要,相同的VRID爲一個組,他將決定多播的MAC地址 18 priority 100 19 advert_int 1 20 nopreempt 21 #不搶佔,只在優先級高的機器上設置便可,優先級低的機器不設置 22 authentication { 23 auth_type PASS 24 auth_pass 1111 25 } 26 track_script { 27 check_mysqld #執行定義的監控腳本 28 } 29 virtual_ipaddress { 30 172.24.8.12 31 } 32 } 33 vrrp_script check_mysqld { 34 script"/etc/keepalived/mysqlcheck/keepalived_check_mysql.sh " 35 interval 2 36 }
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 #mysql is 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 14 MYSQL_OK=1 15 else 16 MYSQL_OK=0 17 fi 18 return $MYSQL_OK 19 } 20 while [ $CHECK_TIME -ne 0 ] 21 do 22 let "CHECK_TIME -= 1" 23 check_mysql_helth 24 if [ $MYSQL_OK = 1 ] ; then 25 CHECK_TIME=0 26 exit 0 27 fi 28 if [ $MYSQL_OK -eq 0 ] && [ $CHECK_TIME -eq 0 ] 29 then 30 /etc/init.d/keepalived stop 31 exit 1 32 fi 33 sleep 1 34 done 35 [root@Master01 ~]# chmod u+x /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh 36 [root@Master01 ~]# service keepalived start
1 [root@Master01 ~]# scp /etc/keepalived/keepalived.conf root@127.24.8.11:/etc/keepalived/
1 [root@Master02 ~]# mkdir -p /etc/keepalived/mysqlcheck/ 2 [root@Master01 ~]# scp /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh root@172.24.8.11:/etc/keepalived/mysqlcheck/ 3 [root@Master02 ~]# chmod u+x /etc/keepalived/mysqlcheck/keepalived_check_mysql.sh 4 [root@Master02 ~]# service keepalived start
1 [root@test ~]# mysql -uroot -h172.24.8.12 -p
1 mysql> show variables like "%hostname%"; 2 mysql> show variables like "%server_id%";
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;
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