Keepalived+MySQL雙主高可用配置

1.安裝環境:

服務器 IP OS MySQL同步用戶名/密碼 VIP
master1 192.168.0.110 CentOS 7.5 sync/syncpwd 192.168.0.250
master2 192.168.0.111 CentOS 7.5 sync/syncpwd 192.168.0.250

MySQL:5.7mysql

Keepalived:2.0.10,VIP:192.168.0.250sql

MySQL安裝參考CentOS 7.5安裝MySQL 5.7教程 免安裝版shell

Keepalived安裝參考CentOS 7.5安裝Keepalived教程數據庫

2.MySQL雙主配置

服務器中已經成功安裝MySQL服務器vim

master1修改my.cnf,新增以下配置:bash

>vim /etc/my.cnf
[msqyld]
server-id=110
log-bin=mysql-bin
sync-binlog=1
binlog-checksum=none
binlog-format=mixed
auto-increment-increment=2
auto-increment-offset=1
log-slave-updates
slave-skip-errors=all

master2修改my.cnf,新增以下配置:服務器

>vim /etc/my.cnf
[msqyld]
server-id=111
log-bin=mysql-bin
sync-binlog=1
binlog-checksum=none
binlog-format=mixed
auto-increment-increment=2
auto-increment-offset=2
log-slave-updates
slave-skip-errors=all

master1和master2重啓MySQL服務:ide

>service mysqld restart

master1和master2新增同步帳戶,見MySQL安裝參考oop

在master1中爲mysql從庫帳戶受權:測試

>grant replication slave on *.* to 'sync'@'%' identified by 'syncpwd';
>flush privileges;
>show master status; #當前主庫狀態,即master1
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |      599 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

在master2中爲mysql從庫帳戶受權:

>grant replication slave on *.* to 'sync'@'%' identified by 'syncpwd';
>flush privileges;
>show master status; #當前主庫狀態,即master2
show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000002 |      468 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

在maste1中指定master2爲主庫:

>stop slave;
>change master to master_host='192.168.0.111',master_user='sync',master_password='syncpwd',master_log_file='mysql-bin.000002',master_log_pos=468;
>flush privileges;

在maste2中指定master1爲主庫:

>stop slave;
>change master to master_host='192.168.0.110',master_user='sync',master_password='syncpwd',master_log_file='mysql-bin.000004',master_log_pos=599;
>flush privileges;

MySQL雙主配置完成,驗證配置成功:

>show slave status\G #master1中顯示的信息
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.111
                  Master_User: sync
                  Master_Port: 3306
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes
>show slave status\G #master2中顯示的信息
*************************** 1. row ***************************
               Slave_IO_State: Waiting for master to send event
                  Master_Host: 192.168.0.110
                  Master_User: sync
                  Master_Port: 3306
             Slave_IO_Running: Yes
            Slave_SQL_Running: Yes

在任一服務器中建立數據庫表或寫入數據均可以在另外一服務器中看到

3.Keepalived高可用配置

master1和master2中已經正確安裝Keepalived,並指定VIP:192.168.0.250,且keepalived可正常漂移

master1修改keepalived.conf,新增配置:

>vim /etc/keepalived/keepalived.conf
virtual_server 192.168.0.250 3306 {
  delay_loop 3
  lb_algo wrr
  lb_kind DR
  persistence_timeout 60
  protocol TCP
  real_server 192.168.0.110 3306 {
    weight 3
    notify_down /usr/local/mysql/chk_mysql.sh
    TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3
      delay_before_retry 3
      connect_port 3306
    }
  }
}

chk_mysql.sh內容

>vim /usr/local/mysql/chk_mysql.sh
#!/bin/bash
pkill keepalived
sleep 30
/etc/init.d/keeplived start

master2修改keepalived.conf,新增配置:

>vim /etc/keepalived/keepalived.conf
virtual_server 192.168.0.250 3306 {
  delay_loop 3
  lb_algo wrr
  lb_kind DR
  persistence_timeout 60
  protocol TCP
  real_server 192.168.0.111 3306 {
    weight 3
    notify_down /usr/local/mysql/chk_mysql.sh
    TCP_CHECK {
      connect_timeout 10
      nb_get_retry 3
      delay_before_retry 3
      connect_port 3306
    }
  }
}

chk_mysql.sh內容

>vim /usr/local/mysql/chk_mysql.sh
#!/bin/bash
pkill keepalived
sleep 30
/etc/init.d/keeplived start

4.測試

交叉驗證,重啓MySQL服務器後須要手動啓動Keepalived

相關文章
相關標籤/搜索