1. 程式人生 > >Keepalived+MySQL雙主高可用配置

Keepalived+MySQL雙主高可用配置

1.安裝環境:

伺服器IPOSMySQL同步使用者名稱/密碼VIP
master1192.168.0.110CentOS 7.5sync/syncpwd192.168.0.250
master2192.168.0.111CentOS 7.5sync/syncpwd192.168.0.250

MySQL:5.7

Keepalived:2.0.10,VIP:192.168.0.250

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

Keepalived安裝參考CentOS 7.5安裝Keepalived教程

2.MySQL雙主配置

伺服器中已經成功安裝MySQL伺服器

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

>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服務:

>service mysqld restart

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

在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伺服器後需要