[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
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。