簡介
本文概述簡單的步驟,教導如何建置 Master-Master Replication 叢集(Galera Cluster)架構的MariaDB(請參考下圖),並安裝phpmyadmin管理MariaDB資
料庫。
Galera Cluster 介紹
為什麼要選擇 Galera Cluster Server,它有什麼優點及功能呢?MySQL/Galera 是一套可以同步多台 MySQL/InnoDB 機器的叢集系統,底下可以列出功能。
- 同步複製資料
- 可讀取和寫入叢集系統內任一節點
- 自動偵測節點錯誤,如果有節點當機,則叢集系統自動移除該節點
- 可任意擴充節點
- 採用 row level 方式來平行複製資料
從上面功能看來,我們可以平行任意擴充節點,動態增加伺服器到叢集系統,要做到上面功能,就是利用
Galera library 來做到同步資料處理,同步的詳細細節,可以參考 Galera library 連結。這邊就不再多描述了。
環境介紹及前置作業
作業系統版本 : Centos 6.5 (64位元)
MariaDB資料庫版本 : 5.5.38 (Galera-Server 64位元)
PHP版本 : 5.4.X (REMI版本)
安裝編輯及wget套件
若於Centos中已有安裝vim (編輯工具)和wget(抓檔案工具)時,可以忽略該步驟
yum -y install wget vim
關閉SELinux
編輯 /etc/sysconfig/selinux檔案,將SELINUX=enforcing 改成 SELINUX=disabled,重新開機。
# This file controls the state of SELinux on the system
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
# targeted - Targeted processes are protected,>
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
設定Open File個數
編輯 /etc/security/limits.conf檔案,設定Open File個數
* hard nofile 65535
* soft nofile 65535
開啟防火牆
編輯 /etc/sysconfig/iptables檔案,開啟8080防火牆(用於node.js的服務),設定完成後請重新啟動防火牆。
-A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4567 -j ACCEPT 使用於Galera
-A INPUT -m state --state NEW -m tcp -p tcp --dport 4444 -j ACCEPT 使用於同步資料
-A INPUT -m state --state NEW -m udp -p udp --dport 4444 -j ACCEPT 使用於同步資料
/etc/init.d/iptables restart
安裝及設定Apache
輸入 yum install httpd* ,安裝Apacher Server相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum -y install httpd*
chkconfig --level 345 httpd on
chkconfig --list httpd
為了安全性考量,設定用戶無法瀏覽Apache預設的首頁,請參考以下步驟進行設定(每行前面都加入#符號,共計4行)。
vi /etc/httpd/conf.d/welcome.conf
#<LocationMatch "^/+$">
#Options -Indexes
#ErrorDocument 403 /error/noindex.html
#</LocationMatch>
啟用Apache時若有出現
Could not reliably determine the server's fully qualified domain name訊息,請修改如下之檔案內容
vi /etc/httpd/conf/httpd.conf
ServerName localhost:80 #新增加這一項參數
重新啟動Apache Server。
/etc/init.d/httpd restart
設定php.ini檔案
編輯 /etc/php.ini檔案,設定以下參數
vi /etc/php.ini
修改為: max_execution_time = 300
修改為: max_input_time = 600
修改為: post_max_size = 800M
修改為: upload_max_filesize = 800M
修改為: default_charset="utf8"
修改為: default_socket_timeout = 300
修改為: short_open_tag = On
修改為: date.timezone = "Asia/Taipei"
安裝及設定MariaDB資料庫
安裝 EPEL 套件
因安裝MariaDB-Galera需要socat套件,故需要先安裝EPEL套件來源檔,請參考以下步驟進行設定。
rpm -ivh http://mirror01.idc.hinet.net/EPEL/6/x86_64/epel-release-6-8.noarch.rpm
設定MariaDB的來源碼
編輯 /etc/yum.repos.d/mariadb.repo檔案,若是32位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-x86
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
若為64位元系統請根據以下方式設定
[mariadb]
name = MariaDB
baseurl = http://yum.mariadb.org/5.5/centos6-amd64
gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB
gpgcheck=1
安裝MariaDB
安裝MariaDB相關套件,並設定於開機時自動啟用,請參考以下步驟進行設定。
yum install MariaDB-Galera-server MariaDB-client galera
service mysql start
駐: 安裝後啟動MariaDB,MariaDB的服務稱為mysql,與MySQL服務名稱(mysqld)不同
設定MariaDB自動啟動
chkconfig --level 345 mysql on
chkconfig --list mysql
MariaDB 設定檔的位置位於/etc/my.cnf.d
my.cnf.d目錄中有兩個檔案,mysql-clients.cnf和server.cnf檔案,可將設定檔之範例檔案拷貝至my.cnf.d/目錄中,範例檔位於 /usr/share/mysql/ 目錄中,可根據主機的記憶體規格選擇合適的設定檔
- my-innodb-heavy-4G.cnf: 適合大於 4GB RAM的伺服器使用。
- my-huge.cnf: 適合 1GB – 2GB RAM的伺服器使用。
- my-large.cnf: 適合 512MB RAM的伺服器使用。
- my-medium.cnf: 適合64MB~512 RAM 的伺服器使用。
- my-small.cnf: 適合記憶體小於 64MB的伺服器。
拷貝範例設定檔至/etc/my.cnf.d/目錄中,命名為mysql-clients.cnf
cp /usr/share/mysql/my-huge.cnf /etc/my.cnf.d/mysql-clients.cnf
service mysql restart
設定MariaDB root帳號之密碼
/usr/bin/mysqladmin -u root password '新密碼'
重新啟動 MariaDB Server
/etc/init.d/mysql restart
啟動MariaDB並配置Galera同步配置
建立 cluster 使用者,密碼為p@ssw0rd,針對 % 跟 localhost 同時建立,帳號密碼配置(每一台資料庫中都建立同步用之帳號)
GRANT USAGE ON *.* to cluster@'%' IDENTIFIED BY 'p@ssw0rd';
GRANT ALL PRIVILEGES on *.* to cluster@'%';
GRANT USAGE ON *.* to cluster@'localhost' IDENTIFIED BY 'p@ssw0rd';
GRANT ALL PRIVILEGES on *.* to cluster@'localhost';
FLUSH PRIVILEGES;
建立Galera設定檔 wsrep.cnf
cp /usr/share/mysql/wsrep.cnf /etc/my.cnf.d/
於第一台主機(192.168.1.100)中編輯 /etc/my.cnf.d/wsrep.cnf 檔案,輸入以下設定
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_sst_auth=cluster:p@ssw0rd
# Galera Cluster Configuration
wsrep_cluster_name="test_cluster"
wsrep_cluster_address="gcomm://第二台主機IP"
# Galera Node Configuration
wsrep_node_address="192.168.1.100"
wsrep_node_name="this_node_name"
重新啟動 mysql 後並執行 netstat -na 看到多一個 listen 4567 port
service mysql start --wsrep-new-cluster
netstat -na
LISTEN
tcp 0 0 0.0.0.0:4567 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:3306 0.0.0.0:*
於第二台主機(192.168.1.101)中編輯 /etc/my.cnf.d/wsrep.cnf 檔案,將 cluster address 設定為第一台主機的IP,輸入以下設定
wsrep_provider=/usr/lib64/galera/libgalera_smm.so
wsrep_sst_auth=cluster:p@ssw0rd
# Galera Cluster Configuration
wsrep_cluster_name="test_cluster"
wsrep_cluster_address="gcomm://第一台主機IP"
# Galera Node Configuration
wsrep_node_address="192.168.1.101"
wsrep_node_name="this_node_name"
重新啟動 mysql 後並執行 netstat -na 看到多一個 listen 4567 port
/etc/init.d/mysql restart
於第一台和第二台主機中執行以下指令,確認Cluster設定完成並啟用
$ mysql -e "SHOW STATUS LIKE 'wsrep%';"
可看到如下結果
+------------------------------+--------------------------------------+
| Variable_name | Value |
+------------------------------+--------------------------------------+
| wsrep_local_state_uuid | b5fc5a4a-1782-11e4-8689-b3cbc5faf010 |
| wsrep_protocol_version | 5 |
| wsrep_last_committed | 0 |
| 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 | 2 |
| wsrep_received_bytes | 216 |
| 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 | 0.000000 |
| wsrep_commit_oooe | 0.000000 |
| wsrep_commit_oool | 0.000000 |
| wsrep_commit_window | 0.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 | 210.71.253.55:3306,210.61.8.40:3306 |
| wsrep_cluster_conf_id | 4 |
| wsrep_cluster_size | 2 |
| wsrep_cluster_state_uuid | b5fc5a4a-1782-11e4-8689-b3cbc5faf010 |
| 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 |
| wsrep_provider_version | 25.3.5(rXXXX) |
| wsrep_ready | ON |
| wsrep_thread_count | 2 |
+------------------------------+--------------------------------------+
測試 Master-Master Replication 架構
登入其中一台主機,建立新的資料庫(playground)與表格(equipment)
mysql -u root -p
CREATE DATABASE playground;
use playground;
CREATE TABLE equipment ( id INT NOT NULL AUTO_INCREMENT, type VARCHAR(50), quant INT, color VARCHAR(25), PRIMARY KEY(id));
輸入資料至equipment表格中
INSERT INTO equipment (type, quant, color) VALUES ("slide", 2, "blue");
再登入第二台主機查看是否有新增playground資料庫及equipment資料表,並新增一筆資料至equipment資料表
use playground;
SELECT * FROM playground.equipment;
INSERT INTO equipment (type, quant, color) VALUES ("swing", 10, "yellow");
再登入至第一台主機查看是否equipment資料表有新增一筆資料
use playground;
SELECT * FROM playground.equipment;
增加新的Node至MariaDB Cluster
可任意新增多台 Node 到 Cluster 叢集系統裡,設置過程如下
- 安裝 MariaDB Server
- 安裝 Galera Library
- 設定wsrep.cnf檔案內容
動態設定 gcomm://參數
若新增一台新的 Node,就必須修改其它 Node 的 gcomm:// 設定,並且重新啟動 MariaDB 服務,這樣會有DownTime時間影響服務,故可透過線上修改 GLOBAL wsrep_cluster_address 的參數值達到不須重新啟動服務,請參考如下設定
mysql -e "SHOW VARIABLES LIKE 'wsrep_cluster_address';"
mysql -u root -p
SET GLOBAL wsrep_cluster_address='gcomm://設定IP';
參考資料
安裝及設定phpMyAdmin
解壓縮phpMyAdmin-4.0.10.1-all-languages
tar -zxvf phpMyAdmin-4.0.10.1-all-languages.tar.gz
搬移 phpMyAdmin-4.0.10.1-all-languages目錄到/usr/share,並修改名稱為phpMyAdmin-4.0.10.1
mv phpMyAdmin-4.0.10.1-all-languages /usr/share/phpMyAdmin-4.0.10.1
複製範本設定檔 config.sample.inc.php 並重新命名為 config.inc.php
cd /usr/share/phpMyAdmin-4.0.10.1
cp config.sample.inc.php config.inc.php
修改 phpMyAdmin 的認證方式,請編輯config.inc.php檔案,重新啟動 Apache
/* Authentication type */
$cfg['Servers'][$i]['auth_type'] = 'cookies';
/etc/init.d/httpd restart
修改config.inc.php檔案的權限
chmod 644 config.inc.php
移除setup安裝資料夾
mv setup setup.org
設定phpMyAdmin網站別名(可視情況設定)
編輯 /etc/httpd/conf/httpd.conf 檔案
#Set Virtual Host
Alias /mysqladm "/usr/share/phpMyAdmin-4.0.10.1"
<Directory "/usr/share/phpMyAdmin-4.0.10.1">
Options -Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
開啟瀏覽器輸入http://您的IP/mysqladm將會看到phpMyAdmin登入網頁,輸入使用者名稱及密碼後即可開始管理MySQL資料庫
Troubleshooting
假設您有遇到 Native table 'performance_schema'.'???' has the wrong structure 等的錯誤訊息LOG (位於/var/lib/mysql/) 時, 表示資料表因為資料庫升版造成異常,可使用以下的指令進行修復
mysql_upgrade -u root -p