用keepalived做mysql高可用

两台机器(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
相关推荐
友友马几秒前
『QT』窗口 (一)
开发语言·数据库·qt
q***78372 分钟前
SQL实现md5加密方法
数据库·sql
q***614113 分钟前
Spring中Aware的用法以及实现
java·数据库·spring
红树林0722 分钟前
渗透测试之sql注入--报错注入
数据库·sql·安全·web安全
菜鸟小九1 小时前
mysql(锁)
数据库·mysql·oracle
c***42101 小时前
【Sql Server】随机查询一条表记录,并重重温回顾下自定义函数的封装和使用
数据库·性能优化
q***44812 小时前
PostgreSQL的备份方式
数据库·postgresql
v***59832 小时前
【SQL Server】超详细SQLServer日期转换、字符串、数学、聚合等常用函数大全(最新版)
数据库·sqlserver
q***23572 小时前
python的sql解析库-sqlparse
数据库·python·sql
云和数据.ChenGuang2 小时前
运维面试题之oracle和mysql单表最大容量
运维·mysql·oracle