1. 程式人生 > >MySQL keepalived 雙主.md

MySQL keepalived 雙主.md

oba time 詳細介紹 bre tcp sync state head mar

MySQL keepalived 雙主搭建

環境說明

系統 IP 主機名 mysql keepalived VIP
CentOS 6.8 192.168.197.61 C6-node1 5.6.36 1.2.13 192.168.197.88
CentOS 6.8 192.168.197.62 C6-node2 5.6.36 1.2.13 192.168.197.88

安裝MySQL雙主

C6-node1

MySQL的安裝及初始化這裏不做詳細介紹,下面貼出node1的my.cnf的配置文件:

[mysqld]
datadir=/data/mysql
port=3306
socket=/tmp/mysql.sock
pid-file=/data/mysql/mysqld.pid
log_error=error.log
user=mysql
skip-name-resolve
log-bin=mysql-bin
log-bin-index=mysql-bin.index
server-id=61
character_set_server=utf8
log-slave-updates=1
relay-log=mysql-relay-bin
relay-log-index=mysql-relay-bin.index
binlog_format=mixed
auto-increment-increment=2
auto-increment-offset=1
gtid_mode=on
enforce_gtid_consistency=1
skip_slave_start=1

[mysql]
prompt=(\\[email protected]\\h) [\\d]>\\_

[client]
user=root
password=

創建復制帳號:

(rootc6-node1) [(none)]> GRANT REPLICATION SLAVE ON *.* TO ‘mysync‘@‘192.168.197.62‘ IDENTIFIED BY ‘redhat‘;
Query OK, 0 rows affected (0.05 sec)

C6-node2

[mysqld]
datadir=/data/mysql
port=3306
socket=/tmp/mysql.sock
pid-file=/data/mysql/mysqld.pid
log_error=error.log
user=mysql
skip-name-resolve
log-bin=mysql-bin
log-bin-index=mysql-bin.index
server-id=62
character_set_server=utf8
log-slave-updates=1
relay-log=mysql-relay-bin
relay-log-index=mysql-relay-bin.index
binlog_format=mixed
auto-increment-increment=2
auto-increment-offset=2
gtid_mode=on
enforce_gtid_consistency=1
skip_slave_start=1

[mysql]
prompt=(\[email protected]\h) [\d]>\_

[client]
user=root
password=

創建復制帳號:

([email protected]) [(none)]> GRANT REPLICATION SLAVE ON *.* TO ‘mysync‘@‘192.168.197.61‘ IDENTIFIED BY ‘redhat‘;
Query OK, 0 rows affected (0.00 sec)

雙主配置

註意:由於這裏配置的是雙主模型所以兩個數據庫的數據一定要保持一致!如果要是不一致請以一個最新說句為主,註意連接的先後順序!
node1配置:

([email protected]) [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.197.62‘
,MASTER_USER=‘mysync‘,MASTER_PASSWORD=‘redhat‘,MASTER_PORT=3306,MASTER_AUTO_POSITION=1;
([email protected]) [(none)]> START SLAVE; Query OK, 0 rows affected (0.01 sec)

node2配置:

([email protected]) [(none)]> CHANGE MASTER TO MASTER_HOST=‘192.168.197.61‘,MASTER_USER=‘mysync‘,MASTER_PASSWORD=‘redhat‘,MASTER_PORT=3306,MASTER_AUTO_POSITION=1;
Query OK, 0 rows affected, 2 warnings (0.03 sec)

([email protected]) [(none)]> START SLAVE;
Query OK, 0 rows affected (0.03 sec)

keepalived 配置

node1安裝配置

安裝keepalived這裏是用的是yum install -y keepalived進行安裝。keepalived的配置文件如下:

global_defs {
   router_id KEEPALIVED_MYSQL
}

vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 61
    priority 140
    nopreempt
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.197.88
    }
}
virtual_server 192.168.197.61 3306 {  
     delay_loop 2    
     protocol TCP  
     real_server 192.168.197.61 3306 {  
     notify_down "/etc/keepalived/check_mysql.sh"  
     TCP_CHECK {  
     connect_timeout 10   
     nb_get_retry 3     
     delay_before_retry 3 
     connect_port 3306  
     }  
} 
}
  • virtual_router_id 取值在0-255之間,用來區分多個instance的VRRP組播。註意: 同一網段中virtual_router_id的值不能重復,否則會出錯。
  • priority 用來選舉master的,要成為master,那麽這個選項的值最好高於其他機器50個點,該項取值範圍是1-255(在此範圍之外會被識別成默認值100)。
  • nopreempt:不主動搶占資源,只在master這臺優先級高的設置,backup不設置。
  • advert_int:發VRRP包的時間間隔,即多久進行一次master選舉(可以認為是健康查檢時間間隔)。
  • delay_loop:延遲輪詢時間(單位秒)。
  • connect_timeout,nb_get_retry,delay_before_retry分別表示超時時長、重試次數,下次重試的時間延遲。

check_mysql 檢測腳本如下:

#!/bin/bash
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_HOST=localhost
MYSQL_USER=root
CHECK_COUNT=5
 
counter=1
while true
do
        mysql -h $MYSQL_HOST -u $MYSQL_USER -e "show status;"  >/dev/null 2>&1
        i=$?
        ps aux | grep mysqld | grep -v grep > /dev/null 2>&1
        j=$?
        if [ $i -eq 0 ] && [ $j -eq 0 ]
        then
                exit 0
        else
                if [ $i -eq 1 ] && [ $j -eq 0 ]
                then
                        exit 0
                else
                        if [ $counter -gt $CHECK_COUNT ]
                        then
                                break
                        fi
                let counter++
                continue
                fi
        fi
        done
/etc/init.d/keepalived stop
exit 1

說明:
1.check_mysql.sh 腳本的需要賦予執行權限。
2.check_mysql腳本沒有使用密碼登錄獲取mysql的狀態,這裏建議創建一個普通用戶來檢測mysql 的狀態。
3.我這邊沒有使用keepalived中輪詢算法的配置,在網上文章說明需要安裝ipvsadm。

node2配置

global_defs {
   router_id KEEPALIVED_MYSQL
}
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 61
    priority 130
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.197.88
    }
}

virtual_server 192.168.197.62 3306 {  
     delay_loop 2    
     protocol TCP  
     real_server 192.168.197.62 3306 {  
     notify_down "/etc/keepalived/check_mysql.sh"  
     TCP_CHECK {  
     connect_timeout 10   
     nb_get_retry 3     
     delay_before_retry 3 
     connect_port 3306  
     }  
} 
}

check_mysql.sh腳本和node1的相同。

啟動並測試

1.啟動時先保證MySQL雙主模式配置是好的,即Slave_IO_Running: YesSlave_SQL_Running: Yes,這兩個要是Yes狀態。
2.keepalived啟動時先啟動priority 低的節點,然後在啟動priority高的節點,避免出現搶占VIP。
3.註意,我在mysql雙主模式中設置了skip_slave_start,即啟動mysql不會自動執行start slave;而是需要手動執行!

測試

# hostname -I
192.168.197.61 192.168.197.88
# ip addr show  eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:9c:7a:30 brd ff:ff:ff:ff:ff:ff
    inet 192.168.197.61/24 brd 192.168.197.255 scope global eth0
    inet 192.168.197.88/32 scope global eth0
    inet6 fe80::20c:29ff:fe9c:7a30/64 scope link 
       valid_lft forever preferred_lft forever
# mysql -u root -h 192.168.197.88 -p
Enter password:

([email protected]192.168.197.88) [(none)]> show variables like ‘server_id‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 61    |
+---------------+-------+
1 row in set (0.00 sec)

([email protected]192.168.197.88) [(none)]> select * from test.DOTA;
+----+--------------+------------+
| Id | Name         | Short_name |
+----+--------------+------------+
|  2 | 敵法師       | DF         |
|  4 | 隱形刺客     | SA         |
|  5 | 沙王         | SK         |
|  7 | 骷髏王       | SNK        |
|  8 | 混沌騎士     | CK         |
|  9 | 冰魂         | AA         |
| 10 | 斧王         | FV         |
| 11 | 司夜刺客     | NA         |
| 12 | 全能騎士     | OK         |
+----+--------------+------------+
9 rows in set (0.00 sec)

停止node1上的mysql並測試:

# hostname -I
192.168.197.62 192.168.197.88
# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:41:57:51 brd ff:ff:ff:ff:ff:ff
    inet 192.168.197.62/24 brd 192.168.197.255 scope global eth0
    inet 192.168.197.88/32 scope global eth0
    inet6 fe80::20c:29ff:fe41:5751/64 scope link 
       valid_lft forever preferred_lft forever
# mysql -uroot -h192.168.197.88 -p
Enter password: 
([email protected]192.168.197.88) [(none)]> show variables like ‘server_id‘;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| server_id     | 62    |
+---------------+-------+
1 row in set (0.00 sec)

([email protected]192.168.197.88) [(none)]> select * from test.DOTA;
+----+--------------+------------+
| Id | Name         | Short_name |
+----+--------------+------------+
|  2 | 敵法師       | DF         |
|  4 | 隱形刺客     | SA         |
|  5 | 沙王         | SK         |
|  7 | 骷髏王       | SNK        |
|  8 | 混沌騎士     | CK         |
|  9 | 冰魂         | AA         |
| 10 | 斧王         | FV         |
| 11 | 司夜刺客     | NA         |
| 12 | 全能騎士     | OK         |
+----+--------------+------------+
9 rows in set (0.00 sec)

說明:
1.以上配置的不足之處是node1在重新啟動mysql和 keepalived時會搶占VIP,這個原因個人認為是我的檢測腳本把keepalived 給關閉了而在重新啟動時由於priority的關系還是會搶占。
2.網上參考的是主上配置nopreempt參數,但是測試的效果還是不行。如有懂得的還請指教。

MySQL keepalived 雙主.md