文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

keepalived+双主

2024-04-02 19:55

关注

[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


阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-数据库
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯