MySQL InnoDB Cluster 配置

說明

  • MySQL的高可用架構不管是社區仍是官方,一直在技術上進行探索,這麼多年提出了多種解決方案,好比MMM, MHA, NDB Cluster, Galera Cluster, InnoDB Cluster, 騰訊的PhxSQL, MySQL Fabric ,aliSQL
  • NDB:基於集羣的引擎-數據被自動切分並複製到數個機器上(數據節點), 適合於那些須要極高查詢性能和高可用性的應用, 原來是爲愛立信的電信應用設計的。 NDB提供了高達99.999%的可靠性,在讀操做多的應用中表現優異。 對於有不少併發寫操做的應用, 仍是推薦用InnoDB
  • 本次部署採用InnoDB Cluster. 每臺服務器實例都運行MySQL Group Replication(冗餘複製機制,內置failover
  • MGR有兩種模式,一種是Single-Primary,一種是Multi-Primary,即單主或者多主。
  • 注意:Multi-Primary模式中,全部的節點都是主節點,均可以同時被讀寫,看上去這彷佛更好,可是由於多主的複雜性,在功能上若是設置了多主模式,則會有一些使用的限制,好比不支持Foreign Keys with Cascading Constraints。
    image.png

配置環境

  • 系統 Ubuntu 14.04
  • MySQL 8.0
  • 服務器(至少3臺):svr1, svr2, svr3

安裝

  1. 安裝源

deb下載頁面:dev.mysql.com/downloads/r…mysql

wget https://repo.mysql.com//mysql-apt-config_0.8.10-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.10-1_all.deb  # 選擇版本mysql-8.0
sudo apt-get update
複製代碼
  1. 安裝MySQL及組件
sudo apt-get install mysql-server mysql-shell mysql-router
複製代碼
  1. 添加MySQL用戶和權限 運行命令: mysql -u root -p 進入MySQL shell,建立用戶名和足夠的權限
# user,password替換爲本身的
mysql> CREATE USER 'user'@'%' IDENTIFIED BY 'password';用戶名和密碼
mysql> GRANT all privileges on *.* to user@'%' identified by 'password' with grant option;
mysql> flush privileges;
複製代碼

配置

  1. 配置文件 修改配置文件/etc/mysql/my.cnf,添加以下配置:
  • 修改不一樣機器的名稱或IP;
  • server_id使用不一樣編號;
  • loose-group_replication_group_name使用UUID形式,集羣中機器使用同一個UUID
  • loose-group_replication_single_primary_mode在單主模式中爲ON,在多主模式中爲OFF
[mysqld]
# server configuration
pid-file        = /var/run/mysqld/mysqld.pid
socket          = /var/run/mysqld/mysqld.sock
datadir         = /var/lib/mysql
log-error       = /var/log/mysql/error.log
bind-address    = 0.0.0.0
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links = 0
# Replication configuration parameters
server_id = 1 #2,3
gtid_mode = ON
enforce_gtid_consistency = ON
master_info_repository = TABLE
relay_log_info_repository = TABLE
transaction_write_set_extraction = XXHASH64
binlog_checksum = NONE
log_slave_updates = ON
log_bin = binlog
binlog_format = ROW
relay-log = svr-relay-bin
#
# Group Replication configuration
loose-group_replication_group_name = a38e32fd-5fb6-11e8-ad7a-00259015d941
loose-group_replication_start_on_boot = OFF
loose-group_replication_local_address = svr1:33061
loose-group_replication_group_seeds = svr1:33061,svr2:33061,svr3:3306
loose-group_replication_bootstrap_group = OFF
loose-group_replication_allow_local_disjoint_gtids_join = ON
# Group Replication configuration multi-primary mode
loose-group_replication_single_primary_mode = OFF
loose-group_replication_enforce_update_everywhere_checks = ON
複製代碼
  1. 檢查配置 運行命令進入mysql-js shell: sudo -i mysqlsh --uri=user@svr1:3306 對集羣中的機器執行如下命令,檢查配置: mysql-js> dba.checkInstanceConfiguration('user@svr1:3306') 經過下列命令查看全部服務器上group_replication配置是否一致: mysql> show variables like 'group_replication%';sql

  2. 建立集羣 若是須要建立單主模式集羣,執行第1句;若是須要建立多主模式集羣,執行第二句。shell

mysql-js> var cluster = dba.createCluster('mysqlCluster')  # 單主模式集羣
mysql-js> var cluster = dba.createCluster('mysqlCluster', {multiMaster:true})  # 多主模式集羣
mysql-js> cluster.addInstance('user@svr2:3306')
mysql-js> cluster.addInstance('user@svr3:3306')
複製代碼
  1. 查看狀態
mysql-js> var cluster = dba.getCluster()
mysql-js> cluster.status();
複製代碼

輸出如下內容則表示集羣創建起來了(當前輸出爲多主模式,單主模式相似):bootstrap

{
    "clusterName": "mysqlCluster",
    "defaultReplicaSet": {
        "name": "default",
        "status": "OK",
        "statusText": "Cluster is ONLINE and can tolerate up to ONE failure.",
        "topology": {
            "svr1:3306": {
                "address": "svr1:3306",
                "mode": "R/W",
                "readReplicas": {},
                "role": "HA",
                "status": "ONLINE"
            },
            "svr2:3306": {
                "address": "svr2:3306",
                "mode": "R/W",
                "readReplicas": {},
                "role": "HA",
                "status": "ONLINE"
            },
            "svr3:3306": {
                "address": "svr3:3306",
                "mode": "R/W",
                "readReplicas": {},
                "role": "HA",
                "status": "ONLINE"
            }
        }
    }
}
複製代碼

部署MySQL Router

客戶端不能直接鏈接服務器,須要經過 MySQL Router 來鏈接。 運行命令:sudo -i mysqlrouter --bootstrap user@svr1:3306 --user mysqlrouter 生成配置文件並運行MySQLRouter(經過keepalived的虛擬IP可配置高可用性router) 配置文件路徑:/etc/mysqlrouter/mysqlrouter.conf,可查看訪問接口 vim

innodb_cluster_overview.png

配置中問題記錄

問題1:[ERROR] Slave SQL for channel 'group_replication_recovery': Could not execute Write_rows event on table mysql_innodb_cluster_metadata.instances; Cannot add or update a child row: a foreign key constraint fails (mysql_innodb_cluster_metadata.instances, CONSTRAINT instances_ibfk_1 FOREIGN KEY (host_id) REFERENCES hosts (host_id)), Error_code: 1452; handler error HA_ERR_NO_REFERENCED_ROW; the event's master log binlog.000001, end_log_pos 3059, Error_code: 1452 解決方式:清空表mysql_innodb_cluster_metadata.hosts; 從新創建集羣bash

問題2:This member has more executed transactions than those present in the group 解決方式: mysql-> stop group_replication; mysql-> reset master;服務器

問題3:用戶操做系統資源的限制 [Warning] Buffered warning: Changed limits: max_open_files: 1024 (requested 5000) [Warning] Buffered warning: Changed limits: table_open_cache: 431 (requested 2000) 解決方式: $ vim /etc/security/limits.conf,添加: mysql soft nproc 2047 mysql hard nproc 16384 mysql soft nofile 1024 mysql hard nofile 65535session

問題4:dba.rebootClusterFromCompleteOutage: The active session instance isn't the most updated in comparison with the ONLINE instances of the Cluster's metadata. 在集羣沒有起來時某些機器的數據表發生變更,致使數據不一致; 解決方式: 全部MySQL機器經過reset master命令清空binlogs mysql> reset master; mysql> show master logs; 而後再運行Dba.rebootClusterFromCompleteOutage重啓集羣。架構

問題5:service mysql restart沒法重啓mysql,mysql stuck,並一直輸出日誌'[Note] Plugin group_replication reported: '[GCS] cli_err 2'' 解決方式:惟一中止MySQL的命令爲:sudo pkill -9 mysqld併發

問題6:如何將Multi-Primary改成Single-Primary? (1). 解散原來的集羣:mysql-js> cluster.dissolve({force: true}) (2). 每臺主機MySQL修改以下配置: mysql> set global group_replication_enforce_update_everywhere_checks=OFF; mysql> set global group_replication_single_primary_mode=ON; (3). 從新建立集羣: mysql-js> var cluster = dba.createCluster('mysqlCluster'); mysql-js> cluster.addInstance('user@svr2:3306'); mysql-js> cluster.addInstance('user@svr3:3306');

This member has more executed transactions than those present in the group

相關文章
相關標籤/搜索