MariaDB Galera Cluster 部署(如何快速部署MariaDB叢集),galeramariadb

來源:互聯網
上載者:User

MariaDB Galera Cluster 部署(如何快速部署MariaDB叢集),galeramariadb

MariaDB作為Mysql的一個分支,在開源項目中已經廣泛使用,例如大熱的openstack,所以,為了保證服務的高可用性,同時提高系統的負載能力,叢集部署是必不可少的。

MariaDB Galera Cluster 介紹

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

主要功能:

  • 同步複製
  • 真正的multi-master,即所有節點可以同時讀寫資料庫
  • 自動的節點成員控制,失效節點自動被清除
  • 新節點加入資料自動複製
  • 真正的並行複製,行級
  • 使用者可以直接連接叢集,使用感受上與MySQL完全一致

優勢:

  • 因為是多主,所以不存在Slavelag(延遲)
  • 不存在丟失事務的情況
  • 同時具有讀和寫的擴充能力
  • 更小的用戶端延遲
  • 節點間資料是同步的,而Master/Slave模式是非同步,不同slave上的binlog可能是不同的

技術:

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

Galera外掛程式保證叢集同步資料,保持資料的一致性,靠的就是可認證的複製,工作原理如:

當用戶端發出一個commit的指令,在事務被提交之前,所有對資料庫的更改都會被write-set收集起來,並且將 write-set 紀錄的內容發送給其他節點。

write-set 將在每個節點進行認證測試,測試結果決定著節點是否應用write-set更改資料。

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

1.安裝環境準備

安裝MariaDB叢集至少需要3台伺服器(如果只有兩台的話需要特殊配置,請參照官方文檔)

在這裡,我列出實驗機器的配置:

作業系統版本:centos7

node4:10.128.20.16 node5:10.128.20.17 node6:10.128.20.18

以第一行為例,node4為 hostname ,10.128.20.16為 ip ,在三台機器修改 /etc/hosts檔案,我的檔案如下:

10.128.20.16 node410.128.20.17 node510.128.20.18 node6

為了保證節點間相互連信,需要禁用防火牆設定(如果需要防火牆,則參照官方網站增加防火牆資訊設定)

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

systemctl stop firewalld

然後將/etc/sysconfig/selinuxselinux 設定成 disabled ,這樣初始化環境就完成了。

2.安裝 MariaDB Galera Cluster
[root@node4 ~]# yum install -y mariadb mariadb-galera-server mariadb-galera-common galera rsync
[root@node5 ~]# yum install -y mariadb mariadb-galera-server mariadb-galera-common galera rsync
[root@node6 ~]# yum install -y mariadb mariadb-galera-server mariadb-galera-common galera rsync
3.配置 MariaDB Galera Cluster

初始化資料庫服務,只在一個節點進行

[root@node4 mariadb]# systemctl start mariadb[root@node4 mariadb]# mysql_secure_installationNOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!In order to log into MariaDB to secure it, we'll need the currentpassword for the root user.  If you've just installed MariaDB, andyou haven't set the root password yet, the password will be blank,so you should just press enter here.Enter current password for root (enter for none):OK, successfully used password, moving on...Setting the root password ensures that nobody can log into the MariaDBroot user without the proper authorisation.Set root password? [Y/n]New password:Re-enter new password:Password updated successfully!Reloading privilege tables.. ... Success!By default, a MariaDB installation has an anonymous user, allowing anyoneto log into MariaDB without having to have a user account created forthem.  This is intended only for testing, and to make the installationgo a bit smoother.  You should remove them before moving into aproduction environment.Remove anonymous users? [Y/n] n ... skipping.Normally, root should only be allowed to connect from 'localhost'.  Thisensures that someone cannot guess at the root password from the network.Disallow root login remotely? [Y/n] y ... Success!By default, MariaDB comes with a database named 'test' that anyone canaccess.  This is also intended only for testing, and should be removedbefore moving into a production environment.Remove test database and access to it? [Y/n] n ... skipping.Reloading the privilege tables will ensure that all changes made so farwill take effect immediately.Reload privilege tables now? [Y/n] y ... Success!Cleaning up...All done!  If you've completed all of the above steps, your MariaDBinstallation should now be secure.Thanks for using MariaDB!

關閉資料庫,修改 /etc/my.cnf.d/galera.cnf

[root@node4 mariadb]# systemctl stop mariadb[root@node4 ~]# vim /etc/my.cnf.d/galera.cnf

修改以下內容:

[mysqld]......wsrep_provider = /usr/lib64/galera/libgalera_smm.sowsrep_cluster_address = "gcomm://node4,node5,node6"wsrep_node_name = node4wsrep_node_address=10.128.20.16#wsrep_provider_options="socket.ssl_key=/etc/pki/galera/galera.key; socket.ssl_cert=/etc/pki/galera/galera.crt;"

提示:如果不用ssl的方式認證的話,請把wsrep_provider_options 注釋掉。

將此檔案複製到node5、node6,注意要把 wsrep_node_namewsrep_node_address改成相應節點的 hostnameip

4.啟動 MariaDB Galera Cluster 服務
[root@node4 ~]# /usr/libexec/mysqld --wsrep-new-cluster --user=root &

觀察日誌:

[root@node4 ~]# tail -f /var/log/mariadb/mariadb.log150701 19:54:17 [Note] WSREP: wsrep_load(): loading provider library 'none'150701 19:54:17 [Note] /usr/libexec/mysqld: ready for connections.Version: '5.5.40-MariaDB-wsrep'  socket: '/var/lib/mysql/mysql.sock'  port: 3306  MariaDB Server, wsrep_25.11.r4026

出現 ready for connections ,證明我們啟動成功,繼續啟動其他節點:

[root@node5 ~]# systemctl start mariadb[root@node6 ~]# systemctl start mariadb

可以查看/var/log/mariadb/mariadb.log,在日誌可以看到節點均加入了叢集中。

警告:--wsrep-new-cluster 這個參數只能在初始化叢集使用,且只能在一個節點使用。

5.查看叢集狀態

我們可以關注幾個關鍵的參數:

wsrep_connected = on 連結已開啟

wsrep_local_index = 1在叢集中的索引值

wsrep_cluster_size =3叢集中節點的數量

wsrep_incoming_addresses = 10.128.20.17:3306,10.128.20.16:3306,10.128.20.18:3306 叢集中節點的訪問地址

6.驗證資料同步

我們在node4上建立資料庫 galera_test ,然後在node5node6 上查詢,如果可以查詢到 galera_test 這個庫,說明資料同步成功,叢集運行正常。

[root@node4 ~]# mysql  -uroot  -proot  -e  "create database galera_test"
[root@node5 ~]# mysql  -uroot  -proot  -e  "show databases"+--------------------+| Database           |+--------------------+| information_schema || galera_test        || mysql              || performance_schema |+--------------------+
[root@node6 ~]# mysql  -uroot  -proot  -e  "show databases"+--------------------+| Database           |+--------------------+| information_schema || galera_test        || mysql              || performance_schema |+--------------------+

至此,我們的 MariaDB Galera Cluster 已經成功部署。

參考文章:
[1]http://galeracluster.com/documentation-webpages/
[2]https://mariadb.com/kb/en/mariadb/getting-started-with-mariadb-galera-cluster/

本文系OneAPM工程師原創文章。OneAPM是中國基礎軟體領域的新興領軍企業,能協助企業使用者和開發人員輕鬆實現:緩慢的程式碼和SQL語句的即時抓取。想閱讀更多技術文章,請訪問OneAPM官方技術部落格。

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.