美文网首页
Mysql Group Replication 集群搭建

Mysql Group Replication 集群搭建

作者: 斜不靠谱 | 来源:发表于2019-07-18 18:25 被阅读0次

    主节点和从节点共同配置

    1 修改配置文件

    
    gtid_mode=ON
    
    enforce_gtid_consistency=ON
    
    # binlog_checksum 默认为CRC32
    
    # 可以动态修改,但是在一个事务中无法修改
    
    # 为CRC32时,master节点会对每个event记录checksum值
    
    # 设置为NONE时,只会在binlog中记录event length(而不是checksum值)
    
    # 修改此参数会开启一个新的binlog文件,一个binlog文件中要么全都有checksums,要么全没有
    
    # 如果master中设置为其他不可识别的值,会导致slave节点设置binlog_checksum为None,并且会停止复制
    
    # 如果想跟以前版本兼容,则需要明确的设置为NONE
    
    binlog_checksum=NONE
    
    # 修改slave_preserve_commit_order
    
    # 官方文档没写,并且默认为0
    
    # 报错 ERROR 3092 (HY000): The server is not configured properly to be an active member of the group. Please see more details on error log.
    
    # 错误日志内容为 2017-12-18T15:43:28.816078+08:00 35 [Warning] Plugin group_replication reported: 'Group Replication requires slave-preserve-commit-order to be set to ON when using more than 1 applier threads.'
    
    slave_preserve_commit_order                = ON 
    
    transaction_write_set_extraction=XXHASH64
    
    # 可以修改名称必须符合uuid格式 loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa" loose-group_replication_start_on_boot=off # server启动时不会自动开启插件 loose-group_replication_local_address= "10.86.43.238:24901"
    
    #集群中可以作为seed节点的列表,此参数不需要列出所有节点,只需要列出希望作为seed的节点即可 
    
    # 如果多个节点同时加入group(不推荐,可能会超时失败),要确保新加如节点的seed节点是已经在group中的,而不是也正在join
    
    loose-group_replication_group_seeds= "10.86.43.239:24901,10.86.43.238:24901,10.86.43.209:24901"
    
    loose-group_replication_bootstrap_group= off # 标记是否为bootstrap,一个集群只能有一个节点设置为on,否则会造成人工脑裂,而且bootstrap节点启动集群建立后,应该修改为off
    
    # mgr默认通过机器名连接, 如果要使用ip需要在my.cnf中配置report-host为本机ip
    report-host=10.86.43.239
    
    

    2 配置复制权限

    节点通过异步复制同步数据,通过名为 group_replication_recovery 的 replication channel 进行数据恢复。

    因此需要设置一个账号权限,用于节点间复制。

    
    set sql_log_bin=0;
    
     CREATE USER 'replication'@'%' IDENTIFIED BY '!4dkaRMz5';
    
     GRANT REPLICATION SLAVE ON *.* TO 'replication'@'%';
    
     FLUSH PRIVILEGES;
    
     SET SQL_LOG_BIN=1;
    
    # 创建好账号后,就可以指定 group_replication_recovery 作为master,以便下次启动时可以从集群其他节点恢复数据
    
     CHANGE MASTER TO MASTER_USER='replication', MASTER_PASSWORD='!4dkaRMz5' FOR CHANNEL 'group_replication_recovery';
    
    # Distributed recovery是节点加入集群的第一步,如果权限认证不通过,则无法加入集群,类似的如果无法识别hostname也会造成recovery process fail
    
    #如果多个组成员将一个默认的主机名外部化到操作系统所设置的缺省主机名,则该成员有可能无法解析到正确的成员地址,而不能加入该组。在这种情况下,使用report_host配置由每个服务器外部化的唯一主机名。
    
    

    3 安装插件

    
    mysql> INSTALL PLUGIN group_replication SONAME 'group_replication.so';
    
    # [https://dev.mysql.com/doc/refman/5.7/en/group-replication-launching.html](https://dev.mysql.com/doc/refman/5.7/en/group-replication-launching.html) (注意下)
    
    The `mysql.session` user must exist before you can load Group Replication. `mysql.session` was added in MySQL version 5.7.19\. If your data dictionary was initialized using an earlier version you must run the [**mysql_upgrade**](https://dev.mysql.com/doc/refman/5.7/en/mysql-upgrade.html "4.4.7 mysql_upgrade — Check and Upgrade MySQL Tables") procedure. If the upgrade is not run, Group Replication fails to start with the error message There was an error when trying to access the server with user: mysql.session@localhost. Make sure the user is present in the server and that mysql_upgrade was ran after a server update..
    
    因为公司打的MySQL包不包含mysql.session用户,可以升级添加
    
    /home/q/mysql/cellar/mysql57/bin/mysql_upgrade -h127.0.0.1 -uroot  -P port
    
    也可以手动创建mysql.session用户
    
    GRANT SUPER ON *.* TO 'mysql.session'@'localhost' identified by password '*THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE'; 
    
    

    只在启动集群起始节点执行

    
     SET GLOBAL group_replication_bootstrap_group=ON;
    
     START GROUP_REPLICATION;
    
     SET GLOBAL group_replication_bootstrap_group=OFF;
    
    # 查看集群节点信息
    
    SELECT  *  FROM performance_schema.replication_group_members;
    
    

    只在增加节点到集群时执行

    
    mysql>  START  GROUP_REPLICATION;
    
    # 查看集群节点信息
    
    SELECT  *  FROM performance_schema.replication_group_members;
    
    # 节点加入gourp后,查看super_read_only
    
    

    注意

    • 因为group_replication_ip_whitelist默认只允许统一网段机器,当搭建跨级房集群是需要配置此参数(joiner和donor都要配置才行)

    • 如果需要添加whitelist,只需要先停止同步,然后设置whitelist即可

    stop group_replication;
     set global group_replication_ip_whitelist = '10.0.0.0/24,';
     start group_replication;
    
    • 需要配置机器名对应ip,mgr默认通过机器名连接, 或者在my.cnf中配置report-host为本机ip

    相关文章

      网友评论

          本文标题:Mysql Group Replication 集群搭建

          本文链接:https://www.haomeiwen.com/subject/ejrrlctx.html