MariaDB Galera Cluster 部署 + keepalived實現高可用

                

           

MariaDB Galera Cluster 部署   html

   

MariaDB做爲Mysql的一個分支,在開源項目中已經普遍使用,例如大熱的openstack,因此,爲了保證服務的高可用性,同時提升系統的負載能力,集羣部署是必不可少的。node

MariaDB Galera Cluster 介紹mysql

 

MariaDB集羣是MariaDB同步多主機集羣。它僅支持XtraDB/ InnoDB存儲引擎(雖然有對MyISAM實驗支持 - 看wsrep_replicate_myisam系統變量)。linux

 

主要功能:sql

 

    同步複製數據庫

    真正的multi-master,即全部節點能夠同時讀寫數據庫vim

    自動的節點成員控制,失效節點自動被清除centos

    新節點加入數據自動複製服務器

    真正的並行複製,行級app

    用戶能夠直接鏈接集羣,使用感覺上與MySQL徹底一致

 

優點:

 

    由於是多主,因此不存在Slavelag(延遲)

    不存在丟失事務的狀況

    同時具備讀和寫的擴展能力

    更小的客戶端延遲

    節點間數據是同步的,而Master/Slave模式是異步的,不一樣slave上的binlog多是不一樣的

 

技術:

 

Galera集羣的複製功能基於Galeralibrary實現,爲了讓MySQL與Galera library通信,特別針對MySQL開發了wsrep API。

 

Galera插件保證集羣同步數據,保持數據的一致性,靠的就是可認證的複製,工做原理以下圖: MariaDB Galera Cluster 部署

 

當客戶端發出一個commit的指令,在事務被提交以前,全部對數據庫的更改都會被write-set收集起來,而且將write-set紀錄的內容發送給其餘節點。

 

write-set將在每一個節點進行認證測試,測試結果決定着節點是否應用write-set更改數據。

 

若是認證測試失敗,節點將丟棄write-set;若是認證測試成功,則事務提交。

1. 安裝環境準備

安裝MariaDB集羣至少須要3臺服務器(若是隻有兩臺的話須要特殊配置,請參照 官方文檔 )

 

在這裏,我列出試驗機器的配置:

 

操做系統版本:centos7

 

maria-server1 192.168.1.153

maria-server2 192.168.1.154

 

爲了保證節點間相互通訊,須要禁用防火牆設置(若是須要防火牆,則參照 官方網站 增長防火牆信息設置)

 

在三個節點分別執行命令:

 

systemctl stop firewalld

 

而後將/etc/sysconfig/selinux的selinux設置成disabled,這樣初始化環境就完成了。

2. 安裝 MariaDB Galera Cluster

配置MariaDByum

yum install -y epel-release

wget https://repos.fedorapeople.org/repos/openstack/openstack-kilo/rdo-release-kilo-2.noarch.rpm

rpm -ivh rdo-release-kilo-2.noarch.rpm

yum update -y

yum install -y mariadb mariadb-libs mariadb-devel mariadb-galera-common mariadb-galera-server  galera rsync

systemctl enable  mariadb

systemctl enable  rsyncd

systemctl start mariadb

mysql_secure_installation  --root 容許遠程登錄 密碼:password

[root@maria-server1 mysql]# mysql -u root -ppassword           

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 5

Server version: 5.5.40-MariaDB-wsrep MariaDB Server, wsrep_25.11.r4026

 

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab and others.

 

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

 

MariaDB [(none)]> grant all privileges on *.* to 'root'@'%' identified by 'password' with grant option;

flush privileges;

MariaDB [(none)]> select host, user from mysql.user;                                                  

+---------------+------+

| host          | user |

+---------------+------+

| %             | root |

| 127.0.0.1     | root |

| ::1           | root |

| localhost     |      |

| localhost     | root |

| maria-server1 |      |

| maria-server1 | root |

+---------------+------+

7 rows in set (0.00 sec)

 

3. 配置 MariaDB Galera Cluster

http://blog.sina.com.cn/s/blog_6de3aa8a0102w00d.html

---192.168.1.153

vim /etc/my.cnf.d/galera.cnf +27

bind-address=0.0.0.0  -->  bind-address=192.168.1.153

wsrep_cluster_name="dbcluster"

 

vim /etc/my.cnf.d/server.cnf

[mysqld]

datadir=/var/lib/mysql

query_cache_size=0

binlog_format=ROW

default_storage_engine=innodb

innodb_autoinc_lock_mode=2

wsrep_provider=/usr/lib64/galera/libgalera_smm.so

wsrep_provider_options="gcache.size=1G"

wsrep_cluster_address=gcomm://192.168.1.153,192.168.1.154[i1]                            

wsrep_cluster_name='dbcluster'

wsrep_node_address='192.168.1.153'

wsrep_node_name='maria-server1'

wsrep_sst_method=rsync

 

---192.168.1.154

vim /etc/my.cnf.d/galera.cnf +27

bind-address=0.0.0.0  -->  bind-address=192.168.1.154

wsrep_cluster_name="dbcluster"

 

vim /etc/my.cnf.d/server.cnf

[mysqld]

datadir=/var/lib/mysql

query_cache_size=0

binlog_format=ROW

default_storage_engine=innodb

innodb_autoinc_lock_mode=2

wsrep_provider=/usr/lib64/galera/libgalera_smm.so

wsrep_provider_options="gcache.size=1G"

wsrep_cluster_address=gcomm://192.168.1.153,192.168.1.154

wsrep_cluster_name='dbcluster'

wsrep_node_address='192.168.1.154'

wsrep_node_name='maria-server2'

wsrep_sst_method=rsync

wsrep_cluster_address ==注意必定要保證有節點存在

第一次初始化爲wsrep_cluster_address= gcomm://

4. 查看集羣狀態

MariaDB [(none)]> show status like '%wsrep_%';

+------------------------------+-----------------------------------------+

| Variable_name                | Value                                   |

+------------------------------+-----------------------------------------+

| wsrep_local_state_uuid       | 2489b818-219b-11e6-9021-b61cb5e054fb    |

| wsrep_protocol_version       | 5                                       |

| wsrep_last_committed         | 18                                      |

| wsrep_replicated             | 0                                       |

| wsrep_replicated_bytes       | 0                                       |

| wsrep_repl_keys              | 0                                       |

| wsrep_repl_keys_bytes        | 0                                       |

| wsrep_repl_data_bytes        | 0                                       |

| wsrep_repl_other_bytes       | 0                                       |

| wsrep_received               | 3                                       |

| wsrep_received_bytes         | 238                                     |

| wsrep_local_commits          | 0                                       |

| wsrep_local_cert_failures    | 0                                       |

| wsrep_local_replays          | 0                                       |

| wsrep_local_send_queue       | 0                                       |

| wsrep_local_send_queue_avg   | 0.000000                                |

| wsrep_local_recv_queue       | 0                                       |

| wsrep_local_recv_queue_avg   | 0.000000                                |

| wsrep_local_cached_downto    | 18446744073709551615                    |

| wsrep_flow_control_paused_ns | 0                                       |

| wsrep_flow_control_paused    | 0.000000                                |

| wsrep_flow_control_sent      | 0                                       |

| wsrep_flow_control_recv      | 0                                       |

| wsrep_cert_deps_distance     | 0.000000                                |

| wsrep_apply_oooe             | 0.000000                                |

| wsrep_apply_oool             | 0.000000                                |

| wsrep_apply_window           | 1.000000                                |

| wsrep_commit_oooe            | 0.000000                                |

| wsrep_commit_oool            | 0.000000                                |

| wsrep_commit_window          | 1.000000                                |

| wsrep_local_state            | 4                                       |

| wsrep_local_state_comment    | Synced                                  |

| wsrep_cert_index_size        | 0                                       |

| wsrep_causal_reads           | 0                                       |

| wsrep_cert_interval          | 0.000000                                |

| wsrep_incoming_addresses     | 192.168.1.154:3306,192.168.1.153:3306 |

| wsrep_cluster_conf_id        | 24                                      |

| wsrep_cluster_size           | 2                                       |

| wsrep_cluster_state_uuid     | 2489b818-219b-11e6-9021-b61cb5e054fb    |

| wsrep_cluster_status         | Primary                                 |

| wsrep_connected              | ON                                      |

| wsrep_local_bf_aborts        | 0                                       |

| wsrep_local_index            | 0                                       |

| wsrep_provider_name          | Galera                                  |

| wsrep_provider_vendor        | Codership Oy <info@codership.com>       |

| wsrep_provider_version       | 3.5(rXXXX)                              |

| wsrep_ready                  | ON                                      |

| wsrep_thread_count           | 2                                       |

+------------------------------+-----------------------------------------+

48 rows in set (0.00 sec)

咱們能夠關注幾個關鍵的參數:

 

wsrep_connected = on 連接已開啓

 

wsrep_local_index = 1 在集羣中的索引值

 

wsrep_cluster_size =3 集羣中節點的數量

 

wsrep_incoming_addresses =192.168.1.154:3306,192.168.1.153:3306集羣中節點的訪問地址

5. 驗證數據同步

maria-server1

MariaDB [(none)]> create database galera_test7;

MariaDB [(none)]> create database galera_test8;

MariaDB [(none)]> show databases;             

+--------------------+

| Database           |

+--------------------+

| information_schema |

| galera_test7       |

| galera_test8       |

| mysql              |

| performance_schema |

| test               |

+--------------------+

6 rows in set (0.00 sec)

 

maria-server2上查看

MariaDB [(none)]> show databases;            

+--------------------+

| Database           |

+--------------------+

| information_schema |

| galera_test7       |

| galera_test8       |

| mysql              |

| performance_schema |

| test               |

+--------------------+

6 rows in set (0.00 sec)

 

6. keepalived實現高可用

安裝keepalived  --2個節點

yum -y install keepalived

---192.168.1.153

[root@maria-server1 keepalived]# vim /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

 

global_defs {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_script check_haproxy {

    script "sh /etc/keepalived/check_mysql.sh"

    interval 2

    weight -4

}

 

vrrp_instance VI_1 {

    state MASTER

    interface eth0

    virtual_router_id 67[i2] 

    priority 100[i3] 

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.157

    }

    track_script {

       check_haproxy

     }

}

vim /etc/keepalived/check_mysql.sh

#===================start==========================

pro=`ps -ef | grep mariadb  | grep -v grep | wc -l`

if [ $pro -eq 0  ];then

   echo `date` "Keepalived -mariadb  is not alived" >> /var/log/messages

   systemctl stop keepalived

fi

#===================end==========================

 

---192.168.1.154

[root@maria-server2 mysql]# vim /etc/keepalived/keepalived.conf

 

! Configuration File for keepalived

 

global_defs {

   notification_email {

     acassen@firewall.loc

     failover@firewall.loc

     sysadmin@firewall.loc

   }

   notification_email_from Alexandre.Cassen@firewall.loc

   smtp_server 127.0.0.1

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

 

vrrp_script check_haproxy {

    script "sh /etc/keepalived/check_mysql.sh"

    interval 2

    weight -4

}

 

vrrp_instance VI_1 {

    state BACKUP

    interface eth0

    virtual_router_id 67

    priority 80

    advert_int 1

    authentication {

        auth_type PASS

        auth_pass 1111

    }

    virtual_ipaddress {

        192.168.1.157

    }

    track_script {

       check_haproxy

     }

}

 

 

7. 問題彙總

配置完虛擬ip後出現

[root@kvmserver2 ~]# mysql -u root -ppassword -h 192.168.1.157

ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.1.157' (111)

vim /etc/my.cnf.d/galera.cnf

bind-address = 註釋掉便可

 

當2個mariadb節點同時關掉啓動是報錯以下:

[ERROR] WSREP: gcs/src/gcs.c:gcs_open():1291: Failed to open channel 'dbcluster' at 'gcomm://192.168.1.153, 192.168.1.154': -110 (Connection timed out)

 

從新初始化gcomm

vim /etc/my.cnf.d/server.cnf

wsrep_cluster_address=gcomm://

--153

systemctl start mariadb

--154

systemctl start mariadb

--153

vim /etc/my.cnf.d/server.cnf

wsrep_cluster_address=gcomm://192.168.1.153,192.168.1.154  

systemctl restart mariadb

 


 [i1]雙主狀態,若是單個ip就是主從狀態

 [i2]確保內網中router_id沒有出現67

 [i3]優先級高

相關文章
相關標籤/搜索