时间:2021-07-01 10:21:17 帮助过:24人阅读
Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.150.173:3306 wrr persistent 60 -> 192.168.150.171:3306 Local 3 0 0 TCP 192.168.150.174:3306 wrr persistent 60 -> 192.168.150.172:3306 Route 3 0 0 -> 192.168.150.171:3306 Local 1 0 0
Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 192.168.150.173:3306 wrr persistent 60 -> 192.168.150.172:3306 Local 3 0 0 TCP 192.168.150.174:3306 wrr persistent 60 -> 192.168.150.172:3306 Local 3 0 0
tar –zxvf keepalived-1.2.12.tar.gz cd keepalived-1.2.12 ./configure --prefix=/usr/local/keepalived/ make make install ln -s /usr/local/keepalived/etc/keepalived /etc/ ln -s /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ ln -s /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ ln -s /usr/local/keepalived/bin/genhash /bin/ ln -s /usr/local/keepalived/sbin/keepalived /sbin/
server-id = 1 ##master ID binlog-do-db = ppl ##允许同步的库 binlog-ignore-db = mysql ##忽略同步的库,也就是不能同步的库 ##配置文件中还需开启log-bin,例如log-bin = mysql-bin mysql –uroot –p
mysql> grant replication slave on *.* to ‘repdb01’@’%’ identified by '123456'; mysql>create database db01; mysql>flush logs; mysql>show master status; mysql>use db01 mysql> create table test(name char);
server-id = 2 ##slave ID master-host = 192.168.150.171 ##指定master的地址 master-user = repdb01 ##同步所用的账号 master-password = 123456 ##同步所用的密码 master-port = 3306 ##master上mysql的端口 replicate-do-db = db01 ##要同步的库名 replicate-ignore-db = mysql ##忽略的库名 slave-skip-errors = 1062 ##当同步异常时,那些错误跳过,本例为1062错误 #log-slave-updates ##同步的同时,也记录自己的binlog日志,如果还有台slave是通过这台机器进行同步,那需要增加此项, #skip-slave-start ##启动时不自动开启slave进程 #read-only ##将库设为只读模式,只能从master同步,不能直接写入(避免自增键值冲突) mysql –uroot –p
mysql>create database db01; mysql>change master to master_log_file=’mysql-bin.000007’,master_log=106; mysql>slave start; mysql>show slave status \G
Master上的配置 vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id MySQL-HA } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 90 priority 100 advert_int 1 notify_master "/usr/local/mysql/bin/remove_slave.sh" nopreempt authentication { auth_type PASS auth_pass abcd1234 } virtual_ipaddress { 192.168.150.173 label eth0:1 192.168.150.174 label eth0:2 } } virtual_server 192.168.150.173 3306 { delay_loop 2 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.150.171 3306 { weight 3 notify_down /usr/local/mysql/bin/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } } virtual_server 192.168.150.174 3306 { delay_loop 2 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.150.171 3306 { weight 1 notify_down /usr/local/mysql/bin/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } real_server 192.168.150.172 3306 { weight 3 TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } } vi /usr/local/mysql/bin/remove_slave.sh #!/bin/bash user=root password=123456 log=/root/mysqllog/remove_slave.log #-------------------------------------------------------------------------------------- echo "`date`" >> $log /usr/bin/mysql -u$user -p$password -e "set global read_only=OFF;reset master;stop slave;change master to master_host='localhost';" >> $log /bin/sed -i 's#read-only#\#read-only#' /etc/my.cnf chomd 755 /usr/local/mysql/bin/remove_slave.sh vi /usr/local/mysql/bin/mysql.sh #!/bin/bash /etc/init.d/keepalived stop Slave上的配置 vi /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { router_id MySQL-HA } vrrp_instance VI_1 { state BACKUP interface eth0 virtual_router_id 90 priority 99 advert_int 1 notify_master "/usr/local/mysql/bin/remove_slave.sh" authentication { auth_type PASS auth_pass ppl.com } virtual_ipaddress { 192.168.150.173 label eth0:1 192.168.150.174 label eth0:2 } } virtual_server 192.168.150.173 3306 { delay_loop 2 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.150.172 3306 { weight 3 notify_down /usr/local/mysql/bin/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } } virtual_server 192.168.150.174 3306 { delay_loop 2 lb_algo wrr lb_kind DR persistence_timeout 60 protocol TCP real_server 192.168.150.172 3306 { weight 3 notify_down /usr/local/mysql/bin/mysql.sh TCP_CHECK { connect_timeout 10 nb_get_retry 3 delay_before_retry 3 connect_port 3306 } } # real_server 192.168.150.172 3306 { # weight 3 # TCP_CHECK { # connect_timeout 10 # nb_get_retry 3 # delay_before_retry 3 # connect_port 3306 # } # } } vi /usr/local/mysql/bin/remove_slave.sh #!/bin/bash user=root password=123456 log=/root/mysqllog/remove_slave.log #-------------------------------------------------------------------------------------- echo "`date`" >> $log /usr/bin/mysql -u$user -p$password -e "set global read_only=OFF;reset master;stop slave;change master to master_host='localhost';" >> $log /bin/sed -i 's#read-only#\#read-only#' /etc/my.cnf chomd 755 /usr/local/mysql/bin/remove_slave.sh vi /usr/local/mysql/bin/mysql.sh #!/bin/bash /etc/init.d/keepalived stop vi /usr/local/keepalived/bin/lvs-rs.sh #!/bin/bash WEB_VIP=192.168.150.174 . /etc/rc.d/init.d/functions case "$1" in start) ifconfig lo:0 $WEB_VIP netmask 255.255.255.255 broadcast $WEB_VIP /sbin/route add -host $WEB_VIP dev lo:0 echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce sysctl -p >/dev/null 2>&1 echo "RealServer Start OK" ;; stop) ifconfig lo:0 down route del $WEB_VIP >/dev/null 2>&1 echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce echo "RealServer Stoped" ;; status) # Status of LVS-DR real server. islothere=`/sbin/ifconfig lo:0 | grep $WEB_VIP` isrothere=`netstat -rn | grep "lo:0" | grep $web_VIP` if [ ! "$islothere" -o ! "isrothere" ];then # Either the route or the lo:0 device # not found. echo "LVS-DR real server Stopped." else echo "LVS-DR Running." fi ;; *) # Invalid entry. echo "$0: Usage: $0 {start|status|stop}" exit 1 ;; esac exit 0 chmod 755 /usr/local/keepalived/bin/lvs-rs.sh echo “/usr/local/keepalived/bin/lvs-rs.sh start” >>/etc/rc.local
[root@rac3 ~]# ip a 1: lo:mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:50:56:95:06:1f brd ff:ff:ff:ff:ff:ff inet 192.168.150.171.171/24 brd 192.168.0.255 scope global eth0 inet 192.168.150.173/32 scope global eth0:1 inet 192.168.150.174/32 scope global eth0:2 inet6 fe80::250:56ff:fe95:61f/64 scope link valid_lft forever preferred_lft forever 3: sit0: mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0 slave上查看 [root@rac1 keepalive]# ip a 1: lo: mtu 16436 qdisc noqueue link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet 192.168.150.174/32 brd 192.168.150.174 scope global lo:0 inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:50:56:95:5e:b4 brd ff:ff:ff:ff:ff:ff inet 192.168.150.188/24 brd 192.168.0.255 scope global eth0 inet 192.168.150.252/24 brd 192.168.0.255 scope global secondary eth0:1 inet 192.168.150.186/24 brd 192.168.0.255 scope global secondary eth0:4 inet6 fe80::250:56ff:fe95:5eb4/64 scope link valid_lft forever preferred_lft forever 3: eth1: mtu 1500 qdisc pfifo_fast qlen 1000 link/ether 00:50:56:95:11:ba brd ff:ff:ff:ff:ff:ff inet 10.10.10.188/24 brd 10.10.10.255 scope global eth1 inet 169.254.157.163/16 brd 169.254.255.255 scope global eth1:1 inet6 fe80::250:56ff:fe95:11ba/64 scope link valid_lft forever preferred_lft forever 4: sit0: mtu 1480 qdisc noop link/sit 0.0.0.0 brd 0.0.0.0