[高可用]Ubuntu搭建高可用的MariaDB Galera資料庫叢集
基於OpenStack的搭建過程中,所有的元件都是儲存在MySQL資料庫中,既然要考慮到整個OpenStack的高可用性,我們需要對每一個元件、節點進行考慮,今天就先對MySQL資料庫的叢集進行實驗。
搭建MySQL的叢集,參考了比較常用的MariaDB Galera叢集方案,由於Oracle公司收購了MySQL,很多開源愛好者擔心Oracle公司隨時有閉源的可能,所以MariaDB是MySQL開發的一個分支,在使用過程中可以把它當成MySQL,任何命令都沒有任何變化,只是名稱有所改變。
對於OpenStack來說,在Icehouse版本還沿用了MySQL資料庫,但是在Juno版本,已經沿用了MariaDB,可見OpenStack也隨時考慮自己的完全開源的問題。
關於MariaDB Galera叢集的特點:
- 同步複製
- 多個主伺服器的拓撲結構
- 可以在任意節點上進行讀寫
- 自動控制成員
- 自動刪除故障節點
- 自動加入節點
- 真正給予行級別的併發複製
- 排程客戶連線
也具有下列的相關優勢
- 沒有Slave延遲
- 不會丟失資料
- 讀取和寫入的可擴充套件性
- 客戶端延遲較小
一般情況下,大家看到的經典的MariaDB Galera叢集架構圖都是至少包含三個節點
為什麼至少要三個節點呢?因為如果只有兩個節點,一旦出現數據不統一,會發生腦裂現象,也就是系統並不知道到底以哪一個節點的結果為正確的結果,引入第三個節點,提供一個仲裁功能,確保了資料的準確性和統一性參考性。
我的實驗測試環境也為三個節點,作業系統為Ubuntu 14.04
1、python1:192.168.3.108
2、Python2:192.168.3.107
3、Python3:192.168.3.133
需要注意的是:
1、伺服器最好有交換分割槽,特別是在資料庫狀態轉移期間,是需要快取的
2、伺服器設定的日誌型別必須是檔案型別,不能是CSV或者其他,而且Query cache必須禁用。
執行過程
1、安裝Maria DB Galera軟體包(分別在三個節點執行)
sudo apt-get install software-properties-common
然後新增MariaDB的倉庫映象,本例使用的清華的映象,具體配置如下
[email protected]:~$ sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
Executing: gpg --ignore-time-conflict --no-options --no-default-keyring --homedir /tmp/tmp.jqXpEbuAw5 --no-auto-check-trustdb --trust-model always --keyring /etc/apt/trusted.gpg --primary-keyring /etc/apt/trusted.gpg --recv-keys --keyserver keyserver.ubuntu.com 0xcbcb082a1bb943db
gpg: requesting key 1BB943DB from hkp server keyserver.ubuntu.com
gpg: key 1BB943DB: public key "MariaDB Package Signing Key <[email protected]>" imported
gpg: Total number processed: 1
gpg: imported: 1
新增配置源到/etc/apt/source.list檔案中
sudo add-apt-repository 'deb http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.0/ubuntu trusty main'
檢視一下新增後的源列表資訊
[email protected]:~$ sudo cat /etc/apt/sources.list
deb http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-security main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-updates main restricted universe multiverse
deb-src http://mirrors.163.com/ubuntu/ trusty-proposed main restricted universe multiverse
deb http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.0/ubuntu trusty main
# deb-src http://mirrors.tuna.tsinghua.edu.cn/mariadb/repo/10.0/ubuntu trusty main
deb-src http://mirrors.163.com/ubuntu/ trusty-backports main restricted universe multiverse
接下來,更新一下源列表資訊
sudo apt-get update
安裝MariaDB資料庫和rsync軟體
sudo apt-get install rsync mariadb-galera-server
注意:mariadb應該安裝整合galera的版本,rsync是用來同步資料庫所使用的工具。
2、設定Galera(三個節點都需要操作)
建立檔案sudo vi /etc/mysql/conf.d/galera.cnf,該檔案需要自己建立,原來不存在,然後輸入以下內容
[mysqld]
binlog_format=ROW
default-storage-engine=innodb
innodb_autoinc_lock_mode=2
query_cache_size=0
query_cache_type=0
bind-address = 0.0.0.0
wsrep_provider=/usr/lib/galera/libgalera_smm.so
wsrep_cluster_name="mytest_wsrep_cluster"
wsrep_cluster_address="gcomm://192.168.3.107,192.168.3.108,192.168.3.133"
wsrep_sst_method=rsync
注意:需要更新自己的MySQL叢集節點IP地址和叢集名稱
啟動叢集
先將所有節點的叢集mySQL服務停掉
sudo service mysql stop
1、啟動第一個節點,第一個節點啟動服務,必須新增-wsrep-new-cluster選項,以建立叢集
sm@python1:~$ sudo service mysql stop
* Stopping MariaDB database server mysqld [ OK ]
sm@python1:~$ sudo service mysql start --wsrep-new-cluster
* Starting MariaDB database server mysqld [ OK ]
* Checking for corrupt, not cleanly closed and upgrade needing tables.
2、驗證叢集
[email protected]:~$ mysql -uroot -proot -e 'select variable_value from information_schema.global_status where variable_name="wsrep_cluster_size"'
+----------------+
| variable_value |
+----------------+
| 1 |
+----------------+
陸續啟動其他節點叢集,再次驗證
[email protected]:~$ mysql -uroot -proot -e 'select variable_value from information_schema.global_status where variable_name="wsrep_cluster_size"'
+----------------+
| variable_value |
+----------------+
| 2 |
+----------------+
[email protected]:~$ mysql -uroot -proot -e 'select variable_value from information_schema.global_status where variable_name="wsrep_cluster_size"'
+----------------+
| variable_value |
+----------------+
| 3 |
+----------------+
寫入測試
我們在節點1建立一個數據庫
[email protected]:~$ mysql -uroot -proot -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
[email protected]:~$
[email protected]:~$ mysqladmin -uroot -proot create mytestdb
[email protected]:~$ mysql -uroot -proot -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
然後在節點2檢視該資料庫是否已經同步到節點2
[email protected]:~$ mysql -uroot -proot -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytestdb |
| performance_schema |
+--------------------+
然後在節點3刪除這個資料庫
[email protected]:~$ mysql -uroot -proot -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytestdb |
| performance_schema |
+--------------------+
[email protected]:~$ mysqladmin -uroot -proot drop mytestdb
Dropping the database is potentially a very bad thing to do.
Any data stored in the database will be destroyed.
Do you really want to drop the 'mytestdb' database [y/N] y
Database "mytestdb" dropped
然後在節點2檢視這個資料庫
[email protected]:~$ mysql -uroot -proot -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
+--------------------+
故障模擬
停止節點1的服務
sm@python1:~$ sudo service mysql stop
* Stopping MariaDB database server mysqld [ OK ]
在節點2檢視叢集節點數量
[email protected]:~$ mysql -uroot -proot -e 'select variable_value from information_schema.global_status where variable_name="wsrep_cluster_size"'
+----------------+
| variable_value |
+----------------+
| 2 |
+----------------+
節點3建立資料庫
sm@python3:~$ mysqladmin -uroot -proot create mytestdb2
啟動節點1服務,檢視新建立的資料庫是否同步過來
[email protected]:~$ sudo service mysql start
* Starting MariaDB database server mysqld [ OK ]
* Checking for corrupt, not cleanly closed and upgrade needing tables.
[email protected]:~$ mysql -uroot -proot -e 'select variable_value from information_schema.global_status where variable_name="wsrep_cluster_size"'
+----------------+
| variable_value |
+----------------+
| 3 |
+----------------+
[email protected]:~$ mysql -uroot -proot -e 'show databases'
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| mytestdb2 |
| performance_schema |
+--------------------+