MySQL innodb cluster是MySQL Enterprise的HA機制,是以MySQL Group Replication搭配上MySQL Shell與MySQL Router組合而成,至少要有三台伺服器才能建立此架構,三台MySQL資料將會同步,若主要讀寫的instance故障,MySQL Router發現狀況,會自動將其他instance升為主要instance。
以下為官方提供架構圖。
開始建置前必須先建立好三台MySQL伺服器、一台MySQL Router伺服器。
Host | IP |
---|---|
Innodb-node1 | 172.31.31.135 |
Innodb-node2 | 172.31.31.126 |
Innodb-node3 | 172.31.31.138 |
建立完先在每個node配置host,否則可能會無法同步。
這邊我會設定虛擬host以免之後hostname被更改
vim /etc/hosts
開始設置innodb cluster
vim /etc/my.cnf
[mysqld]
server-id=1 #節點不可重複
log-bin=/mysql/binlog/mybin
relay-log=/mysql/relaylog/relay-log
gtid_mode=on
binlog_checksum=NONE
enforce_gtid_consistency=on
relay_log_info_repository=table
master_info_repository=table
binlog_format=row
log_slave_updates=on
plugin-dir= /usr/lib64/mysql/plugin
plugin-load=group_replication.so
transaction-write-set-extraction=XXHASH64
loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
loose-group_replication_start_on_boot=off
loose-group_replication_local_address="172.31.31.135:13306"
loose-group_replication_group_seeds="172.31.31.135:13306,172.31.31.126:13306,172.31.31.138:13306"
loose-group_replication_bootstrap_group=off
loose-group_replication_ip_whitelist="172.31.31.135,172.31.31.126,172.31.31.138"
建立binlog & relaylog 並更改owner與group
mkdir /mysql/binlog
mkdir /mysql/relaylog
chown -R mysql.mysql /mysql
重新啟動MySQL服務(各個node都要)
systemctl restart mysqld
安裝mysqlshell
yum localinstall -y mysql-shell-8.0.18-1.el7.x86_64.rpm
安裝完成後開始建立innodb cluster用戶(所有node需執行)
mysqlsh
mysql -js > shell.connect('root@localhost:3306') #以root登入
mysql-js> dba. configureLocalInstance() #建立帳戶
選(2) 建立新user
Account Name: admin@%
登入innodb 帳戶(admin),建立cluster
mysql-js > shell.connect('admin@node1')
mysql-js > var cluster=dba.createCluster('mycluster')
添加節點(node2、node3)
mysql-js> cluster.addInstance('admin@node2:3306')
mysql-js> cluster.addInstance('admin@node3:3306')
查看cluster狀況
mysql-js> cluster.status()
主要instance在innodb-node1。
建立MySQL Router
配置host
vim /etc/hosts
172.31.31.135 innodb-node1 node1
172.31.31.126 innodb-node2 node2
172.31.31.138 innodb-node3 node3
安裝mysql router
yum localinstall mysql-router-commercial-8.0.18-1.1.el7.x86_64.rpm
設定MySQL Router Config
mysqlrouter –bootstrap admin@node1:3306 --user=mysqlrouter
啟動MySQL Router
systemctl start mysqlrouter.service
查看MySQL Router config
cat /etc/mysqlrouter/mysqlrouter.conf
若要登入primary使用6446 port
若要登入secondary使用6447 port
Innodb Cluster建置完成!