1. 程式人生 > >keepalived+雙主

keepalived+雙主

pre def wall lld primary ret protocol upd lob

[root@mini1 ~]# setenforce 0

[root@mini1 ~]# systemctl stop firewalld.service

[root@mini1 ~]# vim /etc/my.cnf

server_id=1

log-bin=mysql-bin

gtid-mode=on

enforce-gtid-consistency=1

log_slave_updates = 1

binlog-format=mixed

auto-increment-increment=2 #自動增長步長,三臺mysql這設置為3

auto-increment-offset=1 #自增長初始值

[root@mini1 ~]# service mysqld start

[root@mini2 ~]# setenforce 0

[root@mini2 ~]# systemctl stop firewalld.service

[root@mini2 ~]# vim /etc/my.cnf

server_id=2

log-bin=mysql-bin

gtid-mode=on

enforce-gtid-consistency=1

log_slave_updates = 1

binlog-format=mixed

auto-increment-increment=2

auto-increment-offset=2

[root@mini2 ~]# service mysqld start

#設置雙主同步

[root@mini1 ~]# mysql -uroot -p123456

mysql> grant replication slave on *.* to rep@'192.168.1.%' identified by '123456';

[root@mini2 ~]# mysql -uroot -p123456

mysql> change master to master_host='192.168.1.128',master_user='rep',master_password='123456',master_port=3306,master_auto_position=1;

mysql> start slave;

mysql> show slave status\G;

mysql> grant replication slave on *.* to rep@'192.168.1.%' identified by '123456';

[root@mini1 ~]# mysql -uroot -p123456

mysql> change master to master_host='192.168.1.128',master_user='rep',master_password='123456',master_port=3306,master_auto_position=1;

mysql> start slave;

mysql> show slave status\G;

#測試是否同步

mysql> create database test;

mysql> create table test.t1(id int primary key auto_increment,name varchar(10));

mysql> insert into test.t1(name) values('tom1');

#登錄master2查看

mysql> select * from test.t1;

+----+------+

| id | name |

+----+------+

| 2 | tom1 |

| 4 | tom1 |

+----+------+

#在master1與master2上安裝keepalived

#keepalived依賴包

[root@mini1 ~]# yum -y install kernel-devel openssl-devel popt-devel

[root@mini1 ~]# tar zxf keepalived-2.0.6.tar.gz

[root@mini1 ~]# cd keepalived-2.0.6/

[root@mini1 keepalived-2.0.6]# ./configure --prefix=/ --with-kernel-dir= /usr/src/kernels/3.10.0-862.9.1.el7.x86_64/ &&make &&make install

[root@mini1 ~]# vim /etc/keepalived/keepalived.conf

[root@mini1 ~]# vim /etc/keepalived/keepalived.conf


! Configuration File for keepalived


global_defs {

router_id mysql_1

}

state BACKUP

interface eno16777728

virtual_router_id 51

auth_type PASS

auth_pass 1111

! Configuration File for keepalived


global_defs {

router_id mysql_1

}


vrrp_instance VI_1 {

state BACKUP

interface eno16777728

virtual_router_id 51

priority 100

advert_int 1

nopreempt

authentication {

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.1.100

}

}


virtual_server 192.168.1.100 3306 {

delay_loop 6

lb_algo rr

lb_kind DR

persistence_timeout 50

protocol TCP


real_server 192.168.1.128 3306 {

weight 1

notify_down /etc/keepalived/bin/mysql.sh

TCP_CHECK {

connect_timeout 3

retry 3

delay_before_retry 3

connect_port 3306

}

}

}

[root@mini1 ~]# mkdir /etc/keepalived/bin

[root@mini1 ~]# vim /etc/keepalived/bin/mysql.sh

#!/bin/bash

pkill keepalived

/sbin/ifdown eno16777728 && /sbin/ifup eno16777728

[root@mini1 ~]# chmod +x /etc/keepalived/bin/mysql.sh

[root@mini1 ~]# systemctl start keepalived.service

[root@mini1 ~]# ss -lanp |grep keepa

p_raw UNCONN 0 0 rarp:* * users:(("keepalived",pid=17863,fd=7))

u_dgr UNCONN 0 0 * 25852 * 8724 users:(("keepalived",pid=17863,fd=3),("keepalived",pid=17862,fd=3),("keepalived",pid=17861,fd=3))

raw UNCONN 0 0 *:112 *:* users:(("keepalived",pid=17863,fd=9))

raw UNCONN 0 0 *:112 *:* users:(("keepalived",pid=17863,fd=8))

raw UNCONN 0 0 *:255 *:* users:(("keepalived",pid=17862,fd=5))

#master2給以上一樣,只需修改配置文件幾個地方和mysql.sh

[root@mini2 ~]# vim /etc/keepalived/keepalived.conf


! Configuration File for keepalived


global_defs {

router_id mysql_2

}


state BACKUP

interface eno16777736

virtual_router_id 51

priority 50

auth_type PASS

auth_pass 1111

}

virtual_ipaddress {

192.168.1.100

}

}


virtual_server 192.168.1.100 3306 {

delay_loop 6

lb_algo rr

lb_kind DR

persistence_timeout 50

protocol TCP

real_server 192.168.1.129 3306 {

weight 1

notify_down /etc/keepalived/bin/mysql.sh

TCP_CHECK {

connect_timeout 3

retry 3

delay_before_retry 3

connect_port 3306

}

}

}

[root@mini2 ~]# vim /etc/keepalived/bin/mysql.sh

#!/bin/bash

pkill keepalived

/sbin/ifdown eno16777736 && /sbin/ifup eno16777736

[root@mini1 ~]# chmod +x /etc/keepalived/bin/mysql.sh

[root@mini1 ~]# ip addr

2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

link/ether 00:0c:29:d8:de:a3 brd ff:ff:ff:ff:ff:ff

inet 192.168.1.128/24 brd 192.168.1.255 scope global noprefixroute dynamic eno16777728

valid_lft 5353953sec preferred_lft 5353953sec

inet 192.168.1.100/32 scope global eno16777728

valid_lft forever preferred_lft forever

[root@mini1 ~]# service mysqld stop

Shutting down MySQL............ SUCCESS!

[root@mini1 ~]# ip addr

2: eno16777728: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

link/ether 00:0c:29:d8:de:a3 brd ff:ff:ff:ff:ff:ff

inet 192.168.1.128/24 brd 192.168.1.255 scope global noprefixroute dynamic eno16777728

valid_lft 5353814sec preferred_lft 5353814sec

inet6 fe80::20c:29ff:fed8:dea3/64 scope link noprefixroute

valid_lft forever preferred_lft forever

[root@mini2 ~]# ip addr

2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000

link/ether 00:0c:29:da:f7:a1 brd ff:ff:ff:ff:ff:ff

inet 192.168.1.129/24 brd 192.168.1.255 scope global noprefixroute dynamic eno16777736

valid_lft 5353785sec preferred_lft 5353785sec

inet 192.168.1.100/32 scope global eno16777736

valid_lft forever preferred_lft forever

inet6 fe80::20c:29ff:feda:f7a1/64 scope link noprefixroute

valid_lft forever preferred_lft forever


keepalived+雙主