一、規划
系統 | 主機名 | IP | 軟件 | server-id |
---|---|---|---|---|
CentOS8 | 192-168-1-116 | 192.168.1.116 | MySQL-5.7.30 | 0 |
CentOS8 | 192-168-1-117 | 192.168.1.117 | MySQL-5.7.30 | 1 |
CentOS8 | 192-168-1-118 | 192.168.1.118 | MySQL-5.7.30 | 2 |
二、InnoDB Cluster部署
1.MySQL安裝---所有節點
#關閉系統源mysql
dnf -y module disable mysql
#如非CentOS8以上版本,需要自己安裝mysql源
rpm -ivh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
#修改為國內源
sed -i 's#repo.mysql.com/yum#mirrors.tuna.tsinghua.edu.cn/mysql/yum#; s/mysql-\([0-9]\)\.\([0-9]\)/mysql\1\2/; s#/el/\([0-9]\)/#-el\1/#; s#$basearch/##' /etc/yum.repos.d/mysql-community.repo
#選擇MySQL版本
#查看所有子庫和狀態
#yum repolist all | grep mysql
#可選禁用8.0,啟用5.7版本
yum -y install yum-utils
yum-config-manager --disable mysql80-community
yum-config-manager --enable mysql57-community
yum -y remove mysql-community-common-8.0.19-1.el7.x86_64
#安裝MySQL
yum -y install mysql-community-server
2.初始化密碼---所有節點
#啟動
systemctl restart mysqld
#查看默認密碼,CentOS8使用YUM安裝默認無密碼
grep "password" /var/log/mysqld.log
#若使用簡單密碼,修改密碼策略
#初始化數據庫,重置密碼
mysql_secure_installation
3.滿足集群條件
1.InnoDB Cluster節點配置---所有節點,對應改變標識符和IP
# 禁用其他引擎
disabled_storage_engines="MyISAM,BLACKHOLE,FEDERATED,ARCHIVE,MEMORY"
# 開啟二進制日志記錄
log_bin=binlog
# 級聯復制
log_slave_updates=ON
# 以行的格式記錄
binlog_format=ROW
# 禁用二進制日志事件校驗
binlog_checksum=NONE
# 開啟全局事務
gtid_mode=ON
# 強制GTID的一致性
enforce_gtid_consistency=ON
# 將master.info元數據保存在系統表中
master_info_repository=TABLE
# 將relay.info元數據保存在系統表中
relay_log_info_repository=TABLE
# 事務寫集提取,使用哈希算法將其編碼為散列
transaction_write_set_extraction=XXHASH64
#小寫表格名稱
lower_case_table_names=1
#並行復制線程數,根據CPU核數設置
slave_parallel_workers=16
#對於多線程slaves,來保障事務在slave上執行的順序與relay log中的順序嚴格一致
slave_preserve_commit_order=1
slave_parallel_type=LOGICAL_CLOCK
# binlog最大大小,一般來說設置為512M或者1G,但不能超過1G
max_binlog_size=1G
# binlog緩存大小
binlog_cache_size=8m
# 最大binlog緩存大小
max_binlog_cache_size=256M
# 版本 8 后新增的(以秒為單位),8.0.4之前默認值為0,8.0.11之后為2592000也就是30天,8.0 以前的是 expire_logs_days(以天為單位),如果有了 binlog_expire_logs_seconds 不會再執行 expire_logs_days
#binlog_expire_logs_seconds=2592000
expire_logs_days=7
# 開啟慢查詢日志
slow_query_log=ON
# 慢查詢時間設置,當查詢時間多於設定的時間值時,記錄日志,以秒為單元
long_query_time=10
# 記錄沒有使用索引的查詢日志
log_queries_not_using_indexes=ON
#最大連接數,當max_connections設置太小時(默認151),MySQL可能會報錯Too many connections。當max_connections設置太大時(1000以上),操作系統可能忙於線程間的切換而失去響應。
#每個連接都會消耗一定內存,最大連接數占上限連接數(show global status like 'Max_used_connections';)的85%左右,如果發現比例在10%以下,MySQL服務器連接上線就設置得過高了。
#Max_used_connections / max_connections = 0.85
max_connections = 1024
# 啟動時加載group_replication
plugin_load_add='group_replication.so'
skip-name-resolve
# 禁用域名解析后需要給集群通知本機地址,如果沒有該值,使用 skip-name-resolve 不起作用,必須要在操作系統 hosts 中配置集群節點的 host 名稱才能識別每台服務器並加入集群
report-host='192.168.1.116'
# 標識符
server_id=1
# 加入的組名,可以修改,只要格式對,必須是有效的UUID。在二進制日志中為組復制事件設置GTID時在內部使用此UUID。您可以SELECT UUID()用來生成UUID。
group_replication_group_name="09b13906-9e35-11ea-8145-005056bb8174"
# 變量以off指示插件在服務器啟動時不自動啟動操作。這很重要,因為它可確保您可以在手動啟動插件之前配置服務器。配置成員后,可設為on以便在服務器啟動時自動啟動組復制。
group_replication_start_on_boot=off
# 設置成員用於與組中其他成員進行內部通信的網絡地址和端口。組復制將此地址用於涉及組通信引擎的遠程實例(XCom,Paxos的變體)的內部成員間連接。
group_replication_local_address= "192.168.1.116:33061"
# 所有組成員都必須解析由 配置的網絡地址 。
group_replication_group_seeds= "192.168.1.116:33061,192.168.1.117:33061,192.168.1.118:33061"
# 是否引導組。在這種情況下,即使s1是該組的第一個成員,我們也會在選項文件中將此變量設置為off。相反,我們配置實例運行的時間,以確保只有一個成員實際引導該組。
group_replication_bootstrap_group=off
# 單主模式
group_replication_single_primary_mode=on
init-connect = 'SET NAMES utf8mb4'
character-set-server = utf8mb4
2.創建復制賬號
1.所有節點
SET SQL_LOG_BIN=0;
CREATE USER 'repl'@'%' IDENTIFIED BY 'P@ssw0rd';
GRANT REPLICATION SLAVE ON *.* TO 'repl'@'%';
FLUSH PRIVILEGES;
SET SQL_LOG_BIN=1;
2.隨選主節點
CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='P@ssw0rd' FOR CHANNEL 'group_replication_recovery';
# 開啟組復制引導
SET GLOBAL group_replication_bootstrap_group=ON;
# 開啟組復制
START GROUP_REPLICATION;
# 關閉組復制引導
SET GLOBAL group_replication_bootstrap_group=OFF;
#查看組復制集群的服務器信息
SELECT * FROM performance_schema.replication_group_members;
3.其他節點
#這一步如果不是空庫,需要自己調
CHANGE MASTER TO MASTER_USER='repl', MASTER_PASSWORD='P@ssw0rd' FOR CHANNEL 'group_replication_recovery';
set global group_replication_allow_local_disjoint_gtids_join=ON;
# 開啟組復制
START GROUP_REPLICATION;
#查看組復制集群的服務器信息
SELECT * FROM performance_schema.replication_group_members;
4.創建集群
1.安裝mysql-shell
dnf -y install mysql-shell
2.創建集群管理賬號-主節點
CREATE USER 'cluster'@'%' IDENTIFIED BY 'P@ssw0rd';
GRANT ALL privileges ON *.* TO 'cluster'@'%' with grant option;
FLUSH PRIVILEGES;
3.創建集群
選擇一台節點制作就行,這里使用 192.168.1.116
mysqlsh --log-level=DEBUG3
mysqlsh內設置實例,需先關閉組復制
# 1.檢查每個節點是否可用加入集群的狀態,是否能夠加入,能夠加入會輸出 OK,如果已經加入會給出提示
dba.checkInstanceConfiguration('cluster@192.168.1.116:3306');
dba.checkInstanceConfiguration('cluster@192.168.1.117:3306');
dba.checkInstanceConfiguration('cluster@192.168.1.118:3306');
# 2.配置每個節點的實例入集群前的連接賬號
dba.configureLocalInstance('cluster@192.168.1.116');
dba.configureLocalInstance('cluster@192.168.1.117');
dba.configureLocalInstance('cluster@192.168.1.118');
連接到其中一個節點,准備創建集群,這里連接 192.168.1.116
shell.connect('cluster@192.168.1.116:3306')
#創建名稱為 KDCluster 的集群
var cluster = dba.createCluster('KDCluster')
#加入其它節點
cluster.addInstance('cluster@192.168.1.117:3306')
cluster.addInstance('cluster@192.168.1.118:3306')
#查看集群狀態
cluster.status()
持久化保存,每個節點執行
mysqlsh
進入后執行
dba.configureLocalInstance('cluster@192.168.1.116');
InnoDB Cluster集群運維命令
查看集群狀態
shell.connect('cluster@192.168.1.116:3306')
cluster=dba.getCluster();
日常使用的幾個重要命令
dba.checkInstanceConfiguration("root@hostname:3306") #檢查節點配置實例,用於加入cluster之前
dba.rebootClusterFromCompleteOutage('myCluster'); #重啟
dba.dropMetadataSchema(); #刪除schema
var cluster = dba.getCluster('myCluster') #獲取當前集群
cluster.checkInstanceState("root@hostname:3306") #檢查cluster里節點狀態
cluster.rejoinInstance("root@hostname:3306") #重新加入節點,我本地測試的時候發現rejoin一直無效,每次是delete后
addcluster.dissolve({force:true}) #刪除集群
cluster.addInstance("root@hostname:3306") #增加節點
cluster.removeInstance("root@hostname:3306") #刪除節點
cluster.removeInstance('root@host:3306',{force:true}) #強制刪除節點
cluster.dissolve({force:true}) #解散集群
cluster.describe(); #集群描述
集群節點狀態
- ONLINE: The instance is online and participating in the cluster.
- OFFLINE: The instance has lost connection to the other instances.
- RECOVERING: The instance is attempting to synchronize with the cluster by retrieving transactions it needs before it can become an ONLINE member.
- UNREACHABLE: The instance has lost communication with the cluster.
- ERROR: The instance has encountered an error during the recovery phase or while applying a transaction