• MySQL 5.7 InnoDB Cluster 部署


    一、规划

    系统 主机名 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])/mysql12/; s#/el/([0-9])/#-el1/#; 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
    
    
  • 相关阅读:
    实验5 编写调试有多个段的程序
    实验四 [bx]和 loop 的使用
    实验三
    实验二
    第一章
    汇编语言第二章知识梳理
    实验一:查看CPU和内存,用机器指令和汇编指令编程
    实验9
    实验5
    实验4:
  • 原文地址:https://www.cnblogs.com/bfbz/p/13025707.html
Copyright © 2020-2023  润新知