Linux MySQL+Keepalived主主热备

上一篇Linux Keepalived双机热备实现了Linux Keepalived双机热备利用Nginx进行节点切换的验证。本文实现MySQL数据库的主主同步。MySQL主主同步是独立的,与Keepalived无关,因此重点说下MySQL的配置,Keepalived节点切换与之前实现是一样的。


环境

Master1:172.18.30.17 安装mysql和keepalived
Master2: 172.18.30.98 安装mysql和keepalived
VIP:172.18.30.188


一、Mysql主主同步环境部署
1、master1配置

打开/etc/my.cnf

vi /etc/my.cnf

在my.cnf文件的[mysqld]配置区域添加下面内容:

server-id = 1         
log-bin = mysql-bin     
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2     
auto-increment-offset = 1    
slave-skip-errors = all

重启数据库

/usr/bin/mysql restart
或
service mariadb restart

登陆数据库

mysql -u root

数据同步授权

grant replication slave,replication client on *.* to mysql@'172.18.30.%' identified by "password";

刷新

flush privileges;

将库锁住,仅允许读,以保证数据一致性,待主主同步环境部署后再解锁(同步前记得先解锁)

flush tables with read lock;

查看log bin日志和pos值位置

show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000001 |     502 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.03 sec)
2、master2配置

打开/etc/my.cnf

vi /etc/my.cnf

在my.cnf文件的[mysqld]配置区域添加下面内容:

server-id = 2        
log-bin = mysql-bin    
sync_binlog = 1
binlog_checksum = none
binlog_format = mixed
auto-increment-increment = 2     
auto-increment-offset = 2    
slave-skip-errors = all

重启数据库

/usr/bin/mysql restart
或
service mariadb restart

数据同步授权

grant replication slave,replication client on *.* to mysql@'172.18.30.%' identified by "password";

刷新

flush privileges;

将库锁住,仅允许读

flush tables with read lock;

查看log bin日志和pos值位置

show master status;
+------------------+----------+--------------+------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+------------------+----------+--------------+------------------+
| mysql-bin.000002 |     504 |              |                  |
+------------------+----------+--------------+------------------+
1 row in set (0.03 sec)
3、master1同步配置

先解锁,将对方数据同步到自己的数据库中

unlock tables;
slave stop;
change  master to master_host='192.18.30.98',master_user='mysql',master_password='password',master_log_file='mysql-bin.000001',master_log_pos=502;
start slave;

查看同步状态,如下出现两个“Yes”,表明同步成功

show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.18.30.98
                  Master_User: mysql
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000001
          Read_Master_Log_Pos: 504
               Relay_Log_File: mysql-relay-bin.000018
                Relay_Log_Pos: 279
        Relay_Master_Log_File: mysql-bin.000001
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
        .........................
        Seconds_Behind_Master: 0
        .........................

这样,master1就和master2实现了主从同步,即master1同步master2的数据。

4、master2同步配置

解锁

unlock tables;
slave stop;
change  master to master_host='172.18.30.17',master_user='mysql',master_password='password',master_log_file='mysql-bin.000001',master_log_pos=502;
start slave;

查看同步状态,如下出现两个“Yes”,表明同步成功

show slave status \G;
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 172.18.30.17
                  Master_User: mysql
                  Master_Port: 3306
                Connect_Retry: 60
              Master_Log_File: mysql-bin.000002
          Read_Master_Log_Pos: 502
               Relay_Log_File: mysql-relay-bin.000018
                Relay_Log_Pos: 279
        Relay_Master_Log_File: mysql-bin.000002
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
        .........................
        Seconds_Behind_Master: 0
        .........................

这样,master2就和master1实现了主从同步,即master2也同步master1的数据。

以上表明双方已经实现了mysql主主同步。

5、验证

打开数据库,修改master1的数据,查看master2会不会变,或反之。


二、配置Keepalived
1、安装keepalived并将其配置成系统服务

参考上一篇Linux Keepalived双机热备

2、编写切换脚本

切换脚本与上一篇的nginx_check.sh类似

vim /opt/chk_mysql.sh
#!/bin/bash
counter=$(netstat -na|grep "LISTEN"|grep "3306"|wc -l)
if [ "${counter}" -eq 0 ]; then
    service keepalived stop
fi
chmod 755 /opt/chk_mysql.sh
service keepalived start
3、验证

通过虚拟IP(VIP)访问数据库,然后关掉一台数据库的服务,查看数据库是否仍然正常。

4、抢占

nopreempt这个参数只能用于state为backup的情况,所以在配置的时候要把master和backup的state都设置成backup,这样才会实现keepalived的非抢占模式。


你可能感兴趣的:(Linux MySQL+Keepalived主主热备)