MySQL高可用架構之MaxScale實踐
MaxScale是mariadb公司開發的一套資料庫中介軟體。它是一個支援高可用、讀寫分離、負載均衡,並且具有良好的可擴充套件性,不但高效能的基於事件驅動,同時具有代理和管理功能。
1、MaxScale軟體下載
下載地址:
--原始碼下載
參考手冊:docs
2、解壓安裝包
(二進位制安裝包)
[root@node3 MaxScale]# tar -xzvf maxscale-1.4.3-1.rhel.6.x86_64.tar.gz
3、安裝依賴包
配置本地yum源:
[root@node3 MaxScale]# mkdir /media/cdrom
[root@node3 MaxScale]# mount CentOS-6.4-x86_64-bin-DVD1.iso /media/cdrom/ -o loop
[root@node3 MaxScale]# rm -rf /etc/yum.repos.d/*.repo
[root@node3 MaxScale]# vi /etc/yum.repos.d/CentOS6.repo
[Base]
name=CentOS6 ISO Base
baseurl=file:///media/cdrom
enabled=1
gpgcheck=0
依賴包檢查安裝:
yum install git gcc gcc-c++ ncurses-devel bison flex glibc-devel cmake libgcc perl make libtool openssl-devel libaio libaio-devel librabbitmq-devel libcurl-devel pcre-devel tcl tcl-devel systemtap-sdt-devel libuuid libuuid-devel
rpm -q libaio libaio-devel novacom-server libedit gcc gcc-c++ ncurses-devel bison glibc-devel cmake libgcc perl make libtool openssl-devel libaio libaio-devel librabbitmq-devel libcurl-devel pcre-devel
升級openssl
[root@node3 MaxScale]# rpm -Uvh openssl-1.0.1e-42.el6_7.1.x86_64.rpm --nodeps
[root@node3 MaxScale]# rpm -Uvh openssl-devel-1.0.1e-42.el6_7.1.x86_64.rpm
4、MaxScale安裝和部署
MaxScale安裝:
1)rpm包安裝
[root@node3 MaxScale]# rpm -ivh maxscale-beta-2.0.0-1.centos.6.x86_64.rpm --nodeps
2)原始碼安裝
(以下是原始碼編譯,其中編譯沒透過,原始碼安裝失敗,建議在centos/rhel 7以上版本原始碼安裝)
原始碼安裝包要求:
CMake version 2.8 or later (Packaging requires version 2.8.12 or later)
GCC version 4.4.7 or later
libaio
OpenSSL
Bison 2.7 or later
Flex 2.5.35 or later
libuuid
rhel 5,6 :libedit-devel MariaDB-devel MariaDB-server
rhel 7 :mariadb-devel mariadb-embedded-devel libedit-devel
[root@node3 MaxScale]# cd MaxScale-2.0
[root@node3 MaxScale-2.0]# cmake ./
或者手動指定編譯引數,如下
cmake ./ -DCMAKE_INSTALL_PREFIX=/usr/local/maxscale \
-DMYSQL_DIR=/usr/local/mysql/include/ \
-DEMBEDDED_LIB=/usr/local/mysql/lib/libmysqld.a \
-DMYSQL_EMBEDDED_LIBRARIES=/usr/local/mysql/lib/ \
-DERRMSG=/usr/local/mysql/share/english/errmsg.sys
[root@node3 MaxScale-2.0]# make -j 8
[root@node3 MaxScale-2.0]# make install
3)二進位制安裝
[root@node3 MaxScale]# mv maxscale-1.4.3-1.rhel.6.x86_64 /usr/local/maxscale
在~/.bash_profile中新增以下環境變數
export MAXSCALE_HOME=/usr/local/maxscale
export LD_LIBRARY_PATH=/usr/local/maxscale/lib
MaxScale配置:
在 master 中為 MaxScale 建立兩個使用者,用於監控模組和路由模組
建立監控賬戶
mysql> create user maxscalemon@'%' identified by "monitor" ;
mysql> grant replication slave, replication client on *.* to maxscalemon@'%';
建立路由使用者
mysql> create user maxscale@'%' identified by "maxscale";
mysql> grant select on mysql.* to maxscale@'%';
mysql> grant show databases on *.* to 'maxscale'@'%';
mysql> flush privileges;
編輯配置檔案:
主要修改的檔案有server1的IP地址和埠以及複製相應的server2,server3配置,修改監控和路由配置,清除只讀服務配置。
[root@node3 MaxScale]# cp /etc/maxscale.cnf /etc/maxscale.cnf_20160823
[root@node3 MaxScale]# vi /etc/maxscale.cnf
[maxscale]
threads=1
[server1]
type=server
address=IP1
port=3306
protocol=MySQLBackend
[server2]
type=server
address=IP2
port=3306
protocol=MySQLBackend
[server3]
type=server
address=IP3
port=3306
protocol=MySQLBackend
[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2,server3
user=maxscalemon
passwd=monitor ##--改為加密的密碼
monitor_interval=10000
[Read-Only Service]
type=service
router=readconnroute
servers=server1,server2,server3
user=maxscale
passwd=maxscale ##--改為加密的密碼
router_options=slave
[Read-Write Service]
type=service
router=readwritesplit
servers=server1
user=maxscale
passwd=maxscale ##--改為加密的密碼
max_slave_connections=100%
[MaxAdmin Service]
type=service
router=cli
[Read-Only Listener]
type=listener
service=Read-Only Service
protocol=MySQLClient
port=4008
[Read-Write Listener]
type=listener
service=Read-Write Service
protocol=MySQLClient
port=4006
[MaxAdmin Listener]
type=listener
service=MaxAdmin Service
protocol=maxscaled
#socket=default
port=6603
加密密碼
[root@node3 ~]# maxkeys /var/lib/maxscale
加密配置檔案密碼
[root@node3 ~]# maxpasswd /var/lib/maxscale/.secrets monitor
7429FE1AABA353442178F74131697531
[root@node3 ~]# maxpasswd /var/lib/maxscale/.secrets maxscale
26C20853B625AD18686C0D2AC8A11E60
將加密後的密碼填寫到配置檔案中
啟動maxscale服務:
[root@node3 MaxScale]# maxscale --config=/etc/maxscale.cnf
或者 maxscale -f /etc/maxscale.cnf
5、MaxScale使用
1) 透過service服務關閉、啟動、重啟、檢視maxscale狀態等
[root@node3 MaxScale]# service maxscale
Usage: /etc/init.d/maxscale {start|stop|status|restart|condrestart|reload}
2) 啟動maxscale
service maxscale start
maxscale -f /etc/maxscale.conf
3) 關閉maxscale
service maxscale stop
4) 登陸maxscale管理控制檯管理
[root@node3
MaxScale]# maxadmin -uadmin -pmariadb
-P6603
6、MaxScale讀寫分離和負載均衡測試
開啟general log
mysql> show variables like 'general_log';
mysql> set global general_log=1;
[root@node3 MaxScale]# mysql -udbadmin -pdbadmin -hip3 -P4008 -e "select * from dbtest.t2"
[root@node2 ~]# tailf /usr/local/mysql/data/node2.log
166 Connect dbadmin@node3 on
166 Query select @@version_comment limit 1
166 Query select * from dbtest.t1
[root@node3 MaxScale]# mysql -udbadmin -pdbadmin -hip3 -P4008 -e "select * from dbtest.t2"
[root@node3 ~]# tailf /usr/local/mysql/data/node3.log
160825 14:25:29 208 Connect dbadmin@node3 on
208 Query select @@version_comment limit 1
208 Query select * from dbtest.t2
208 Quit
179 Query SELECT @@server_id
179 Query SHOW SLAVE STATUS
160825 14:25:30 209 Connect dbadmin@node3 on
209 Query select @@version_comment limit 1
209 Query select * from dbtest.t2
209 Quit
160825 14:25:31 204 Query SELECT @@server_id
204 Query SHOW SLAVE STATUS
[root@node3 MaxScale]# mysql -udbadmin -pdbadmin -hip3 -P4006 -e "insert into dbtest.t2 values(1111)"
[root@node1 ~]# tailf /usr/local/mysql/data/node1.log
160825 14:26:53 203 Connect dbadmin@node3 on
203 Query select @@version_comment limit 1
203 Query insert into dbtest.t2 values(1111)
203 Quit
7、MaxScale安裝錯誤資訊以及解決方案錯誤資訊01:
Initialized empty Git repository in /tools/MaxScale/MaxScale-2.0/build/connector-c-prefix/src/connector-c/.git/
error: Couldn't resolve host 'github.com' while accessing
fatal: HTTP request failed
Initialized empty Git repository in /tools/MaxScale/MaxScale-2.0/build/connector-c-prefix/src/connector-c/.git/
error: Couldn't resolve host 'github.com' while accessing
fatal: HTTP request failed
Initialized empty Git repository in /tools/MaxScale/MaxScale-2.0/build/connector-c-prefix/src/connector-c/.git/
error: Couldn't resolve host 'github.com' while accessing
fatal: HTTP request failed
-- Had to git clone more than once:
3 times.
CMake Error at /tools/MaxScale/MaxScale-2.0/build/connector-c-prefix/tmp/connector-c-gitclone.cmake:40 (message):
Failed to clone repository:
''
make[2]: *** [connector-c-prefix/src/connector-c-stamp/connector-c-download] Error 1
make[1]: *** [CMakeFiles/connector-c.dir/all] Error 2
make: *** [all] Error 2
解決方案:
上述錯誤是原始碼編譯報錯,暫無好的解決方案,如果可以連線外網可以透過原始碼編譯安裝或者使用高版本os的伺服器。
錯誤資訊02:
2016-08-23 18:26:54 notice : Loaded module mysqlmon: V1.4.0 from /usr/lib64/maxscale/libmysqlmon.so
2016-08-23 18:26:54 notice : Encrypted password file /var/lib/maxscale/.secrets can't be accessed (No such file or directory). Password encryption is not used.
2016-08-23 18:26:54 error : 1 errors were encountered while processing the configuration file '/etc/maxscale.cnf'.
2016-08-23 18:26:54 error : Failed to open, read or process the MaxScale configuration file /etc/maxscale.cnf. Exiting.
2016-08-23 18:26:54 MaxScale is shut down.
解決方案:
建立密碼檔案,並將加密後的密碼在配置檔案中替換。
maxkeys /var/lib/maxscale
maxpasswd /var/lib/maxscale/.secrets monitor
maxpasswd /var/lib/maxscale/.secrets maxscale
錯誤資訊03:
2016-08-24 11:48:43 notice : Loaded module readconnroute: V1.1.0 from /usr/lib64/maxscale/libreadconnroute.so
2016-08-24 11:48:43 error : The service 'Read-Only Service' is missing a definition of the servers that provide the service.
解決方案:
將配置檔案中的readonly內容補全,內容如下
[Read-Only Service]
type=service
router=readconnroute
servers=
user=maxscale
passwd=26C20853B625AD18686C0D2AC8A11E60
router_options=slave
錯誤資訊04:
rpm -ivh maxscale-1.4.3-1.rhel.6.x86_64.rpm
warning: maxscale-1.4.3-1.rhel.6.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID 8167ee24: NOKEY
error: Failed dependencies:
libcrypto.so.10(libcrypto.so.10)(64bit) is needed by maxscale-1.4.3-1.x86_64
libssl.so.10(libssl.so.10)(64bit) is needed by maxscale-1.4.3-1.x86_64
解決方案:
透過升級openssl解決,rpm -Uvh openssl-1.0.1e-42.el6_7.1.x86_64.rpm --nodeps和rpm -Uvh openssl-devel-1.0.1e-42.el6_7.1.x86_64.rpm 。
8、MaxScale附錄
1)rpm預設安裝包相關路徑
/usr/share/maxscale/ 共享配置安裝路徑
/var/lib/maxscale 資料檔案目錄
/usr/bin/maxscale 執行檔案目錄
/var/log/maxscale 日誌目錄
/usr/lib64/maxscale 庫檔案目錄
2)配置檔案詳解
[maxscale]
threads=auto #開啟執行緒個數,預設為1.設定為auto會同cpu核數相同
ms_timestamp=1 #timestamp精度
syslog=1 #將日誌寫入到syslog中
maxlog=1 #將日誌寫入到maxscale的日誌檔案中
log_to_shm=0 #不將日誌寫入到共享快取中,開啟debug模式時可開啟加快速度
log_warning=1 #記錄告警資訊
log_notice=1 #記錄notice
log_info=1 #記錄info
log_debug=0 #不開啟debug模式
log_augmentation=1 #日誌遞增
#相關目錄設定 ,如果改變rpm的日誌和資料檔案路徑,需要透過建立相應目錄並改變相應屬主為maxscale。
logdir=/usr/local/maxscale/log/
datadir=/usr/local/maxscale/data/
libdir=/usr/lib64/maxscale/
cachedir=/usr/local/maxscale/cache/
piddir=/usr/local/maxscale/
execdir=/usr/bin/
#相關的監控資訊,監控的使用者需要對後端資料庫有訪問replication client的許可權:grant replication client
[MySQL Monitor]
type=monitor
module=mysqlmon
servers=server1,server2,server3
user=root
passwd=7AE087FBF864EBB87D108C3AB1603D0D
monitor_interval=1000 #監控心跳為1秒
detect_replication_lag=true #監控主從複製延遲,可用後續指定router service的max_slave_replication_lag單位是秒,來控制maxscale執行的最大延遲
detect_stale_master=true #當複製slave全部斷掉時,maxscale仍然可用,將所有的訪問指向
#read-only的只讀節點slave分離
[Read-Only Service]
type=service
router=readconnroute
servers=server1,server2,server3
user=root
passwd=7AE087FBF864EBB87D108C3AB1603D0D
router_options=slave
enable_root_user=1
#讀寫分離,使用者需要有SELECT ON mysql.db;SELECT ON mysql.tables_priv;SHOW DATABASES ON *.*的許可權
[Read-Write Service]
type=service
router=readwritesplit
servers=server1,server2,server3
user=root
passwd=7AE087FBF864EBB87D108C3AB1603D0D
use_sql_variables_in=master #sql語句中的存在變數只指向master中執行
enable_root_user=1 #允許root使用者登入執行
# master_accept_reads=true #master節點也可以轉發讀請求
max_slave_replication_lag=5 #複製延遲最大為5秒(必須比monitor的interval大)
3)maxscale缺點
1)建立連結的時候,不支援壓縮協議
2)轉發路由不能動態的識別master節點的遷移
3)LONGLOB欄位不支援
4)在一下情況會將語句轉到master節點中(保證事務一致):
明確指定事務;
prepared的語句;
語句中包含儲存過程,自定義函式
包含多條語句資訊:INSERT INTO ... ; SELECT LAST_INSERT_ID();
5)一些語句預設會傳送到後端的所有server中,但是可以指定use_sql_variables_in=[master|all] (default: all)
6)maxscale不支援主機名匹配的認證模式,只支援IP地址方式的host解析。所以在新增user的時候記得使用合適的正規化。
7)跨庫查詢不支援,會顯示的指定到第一個資料庫中
8)透過select方式改變會話變數的行為不支援
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/27067062/viewspace-2124039/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- MySQL高可用架構之MHA實踐MySql架構
- MySQL高可用架構之PXC實踐MySql架構
- MySQL高可用架構之MHA 原理與實踐MySql架構
- MySQL 實現高可用架構之 MHAMySql架構
- MySQL 高可用架構之 MMM 架構MySql架構
- MySQL高可用架構案例篇:UCloud最佳實踐MySql架構Cloud
- Redis 高可用架構最佳實踐Redis架構
- Mysql高可用架構方案MySql架構
- MySQL高可用架構:mysql+keepalived實現MySql架構
- MySQL高可用架構之Keepalived+主從架構部署MySql架構
- MySQL資料庫實現高可用架構之MHA的實戰MySql資料庫架構
- MySQL高可用架構對比MySql架構
- mysql高可用架構MHA搭建MySql架構
- Keepalived 架構高可用 Mysql架構MySql
- mysql MHA 高可用架構部署MySql架構
- 構建高併發高可用的電商平臺架構實踐架構
- 構建MHA實現MySQL高可用叢集架構MySql架構
- 同程旅行基於 RocketMQ 高可用架構實踐MQ架構
- 來自 Google 的高可用架構理念與實踐Go架構
- MySQL高可用架構設計分析MySql架構
- 《MySQL效能優化和高可用架構實踐》簡介與推薦序MySql優化架構
- 高可用架構架構
- 深入高可用架構原理與實踐 書籍學習架構
- MySQL高可用架構-MMM、MHA、MGR、PXCMySql架構
- 淺談MySQL叢集高可用架構MySql架構
- 架構師日記-軟體高可用實踐那些事兒架構
- 【Mysql】高可用架構之-Lvs+keepalive+Altas+MHAMySql架構
- MySQL主從原理, 高可用架構與高效能架構MySql架構
- Oracle 高可用架構Oracle架構
- MySQL資料庫架構——高可用演進MySql資料庫架構
- MySQL 高可用架構 - MHA環境部署記錄MySql架構
- MySQL高可用架構-MHA環境部署記錄MySql架構
- MySQL高可用架構-MMM環境部署記錄MySql架構
- mysql高可用架構MHA搭建(centos7+mysql5.7.28)MySql架構CentOS
- MHA高可用架構的實現方式架構
- MySQL叢集架構:MHA+MySQL-PROXY+LVS實現MySQL叢集架構高可用/高效能MySql架構
- Canal高可用架構部署架構
- 【Redis】Sentinel 高可用架構Redis架構