MySQL高可用性之Keepalived+MySQL(双主热备)

环境说明:

操作系统 IP地址 hostname 软件
centos 7 192.168.11.83 web01 mariadb+keepalived
centos 7 192.168.11.84 web02 mariadb+keepalived

准备工作:

(1)关闭两台服务器的防火墙、Selinux应用。

[root@web01 ~]#  setenforce 0
[root@web01 ~]#  systemctl stop firewalld
[root@web02 ~]#  setenforce 0
[root@web02 ~]#  systemctl stop firewalld

(2)两台服务器都安装好mariadb和keepalived

[root@web01 ~]#  yum -y install mariadb mariadb-server keepalived
[root@web02 ~]#  yum -y install mariadb mariadb-server keepalived

一、更改主服务器的配置文件;

[root@web01 ~]# vim /etc/my.cnf
[mysqld]
server-id=1  #设置服务器id,为1表示主服务器
log-bin=mysql-bin  #启动MySQ二进制日志系统
auto-increment-increment = 2   #字段变化增量值
auto-increment-offset = 1 #初始字段ID为1
slave-skip-errors = all #忽略所有复制产生的错误
binlog-ignore-db=mysql,information_schema #忽略写入binlog日志的库
:wq #保存退出
[root@web01 ~]# systemctl restart mariadb

二、更改从服务器的配置文件

[root@web02 ~]# vim /etc/my.cnf
[mysqld]
server-id=2  #设置服务器id,为2表示从服务器
log-bin=mysql-bin  #启动MySQ二进制日志系统
auto-increment-increment = 2   #字段变化增量值
auto-increment-offset = 1 #初始字段ID为1
slave-skip-errors = all #忽略所有复制产生的错误
binlog-ignore-db=mysql,information_schema #忽略写入binlog日志的库
:wq #保存退出
[root@web02 ~]# systemctl restart mariadb

三、配置主、从服务器两者一样(由于我们数据库中没有数据,需要自己手动新增进行测试)

[root@web01 ~]# mysql -u root -p   #输入密码进入mariadb
MariaDB [(none)]> create database test; #创建数据库
MariaDB [(none)]> use test; #进入数据库内
MariaDB [(none)]>CREATE TABLE mybook (name char(15),price int,pages int);  #创建表单
MariaDB [(none)]>INSERT INTO mybook(name,price,pages) VALUES('linuxprobe', '60', '518'); #向表单中添加数据
MariaDB [(none)]>SELECT * FROM mybook;  #查看表单中的数据
+------------+-------+-------+
| name       | price | pages |
+------------+-------+-------+
| linuxprobe |    60 |   518 |
+------------+-------+-------+
row in set (0.00 sec)

四、配置主服务器(创建同步数据库的账号)

MariaDB [test]> grant replication slave on *.* to 'check'@'%' identified by '123456';  #用户名为check,密码为123456
MariaDB [(none)]> show master status; #记录File和Postion中的数据,等下需要填到从服务器配置里
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000003 |     1097 | test         | mysql            |
+------------------+----------+--------------+------------------+
row in set (0.00 sec)
MariaDB [(none)]> flush  privileges;
MariaDB [(none)]> change master to master_host='192.168.11.84', master_user='check', master_password='123456', master_log_file='mysql-bin.000003', master_log_pos=1097;
MariaDB [(none)]> start slave;

五、配置从服务器

[root@web02 ~]# mysql -u root -p
MariaDB [(none)]> grant replication slave on *.* to 'check'@'%' identified by '123456';  #用户名为check,密码为123456
MariaDB [(none)]> change master to master_host='192.168.11.83',master_user='check',master_password='123456',master_log_file='mysql-bin.000003' ,master_log_pos=1097;  #注意上图中的File和Postion的值要填写正确。
MariaDB [(none)]> slave start;   #开启从服务器同步
MariaDB [(none)]> show slave status\G #查看slave同步信息
Slave_IO_Running: Yes   
Slave_SQL_Running: Yes  
#上面这两行显示都是Yes,表示同步成功

六、测试主、从服务器是否同步;

(1)先向主服务器插入一条数据,看从服务器是否同步。

[root@web01 ~]# mysql -uroot -p
MariaDB [(none)]> use test;
INSERT INTO mybook(name,price,pages) VALUES('linux', '100', '100');
MariaDB [test]> select *from mybook;
+------------+-------+-------+
| name       | price | pages |
+------------+-------+-------+
| linuxprobe |    60 |   518 |
| linux      |   100 |   100 |
+------------+-------+-------+
2 rows in set (0.00 sec)
#此显示是主服务器中的数据表;

(2)进入从服务器看数据库与主服务器一样;

[root@web02 ~]# mysql -uroot -p
MariaDB [test]> select *from mybook;
+------------+-------+-------+
| name       | price | pages |
+------------+-------+-------+
| linuxprobe |    60 |   518 |
| linux      |   100 |   100 |
+------------+-------+-------+
2 rows in set (0.00 sec)
#此显示是从服务器中的数据表;显示与上面的主服务器一致,表示同步成功;

七、配置keepalived实现热备

[root@web01 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id MYSQL_HA  #标识,双主相同
}

vrrp_instance VI_1 {
    state BACKUP    #两台都设置BACKUP
    interface ens33
    virtual_router_id 51  #主备相同
    priority 100    #优先级,backup设置90
    advert_int 1
    nopreempt   #不主动抢占资源,只在master这台优先级高的设置,backup不设置
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.11.90       #VIP地址
    }
}

virtual_server 192.168.11.90 3306 {
    delay_loop 6
    #lb_algo rr   #LVS算法,用不到,我们就关闭了
    #lb_kind NAT  #LVS模式,如果不关闭,备用服务器不能通过VIP连接主MySQL
persistence_timeout 50
    protocol TCP
    real_server 192.168.11.83 3306 {   #检测本地mysql,backup也要写检测本地mysql
        weight 3
        notify_down /etc/keepalived/mysql.sh  #当mysq服down时,执行此脚本,杀死keepalived实现切换
            TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            }
        }
    }
[root@web01 ~]# vim /etc/keepalived/mysql.sh      
#!/bin/bash
killall keepalived
[root@web01 ~]# chmod +x /etc/keepalived/mysql.sh

#从服务器keepalived文件中只修改priority为90、nopreempt不设置、real_server设置本地IP。

#授权两台Mysql服务器允许root远程登录,用于在其他服务器登陆测试!

MariaDB [(none)]> grant all on *.* to'root'@'%' identified by '123456';
MariaDB [(none)]> flush privileges;
[root@web01 ~]# systemctl restart keepalived
[root@web02 ~]# systemctl restart keepalived

 八、测试高可用性

1、通过Mysql客户端通过VIP连接,看是否连接成功。

2、停止web01这台mysql服务,是否能正常切换至web02上面,可通过ip addr命令来查看VIP地址的漂移情况。

3、可通过查看/var/log/messges日志,看出主备切换过程。

4、web01服务器故障恢复后,是否主动抢占资源,成为活动服务器,可通过ip addr 命令看VIP地址是否恢复。

posted @ 2019-12-30 17:46  区域管理员  阅读(320)  评论(0编辑  收藏  举报