參考文章來源:
https://www.cnblogs.com/kevingrace/p/10466530.html
一.Innodb cluster集群介紹
- MySQL的高可用架構(gòu)無論是社區(qū)還是官方资铡,一直在技術(shù)上進(jìn)行探索邦尊,這么多年提出了多種解決方案茅姜,比如
MMM
,MHA
,NDB Cluster
,Galera Cluster
,InnoDB Cluster
, 騰訊的PhxSQL
,MySQL Fabric
,aliSQL
修赞。 - MySQL官方在2017年4月推出了一套完整的沮明、高可用的Mysql解決方案 - MySQL InnoDB Cluster, 即一組MySQL服務(wù)器可以配置為一個(gè)MySQL集群神年。在默認(rèn)的單主節(jié)點(diǎn)模式下卖局,集群服務(wù)器具有一個(gè)讀寫主節(jié)點(diǎn)和多個(gè)只讀輔節(jié)點(diǎn)拒秘。輔助服務(wù)器是主服務(wù)器的副本考榨】缦福客戶端應(yīng)用程序通過MySQL Router連接到主服務(wù)程序。如果主服務(wù)連接失敗河质,則次要的節(jié)點(diǎn)自動(dòng)提升為主節(jié)點(diǎn)冀惭,MySQL Router請(qǐng)求到新的主節(jié)點(diǎn)。InnoDB Cluster不提供NDB Cluster支持掀鹅。
- 分布式MySQL之InnoDB和NDB
分布式MySQL主要有InnoDB和NDB模式, NDB是基于集群的引擎-數(shù)據(jù)被自動(dòng)切分并復(fù)制到數(shù)個(gè)機(jī)器上(數(shù)據(jù)節(jié)點(diǎn))散休, 適合于那些需要極高查詢性能和高可用性的應(yīng)用, 原來是為愛立信的電信應(yīng)用設(shè)計(jì)的淫半。 NDB提供了高達(dá)99.999%的可靠性溃槐,在讀操作多的應(yīng)用中表現(xiàn)優(yōu)異。 對(duì)于有很多并發(fā)寫操作的應(yīng)用科吭, 還是推薦用InnoDB昏滴。
========== NDB和InnoDB存儲(chǔ)引擎之間的特性差異 ==========
InnoDB(MySQL 5.7)特性:
- InnoDB版本:InnoDB 5.7.20猴鲫;
- NDB Cluster版本:不支持;
- 最大存儲(chǔ)長(zhǎng)度:64TB谣殊;
- 事物:所有標(biāo)準(zhǔn)事物類型拂共;
- 多版本并發(fā)控制:支持;
- 數(shù)據(jù)壓縮:支持姻几;
- 大行支:VARBINARY宜狐、VARCHAR、BLOB蛇捌;
- 同步支持:半同步抚恒、異步;
- 塊讀嚷绨琛:支持俭驮;
- 塊寫入:需要使用水平分區(qū);
- 高可用性:高春贸;
NDB 7.5/7.6特性:
- InnoDB版本:InnoDB 5.7.20混萝;
- NDB Cluster版本: NDB 7.5.8/7.6.4真朗;
- 最大存儲(chǔ)長(zhǎng)度:128TB少漆;
- 事物:讀提交;
- 多版本并發(fā)控制:不支持碰纬;
- 數(shù)據(jù)壓縮支持:不支持允粤;
- 大行支持:BLOB崭倘、 TEXT;
- 同步支持半:自動(dòng)同步维哈;
- 塊讀壬獭:支持;
- 塊寫入:支持阔挠;
- 高可用性:非常高;
- 本次部署采用
InnoDB Cluster
. 每臺(tái)服務(wù)器實(shí)例都運(yùn)行MySQL Group Replication
(冗余復(fù)制機(jī)制脑蠕,內(nèi)置failover
) -
MGR
有兩種模式购撼,一種是Single-Primary
,一種是Multi-Primary
谴仙,即單主或者多主迂求。 - 注意:
Multi-Primary
模式中,所有的節(jié)點(diǎn)都是主節(jié)點(diǎn)晃跺,都可以同時(shí)被讀寫揩局,看上去這似乎更好,但是因?yàn)槎嘀鞯膹?fù)雜性掀虎,在功能上如果設(shè)置了多主模式凌盯,則會(huì)有一些使用的限制付枫,比如不支持Foreign Keys with Cascading Constraints。
MySQL InnoDB Cluster 集群特性, 有什么好處 (為什么使用它)
-
集成易用
MySQL InnoDB集群緊密集成了MySQL Servers with Group Replication驰怎,MySQL Router阐滩,和MySQL Shell,所以不必依賴于外部工具县忌,腳本或其他部件掂榔。 另外它利用了現(xiàn)有的MySQL特性,如:InnoDB, GTIDs, binary logs, multi-threaded slave execution, multi-source replication and Performance Schema症杏∽盎瘢可以在五分鐘內(nèi)利用MySQL Shell中的腳本化的管理API來創(chuàng)建及管理MySQL集群。 -
使用組復(fù)制的mysql server HA
組復(fù)制提供了內(nèi)置的組成員管理厉颤、數(shù)據(jù)一致性保證穴豫、沖突檢測(cè)和處理、節(jié)點(diǎn)故障檢測(cè)和數(shù)據(jù)庫(kù)故障轉(zhuǎn)移相關(guān)操作的本地高可用性走芋,無需人工干預(yù)或自定義工具绩郎。組復(fù)制同時(shí)實(shí)現(xiàn)了帶自動(dòng)選主的單主模式及任意更新的多主模式。通過使用一個(gè)強(qiáng)大的新的組通信系統(tǒng)翁逞,它提供了流行的Paxos算法的內(nèi)部實(shí)現(xiàn)肋杖,來自動(dòng)協(xié)調(diào)數(shù)據(jù)復(fù)制、一致性挖函、membership状植。這提供了使MySQL數(shù)據(jù)庫(kù)高度可用所需的所有內(nèi)置機(jī)制。 -
彈性
通過組復(fù)制怨喘,一組服務(wù)器協(xié)調(diào)在一起形成一個(gè)組津畸。組成員是動(dòng)態(tài)的,服務(wù)器可以自愿或強(qiáng)制的地離開及隨時(shí)加入必怜。組將根據(jù)需要自動(dòng)重新配置自己肉拓,并確保任何加入成員與組同步。這樣就可以方便地在需要時(shí)快速地調(diào)整數(shù)據(jù)庫(kù)的總?cè)萘俊?/li> -
故障檢測(cè)
組復(fù)制實(shí)現(xiàn)了一個(gè)分布式故障檢測(cè)器來查找并報(bào)告failed或不再參與組的服務(wù)器梳庆,組中剩余成員將重新配置暖途。 -
容錯(cuò)
組復(fù)制基于流行的Paxos分布式算法來提供服務(wù)器之間的分布式協(xié)調(diào)。為了使一個(gè)小組繼續(xù)發(fā)揮作用膏执,它要求大多數(shù)成員在線驻售,并就每一個(gè)變化達(dá)成協(xié)議。這允許MySQL數(shù)據(jù)庫(kù)在發(fā)生故障時(shí)安全地繼續(xù)操作更米,而無需人工干預(yù)欺栗,不存在數(shù)據(jù)丟失或數(shù)據(jù)損壞的風(fēng)險(xiǎn)。 -
自愈
如果一個(gè)服務(wù)器加入該組,它將自動(dòng)將其狀態(tài)與現(xiàn)有成員同步迟几。如果服務(wù)器離開該組消请,例如它被取下來進(jìn)行維護(hù),剩下的服務(wù)器將看到它已離開瘤旨,并將自動(dòng)重新配置組梯啤。當(dāng)服務(wù)器后重新加入組,它會(huì)自動(dòng)重新與組同步存哲。 -
監(jiān)測(cè)
MySQL Enterprise Monitor 3.4及以后的版本全面支持組復(fù)制因宇;監(jiān)控每個(gè)節(jié)點(diǎn)的配置,健康祟偷,和性能察滑。并且提供最佳實(shí)踐建議和提醒,以及易于理解的可視化工具修肠,允許您輕松地監(jiān)控和管理您的組復(fù)制和InnoDB集群贺辰。 -
通過MySQL Router為mysql客戶機(jī)應(yīng)用程序?qū)崿F(xiàn)HA
MySQL的路由器允許您輕松遷移您的獨(dú)立的MySQL實(shí)例到本地分布式高可用集群而不影響現(xiàn)有的應(yīng)用程序。新metadata_cache插件為Innodb 集群提供了透明的客戶端連接路由嵌施、負(fù)載平衡和故障轉(zhuǎn)移的能力饲化。 -
簡(jiǎn)單易用的MySQL shell
MySQL Shell為所有MySQL相關(guān)的任務(wù)提供了一個(gè)直觀、靈活吗伤、功能強(qiáng)大的接口吃靠。
新的adminapi使得它很容易用一種自我描述的自然語言來創(chuàng)建,監(jiān)控和管理包括MySQL Router在內(nèi)的MySQL InnoDB集群足淆,而不需要了解低層次的概念巢块,配置選項(xiàng),或其他復(fù)雜的方面巧号。
二.工作原理
MySQL InnoDB集群提供了一個(gè)集成的族奢,本地的,HA解決方案丹鸿。Mysq Innodb Cluster是利用組復(fù)制的 pxos 協(xié)議越走,保障數(shù)據(jù)一致性,組復(fù)制支持單主模式和多主模式靠欢。
MySQL InnoDB集群由以下幾部分組成:
- MySQL Servers with Group Replication:向集群的所有成員復(fù)制數(shù)據(jù)弥姻,同時(shí)提供容錯(cuò)、自動(dòng)故障轉(zhuǎn)移和彈性掺涛。MySQL Server 5.7.17或更高的版本。
- MySQL Router:確碧劢客戶端請(qǐng)求是負(fù)載平衡的薪缆,并在任何數(shù)據(jù)庫(kù)故障時(shí)路由到正確的服務(wù)器。MySQL Router 2.1.3或更高的版本。
- MySQL Shell:通過內(nèi)置的管理API創(chuàng)建及管理Innodb集群拣帽。MySQL Shell 1.0.9或更高的版本疼电。
各個(gè)組件的關(guān)系和工作流程如下:
三.集群方案
我采用3臺(tái)centos7.7 服務(wù)器來搭建MySQL Innodb cluster集群,分別是一主兩從减拭,如下部署:
ip地址 主機(jī)名 角色 安裝軟件
192.168.62.11 node-1 cluster節(jié)點(diǎn)1 Mysql5.7, mysql-shell, mysql-route
192.168.62.12 node-2 cluster節(jié)點(diǎn)2 Mysql5.7, mysql-shell, mysql-route
192.168.62.13 node-3 cluster節(jié)點(diǎn)3 Mysql5.7, mysql-shell
mysql蔽豺,mysql shell 和mysql router的安裝包如下:
- mysql:mysql-5.7.29-linux-glibc2.12-x86_64.tar.gz
- mysql-shell:mysql-shell-1.0.11-linux-glibc2.12-x86-64bit.tar.gz
- mysql-router:mysql-router-2.1.6-linux-glibc2.12-x86-64bit.tar.gz
根據(jù)官方的文檔 https://dev.mysql.com/doc/refman/8.0/en/mysql-innodb-cluster-userguide.html,mysql-shell需要python環(huán)境拧粪,至少在2.7版本以上修陡,所以在安裝使用前,需要提前部署好python環(huán)境可霎。
查看python版本
python -V
Python 2.7.5
四.環(huán)境準(zhǔn)備(所有服務(wù)器都要操作)
1.關(guān)閉防火墻魄鸦,關(guān)閉方法因系統(tǒng)不同,所以不列出
2.關(guān)閉selinux(Centos)癣朗,關(guān)閉方法因系統(tǒng)不同拾因,所以不列出
3.配置每個(gè)節(jié)點(diǎn)的/etc/hosts主機(jī)映射, 方便通過節(jié)點(diǎn)的hostname進(jìn)行連接
這一步很重要,否則可能會(huì)出現(xiàn)無法同步的情況旷余,因?yàn)閿?shù)據(jù)庫(kù)需要根據(jù)member_host同步绢记,如果不配置,默認(rèn)就是localhost正卧,這樣時(shí)無法通信的!!!
vim /etc/hosts
...........
192.168.62.11 node-1
192.168.62.12 node-2
192.168.62.13 node-3
4.優(yōu)化配置
[root@localhost ~]# cat>>/etc/sysctl.conf <<EOF
fs.aio-max-nr = 1048576
fs.file-max = 681574400
kernel.shmmax = 137438953472
kernel.shmmni = 4096
kernel.sem = 250 32000 100 200
net.ipv4.ip_local_port_range = 9000 65000
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 1048586
EOF
[root@localhost ~]# sysctl -p
#mysql服務(wù)需要打開15000個(gè)文件數(shù)蠢熄,默認(rèn)用戶只有1024(通過ulimit -n 查詢當(dāng)前是多少),修改如下,其中cluster為我安裝mysql使用的linux用戶:
[root@localhost ~]# cat>>/etc/security/limits.conf <<EOF
cluster soft nproc 65536
cluster hard nproc 65536
cluster soft nofile 65536
cluster hard nofile 65536
EOF
五.環(huán)境部署記錄
5.1.在三個(gè)cluster節(jié)點(diǎn)安裝和部署Mysql5.7及 mysql-shell穗酥。
三個(gè)節(jié)點(diǎn)同樣操作,此處只列出第一個(gè)節(jié)點(diǎn)的安裝配置記錄
安裝mysql5.7
使用yum方式安裝Mysql5.7护赊,參考:
https://www.cnblogs.com/kevingrace/p/8340690.html
非root用戶下安裝mysql,參考:
http://www.reibang.com/p/d91e79b5146b安裝mysql-shell
#解壓
tar -zvxf mysql-shell-1.0.11-linux-glibc2.12-x86-64bit.tar.gz
mv mysql-shell-1.0.11-linux-glibc2.12-x86-64bit mysql-shell
#在用戶目錄添加環(huán)境變量砾跃,編輯.bash_profile或者/etc/profile末尾添加
export PATH=$PATH:/home/cluster/mysql-shell/bin
#更新環(huán)境變量
source /etc/profile
#輸入指令查詢版本
mysqlsh --version
mysqlsh Ver 1.0.11 for Linux on x86_64 - for MySQL 5.7.20 (MySQL Community Server (GPL))
- 測(cè)試mysql-shell
[cluster@centos76 ~]$ mysqlsh
MySQL Shell 1.0.11
Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type '\help' or '\?' for help; '\quit' to exit.
Currently in JavaScript mode. Use \sql to switch to SQL mode and execute queries.
mysql-js>
#連接本節(jié)點(diǎn)數(shù)據(jù)庫(kù)
mysql-js> shell.connect('root@node-1:3305');
#回車骏啰,會(huì)提示輸入密碼
Please provide the password for 'root@node-1:3305':
Creating a Session to 'root@node-1:3305'
Your MySQL connection id is 20
No default schema selected; type \use <schema> to set one.
mysql-js>
#如上所示,表示連接成功
連接時(shí)可能會(huì)報(bào)如下錯(cuò)誤:
Shell.connect: Host 'node-1' is not allowed to connect to this MySQL server (MySQL Error 1130)
需要在MySQL中抽高,新建用戶及授權(quán)判耕,如下:
CREATE USER 'root'@'%' IDENTIFIED BY '123456';
grant all privileges on *.* to 'root'@'%' with grant option;
flush privileges; /*刷新權(quán)限*/
- 配置my.cnf
- 配置前先備份
- 修改不同機(jī)器的server_id、ip翘骂;3個(gè)cluster節(jié)點(diǎn)除了server_id壁熄、loose-group_replication_local_address 兩個(gè)參數(shù)不一樣外,其他保持一致碳竟。
- loose-group_replication_group_name使用UUID形式草丧,集群中機(jī)器使用同一個(gè)UUID;
- loose-group_replication_single_primary_mode在單主模式中為ON莹桅,在多主模式中為OFF
- 配置完成后, 將my.cnf文件拷貝到其他兩個(gè)節(jié)點(diǎn)昌执,并且依次重啟三個(gè)節(jié)點(diǎn)的數(shù)據(jù)庫(kù),安裝MGR插件,設(shè)置復(fù)制賬號(hào)(所有MGR節(jié)點(diǎn)都要執(zhí)行)
# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
[mysqld]
#
# Remove leading # and set to the amount of RAM for the most important data
# cache in MySQL. Start at 70% of total RAM for dedicated server, else 10%.
# innodb_buffer_pool_size = 128M
#
# Remove leading # to turn on a very important data integrity option: logging
# changes to the binary log between backups.
# log_bin
#
# Remove leading # to set options mainly useful for reporting servers.
# The server defaults are faster for transactions and fast SELECTs.
# Adjust sizes as needed, experiment to find the optimal values.
# join_buffer_size = 128M
# sort_buffer_size = 2M
# read_rnd_buffer_size = 2M
character-set-server = utf8
port = 3305
bind-address = node-1 #需提前在 /etc/hosts 增加 node-1記錄
basedir = /home/cluster/mysql
datadir = /home/cluster/mysql/data
socket = /home/cluster/mysql/mysql.sock
log_error=/home/cluster/mysql/error.log
pid-file=/home/cluster/mysql/mysql.pid
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links = 0
# Replication configuration parameters
server_id = 1
gtid_mode = ON
enforce_gtid_consistency = ON
log_bin = binlog
log_slave_updates = ON
binlog_format = ROW
binlog_checksum = NONE
master_info_repository=TABLE
relay_log_info_repository=TABLE
max_connections=200
max_allowed_packet=16M
#server必須為每個(gè)事務(wù)收集寫集合懂拾,并使用XXHASH64哈希算法將其編碼為散列
transaction_write_set_extraction=XXHASH64
#告知插件加入或創(chuàng)建組命名煤禽,UUID,集群中UUID一致
loose-group_replication_group_name="816dd734-5cfd-11ea-9ba1-fa163e443845"
#server啟動(dòng)時(shí)不自啟組復(fù)制,為了避免每次啟動(dòng)自動(dòng)引導(dǎo)具有相同名稱的第二個(gè)組,所以設(shè)置為OFF。
loose-group_replication_start_on_boot=off
#告訴插件使用IP地址岖赋,端口24901用于接收組中其他成員轉(zhuǎn)入連接
loose-group_replication_local_address= "node-1:3305"
#啟動(dòng)組server檬果,種子server,加入組應(yīng)該連接這些的ip和端口唐断;其他server要加入組得由組成員同意
loose-group_replication_group_seeds="node-1:3305,node-2:3305,node-3:3305"
loose-group_replication_ip_whitelist="node-1,node-2,node-3"
loose-group_replication_bootstrap_group = off
loose-group_replication_allow_local_disjoint_gtids_join = ON
# 使用MGR的單主模式
loose-group_replication_single_primary_mode = on
loose-group_replication_enforce_update_everywhere_checks = off
# 禁止其他引擎創(chuàng)建庫(kù)表
disabled_storage_engines = MyISAM,BLACKHOLE,FEDERATED,CSV,ARCHIVE
[client]
port=3305
default-character-set=utf8
socket=/home/cluster/mysql/mysql.sock
5.2.配置創(chuàng)建Innodb Cluster集群
- 持久化集群配置文件选脊,會(huì)自動(dòng)在指定的my.cnf文件中添加配置項(xiàng) (需要三個(gè)節(jié)點(diǎn)同樣操作)
mysqlsh
mysql-js> dba.configureLocalInstance('root@node-1:3305');
Please provide the password for 'root@node-1:3305':
#輸入密碼
Detecting the configuration file...
Found configuration file at standard location: /etc/my.cnf
Do you want to modify this file? [Y|n]: [Y|n]:
#由于配置時(shí)不是采用用默認(rèn)的/etc/my.cnf文件,此處選n栗涂,然后指定配置文件路徑
Default file not found at the standard locations.
Please specify the path to the MySQL configuration file:/home/cluster/mysql/my.cnf
Validating instance...
The instance 'node-2:3305' is valid for Cluster usage
You can now use it in an InnoDB Cluster.
{
"status": "ok"
}
mysql-js>
由上面的信息看出, status 為 ok 說明配置沒問題了知牌,檢查my.cnf配置文件是否發(fā)生變化,可以查看實(shí)例狀態(tài)
mysql-js > dba.checkInstanceConfiguration('root@node-1:3305');
- 在管理節(jié)點(diǎn)創(chuàng)建 cluster集群斤程,這里我采用第一個(gè)節(jié)點(diǎn)作為管理節(jié)點(diǎn)創(chuàng)建角寸,節(jié)點(diǎn):node-1(只在管理節(jié)點(diǎn)操作創(chuàng)建,窗口一致保持打開忿墅,添加節(jié)點(diǎn)時(shí)也需要用到)
mysqlsh
#連接
mysql-js > shell.connect('root@node-1:3305');
# 創(chuàng)建一個(gè) cluster扁藕,命名為 'myCluster'
mysql-js > var cluster = dba.createCluster('myCluster');
A new InnoDB cluster will be created on instance 'root@node-1:3305'.
Creating InnoDB cluster 'myCluster' on 'root@node-1:3305'...
Adding Seed Instance...
Cluster successfully created. Use Cluster.addInstance() to add MySQL instances.
At least 3 instances are needed for the cluster to be able to withstand up to
one server failure.
mysql-js>
# 創(chuàng)建成功后,查看cluster狀態(tài)疚脐,可以看到master已經(jīng)添加進(jìn)cluster
mysql-js> cluster.status();
{
"clusterName": "myCluster",
"defaultReplicaSet": {
"name": "default",
"primary": "node-1:3305",
"status": "OK_NO_TOLERANCE",
"statusText": "Cluster is NOT tolerant to any failures.",
"topology": {
"node-1:3305": {
"address": "node-1:3305",
"mode": "R/W",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
}
}
}
}
- 通過cluster狀態(tài)查看亿柑,可以看到master已經(jīng)添加進(jìn)cluster,現(xiàn)在添加slave從節(jié)點(diǎn) (在管理節(jié)點(diǎn)操作添加)
mysqlsh
#連接
mysql-js > shell.connect('root@node-1:3305');
# 添加實(shí)例
mysql-js> cluster.addInstance('root@node-2:3305');
# 如果 之前的管理終端關(guān)閉棍弄,重新打開望薄,執(zhí)行語句時(shí)會(huì)報(bào)錯(cuò):
TypeError: Cannot read property 'status' of undefined
#這時(shí)需要先執(zhí)行下面這條語句獲取后,再添加節(jié)點(diǎn)
mysql-js> cluster=dba.getCluster();
# 先添加node-2節(jié)點(diǎn)
mysql-js> cluster.addInstance('root@node-2:3305');
A new instance will be added to the InnoDB cluster. Depending on the amount of
data on the cluster this might take from a few seconds to several hours.
#輸入密碼
Please provide the password for 'root@node-2:3305':
Adding instance to the cluster ...
The instance 'root@node-2:3305' was successfully added to the cluster.
mysql-js>
# 再添加node-3節(jié)點(diǎn)
mysql-js> cluster.addInstance('root@node-3:3305');
A new instance will be added to the InnoDB cluster. Depending on the amount of
data on the cluster this might take from a few seconds to several hours.
Please provide the password for 'root@node-3:3305':
Adding instance to the cluster ...
The instance 'root@node-3:3305' was successfully added to the cluster.
mysql-js>
#成功后查看集群狀態(tài)
mysql-js> cluster.status();
{
"clusterName": "myCluster",
"defaultReplicaSet": {
"name": "default",
"primary": "node-1:3305",
"status": "OK",
"statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
"topology": {
"node-1:3305": {
"address": "node-1:3305",
"mode": "R/W",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"node-2:3305": {
"address": "node-2:3305",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
},
"node-3:3305": {
"address": "node-3:3305",
"mode": "R/O",
"readReplicas": {},
"role": "HA",
"status": "ONLINE"
}
}
}
}mysql-js>
通過上面cluster集群信息可知, node-1節(jié)點(diǎn)是主節(jié)點(diǎn), 具有R/W讀寫權(quán)限, 其他兩個(gè)節(jié)點(diǎn)是從節(jié)點(diǎn), 具有R/O 只讀權(quán)限。
5.3.在node-1和node-2分別安裝mysql-route
1.mysql-route介紹
- MySQL Router是處于應(yīng)用client和dbserver之間的輕量級(jí)代理程序呼畸,它能檢測(cè)痕支,分析和轉(zhuǎn)發(fā)查詢到后端數(shù)據(jù)庫(kù)實(shí)例,并把結(jié)果返回給client蛮原。是mysql-proxy的一個(gè)替代品卧须。
- Router實(shí)現(xiàn)讀寫分離,程序不是直接連接數(shù)據(jù)庫(kù)IP儒陨,而是固定連接到mysql router花嘶。MySQL Router對(duì)前端應(yīng)用是透明的。應(yīng)用程序把MySQL Router當(dāng)作是普通的mysql實(shí)例蹦漠,把查詢發(fā)給MySQL Router,而MySQL Router會(huì)把查詢結(jié)果返回給前端的應(yīng)用程序椭员。
- 從數(shù)據(jù)庫(kù)服務(wù)器故障,業(yè)務(wù)可以正常運(yùn)行笛园。由MySQL Router來進(jìn)行自動(dòng)下線不可用服務(wù)器拆撼。程序配置不需要任何修改容劳。
- 主數(shù)據(jù)庫(kù)故障,由MySQL Router來決定主從自動(dòng)切換闸度,業(yè)務(wù)可以正常訪問。程序配置不需要做任何修改蚜印。
- 安裝配置
# 解壓
tar -xzvf mysql-router-2.1.6-linux-glibc2.12-x86-64bit.tar.gz
mv mysql-router-2.1.6-linux-glibc2.12-x86-64bit mysql-router-init
修改用戶配置文件vi .bash_profile莺禁,內(nèi)容如下:
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export PATH=$PATH:/home/cluster/mysql/bin
export PATH=$PATH:/home/cluster/mysql-shell/bin
export PATH=$PATH:/home/cluster/mysql-router-init/bin
source .bash_profile
mysqlprovision --version
mysqlprovision version 2.1.0
- 配置MySQL router
mysqlrouter --bootstrap root@node-1:3305 -d /home/cluster/mysql-router --conf-base-port 23306
# 選項(xiàng)說明參考 https://dev.mysql.com/doc/mysql-router/8.0/en/mysqlrouter.html
Please enter MySQL password for root:
Bootstrapping MySQL Router instance at /home/cluster/mysql-router...
MySQL Router has now been configured for the InnoDB cluster 'myCluster'.
The following connection information can be used to connect to the cluster.
Classic MySQL protocol connections to cluster 'myCluster':
- Read/Write Connections: localhost:23306 #讀寫端口
- Read/Only Connections: localhost:23307 #只讀端口
X protocol connections to cluster 'myCluster':
- Read/Write Connections: localhost:23308
- Read/Only Connections: localhost:23309
會(huì)自動(dòng)在/home/cluster/mysql-router目錄下生成配置文件及啟動(dòng)腳本
進(jìn)入 mysql-router 目錄,配置并啟動(dòng)(可以修改配置文件, 也可以默認(rèn)不修改)
#啟動(dòng)
/home/cluster/mysql-router/start.sh
PID 16819 written to /home/cluster/mysql-router/mysqlrouter.pid
#默認(rèn)通過route連接mysql后, 23306端口連接后可以進(jìn)行讀寫操作. 23307端口連接后只能進(jìn)行只讀操作.
$ netstat -nltp | grep 2330
tcp 0 0 0.0.0.0:23306 0.0.0.0:* LISTEN off (0.00/0/0)
tcp 0 0 0.0.0.0:23307 0.0.0.0:* LISTEN off (0.00/0/0)
tcp 0 0 0.0.0.0:23308 0.0.0.0:* LISTEN off (0.00/0/0)
tcp 0 0 0.0.0.0:23309 0.0.0.0:* LISTEN off (0.00/0/0)
這樣就可以使用MySQL客戶端連接router了. 下面驗(yàn)證下連接router:
- 管理MySQL Router
a) 管理節(jié)點(diǎn)本機(jī)mysql-shell連接:
[cluster@node-1 ~]$ mysqlsh --uri root@node-1:23306
b) 管理節(jié)點(diǎn)本機(jī)mysql連接:
[cluster@node-2 ~]$ mysql -u root -h node-1 -P 23306 -p
c) 遠(yuǎn)程客戶機(jī)通過route連接mysql
[root@db-node01 ~]# mysql -u root -h 127.0.0.1 -P 23306 -p
此外, 還可以利用keepalived實(shí)現(xiàn)InnoDB Cluster的高可用, 即兩臺(tái)db-route管理節(jié)點(diǎn), 通過VIP資源實(shí)現(xiàn)故障無感知切換.