用keepalived做mysql高可用

2023-12-13 03:50:55

两台机器(centos7系统)安装mysql

[root@mysql-keep-master ~]# wget https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
[root@mysql-keep-master ~]# rpm -ivh mysql80-community-release-el7-3.noarch.rpm
[root@mysql-keep-master ~]# yum -y install yum-utils #安装yum工具包
[root@mysql-keep-master ~]# vim /etc/yum.repos.d/mysql-community.repo

安装mysql

[root@mysql-keep-master ~]# yum install -y mysql-community-server
[root@mysql-keep-master ~]# systemctl start mysqld 
[root@mysql-keep-master ~]# systemctl enable mysqld

修改密码

[root@mysql-keep-master ~]# grep pass /var/log/mysqld.log 
2020-02-18T13:54:25.706635Z 1 [Note] A temporary password is generated for root@localhost: %baWi=4!NsT)
[root@mysql-keep-master ~]# mysqladmin -uroot -p'%baWi=4!NsT)' password 'Admin@123!'
 制作互为主从:
[root@mysql-keep-master ~]# vim /etc/my.cnf   #编辑配置文件开启gtid
log-bin=m1.log  
server-id=1 
gtid_mode = ON    #开启gtid
enforce_gtid_consistency=1  #强制gtid移植性

[root@mysql-keep-master ~]# systemctl restart mysqld 
[root@mysql-keep-master ~]# mysql -uroot -p'Admin@123!'  #登录数据库

 mysql> GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY 'Admin@123';  #创建远程登录用户

mysql> flush privileges;
mysql> grant replication  slave,reload,super  on *.*  to  'slave'@'%'  identified by  'Admin@123';  ---创建主从授权用户

mysql> flush privileges;  #刷新授权
mysql> \e   #指定主服务器信息
CHANGE MASTER TO
MASTER_HOST='192.168.58.136',      #指定主服务器的ip或者主机名
MASTER_USER='slave',           #授权用户
MASTER_PASSWORD='QianFeng@123',  #授权用户密码
master_auto_position=1
-> ;
启动slave
mysql> start slave;

修改配置文件

[root@mysql-keep-backup ~]# vim /etc/my.cnf
log-bin=m2.log
server-id=2
gtid_mode = ON
enforce_gtid_consistency=1
[root@mysql-keep-backup ~]# systemctl restart mysqld 
[root@mysql-keep-backup ~]# mysql -uroot -p'QianFeng@123!'
mysql> GRANT ALL ON *.* TO 'root'@'%' IDENTIFIED BY 'QianFeng@123';
mysql> flush privileges;
mysql> grant replication  slave,reload,super  on *.*  to  'slave'@'%'  identified by  'QianFeng@123';
mysql> flush privileges;

指定主服务器信息

mysql> \e
CHANGE MASTER TO
MASTER_HOST='192.168.58.135',
MASTER_USER='slave',
MASTER_PASSWORD='QianFeng@123',
master_auto_position=1
-> ;

启动slave

mysql> start slave;

两台机器验证是否为yes

mysql> show slave status\G 

两台机器安装keepalived

[root@mysql-keep-master ~]# yum -y install keepalived
[root@mysql-keep-master ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@mysql-keep-master ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
 
global_defs {
   router_id master1
}
 
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 80
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.58.16/24
    }
}

将配置文件拷贝到backu机器

[root@mysql-keep-backup ~]# mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@mysql-keep-master ~]# scp /etc/keepalived/keepalived.conf 192.168.58.136:/etc/keepalived/
[root@mysql-keep-backup ~]# vim /etc/keepalived/keepalived.conf  #修改如下
! Configuration File for keepalived
 
global_defs {
   router_id backup1
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface ens33
    virtual_router_id 80
    priority 50
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.58.16/24
    }
}

两台机器启动keepalived

[root@mysql-keep-master ~]# systemctl start keepalived
[root@mysql-keep-backup ~]# systemctl start keepalived
[root@mysql-keep-master ~]# ip a   #vip在master上面
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:92:0a:64 brd ff:ff:ff:ff:ff:ff
    inet 192.168.58.135/24 brd 192.168.58.255 scope global dynamic ens33
       valid_lft 1729sec preferred_lft 1729sec
    inet 192.168.58.16/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::86f4:1d74:ad05:c61c/64 scope link 
       valid_lft forever preferred_lft forever

测试:

[root@mysql-keep-master ~]# mysql -uroot -p'Admin@123' -h 192.168.58.16  #登录vip
mysql> create database test1;
Query OK, 1 row affected (0.07 sec)
 
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.08 sec)
验证
[root@mysql-keep-backup ~]# mysql -uroot -p'Admin@123!'
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test1              |
+--------------------+
5 rows in set (0.12 sec)

[root@mysql-keep-master ~]# systemctl stop keepalived #关闭master
[root@mysql-keep-backup ~]# ip a  #vip切换backup机器
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:a2:0f:5c brd ff:ff:ff:ff:ff:ff
    inet 192.168.58.136/24 brd 192.168.58.255 scope global dynamic ens33
       valid_lft 1547sec preferred_lft 1547sec
    inet 192.168.58.16/24 scope global secondary ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::af6e:2eff:e2d1:99cb/64 scope link 
       valid_lft forever preferred_lft forever

测试登录

[root@mysql-keep-master ~]# mysql -uroot -p'Admin@123' -h 192.168.58.16

文章来源:https://blog.csdn.net/xubaodong_1/article/details/134869482
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。