MariaDB 做爲 Mysql 的一個分支,在開源項目中已經普遍使用,例如大熱的 openstack,因此,爲了保證服務的高可用性,同時提升系統的負載能力,集羣部署是必不可少的。html
MariaDB 集羣是 MariaDB 同步多主機集羣。它僅支持 XtraDB/ InnoDB 存儲引擎(雖然有對 MyISAM 實驗支持 - 看 wsrep_replicate_myisam 系統變量)。node
主要功能:mysql
優點:linux
技術:web
Galera 集羣的複製功能基於 Galeralibrary 實現,爲了讓 MySQL 與 Galera library 通信,特別針對 MySQL 開發了 wsrep API。sql
Galera 插件保證集羣同步數據,保持數據的一致性,靠的就是可認證的複製,工做原理以下圖:數據庫
當客戶端發出一個 commit 的指令,在事務被提交以前,全部對數據庫的更改都會被write-set
收集起來,而且將 write-set
紀錄的內容發送給其餘節點。vim
write-set
將在每一個節點進行認證測試,測試結果決定着節點是否應用write-set
更改數據。centos
若是認證測試失敗,節點將丟棄 write-set ;若是認證測試成功,則事務提交。服務器
安裝 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 node4 10.128.20.17 node5 10.128.20.18 node6
爲了保證節點間相互通訊,須要禁用防火牆設置(若是須要防火牆,則參照官方網站增長防火牆信息設置)
在三個節點分別執行命令:
systemctl stop firewalld
而後將/etc/sysconfig/selinux
的 selinux
設置成 disabled
,這樣初始化環境就完成了。
[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
初始化數據庫服務,只在一個節點進行
[root@node4 mariadb]# systemctl start mariadb [root@node4 mariadb]# mysql_secure_installation NOTE: 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 current password for the root user. If you've just installed MariaDB, and you 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 MariaDB root 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 anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] n ... skipping. Normally, root should only be allowed to connect from 'localhost'. This ensures 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 can access. This is also intended only for testing, and should be removed before 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 far will 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 MariaDB installation 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.so wsrep_cluster_address = "gcomm://node4,node5,node6" wsrep_node_name = node4 wsrep_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
註釋掉。
將此文件複製到node五、node6,注意要把 wsrep_node_name
和 wsrep_node_address
改爲相應節點的 hostname
和ip
。
[root@node4 ~]# /usr/libexec/mysqld --wsrep-new-cluster --user=root &
觀察日誌:
[root@node4 ~]# tail -f /var/log/mariadb/mariadb.log 150701 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
這個參數只能在初始化集羣使用,且只能在一個節點使用。
咱們能夠關注幾個關鍵的參數:
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
集羣中節點的訪問地址
咱們在node4
上新建數據庫 galera_test
,而後在node5
和node6
上查詢,若是能夠查詢到 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/