zoukankan      html  css  js  c++  java
  • Centos7.5基于MySQL5.7的InnoDB Cluster多节点集群环境部署

    Centos7.5基于MySQL5.7的InnoDB Cluster多节点集群环境部署
    一.MySQL InnoDB Cluster 介绍
    MySQL的高可用架构解决方案,比如MMM, MHA, NDB Cluster, Galera Cluster, InnoDB Cluster, 腾讯的PhxSQL, MySQL Fabric, aliSQL。MySQL官方在2017年4月推出了MySQL InnoDB Cluster。
    在默认的单主节点模式下,集群服务器具有一个读写主节点和多个只读辅节点。辅助服务器是主服务器的副本。客户端应用程序通过MySQL Router连接到主服务程序。
    如果主服务连接失败,则次要的节点自动提升为主节点,MySQL Router请求到新的主节点。InnoDB Cluster不提供NDB Cluster支持。
    ->分布式MySQL之InnoDB和NDB
    分布式MySQL主要有InnoDB和NDB模式, NDB是基于集群的引擎-数据被自动切分并复制到数个机器上(数据节点), 适合于那些需要极高查询性能和高可用性的应用, 原来是为爱立信的电信应用设计的。
    NDB提供了高达99.999%的可靠性,在读操作多的应用中表现优异。 对于有很多并发写操作的应用, 还是推荐用InnoDB。

    二. Centos7.5基于MySQL5.7的InnoDB Cluster高可用环境部署
    下面部署采用InnoDB Cluster, 每台服务器实例都运行MySQL Group Replication (即冗余复制机制,内置failover), MGR有两种模式,一种是Single-Primary,一种是Multi-Primary,即单主或者多主。
    需求注意:模式Multi-Primary中,所有的节点都是主节点,都可以同时被读写,在功能上如果设置了多主模式,则会有一些使用的限制,比如不支持Foreign Keys with Cascading Constraints。
    #软件包下载
    mysql-shell下载地址: https://pan.baidu.com/s/1nPWcmKb2T_iDmpQ84ZrVmQ 提取密码: u425
    mysql-route下载地址: https://pan.baidu.com/s/1Tb7lxnxyiFdwxkdKOlU29Q 提取密码: sq6h

    2.1 环境准备
    3台centos7.3版本的服务器用来部署innodb cluster多节点集群环境 (至少也要需要3台服务器),
    1) node1、node2、node3 作为 cluster 节点服务器, 三个节点都要安装 mysql与mysql-shell
    2) route1 作为管理节点服务器,用来负责创建 cluster,并作为 cluster 的路由, 该节点需要安装mysql-shell、mysql-router,与node1在同一个服务器
    3) 所有节点的python版本要在2.7以上,centos已经自带python2.7

    ip地址 主机名 角色 安装软件
    192.168.92.101 node1 cluster节点1 Mysql5.7, mysql-shell
    192.168.92.102 node2 cluster节点2 Mysql5.7, mysql-shell
    192.168.92.103 node3 cluster节点3 Mysql5.7, mysql-shell
    192.168.92.104 route1 管理节点1 mysql-shell, mysql-route

    cat /etc/redhat-release
    python -V
    systemctl stop firewalld
    firewall-cmd --state

    #关闭每个节点的selinux
    cat /etc/sysconfig/selinux |grep "SELINUX=disabled"
    setenforce 0
    getenforce

    #配置每个节点的/etc/hosts主机映射, 方便通过节点的hostname进行连接
    vim /etc/hosts

    192.168.92.101 node1
    192.168.92.102 node2
    192.168.92.103 node3
    192.168.92.101 route1

    #三台服务器设置免密,设置node1到其他两台机器的免密登录
    ssh-keygen -t rsa
    ssh-copy-id node1
    ssh-copy-id node2
    ssh-copy-id node3
    #主节点查看ssh-keygen是否成功
    ssh node1 date
    ssh node2 date
    ssh node3 date

    #所有节点进行如下的相关优化配置
    cat>>/etc/sysctl.conf <<EOF
    fs.aio-max-nr = 1048576
    fs.file-max = 681574400
    kernel.shmmax = 137438953472
    kernel.shmmni = 4096
    kernel.sem = 250 32000 100 200
    net.ipv4.ip_local_port_range = 9000 65000
    net.core.rmem_default = 262144
    net.core.rmem_max = 4194304
    net.core.wmem_default = 262144
    net.core.wmem_max = 1048586
    EOF

    sysctl -p

    cat>>/etc/security/limits.conf <<EOF
    mysql soft nproc 65536
    mysql hard nproc 65536
    mysql soft nofile 65536
    mysql hard nofile 65536
    EOF

    cat>>/etc/pam.d/login <<EOF
    session required /lib/security/pam_limits.so
    session required pam_limits.so
    EOF

    cat>>/etc/profile<<EOF
    if [ $USER = "mysql" ]; then
    ulimit -u 16384 -n 65536
    fi
    EOF

    source /etc/profile

    #2.2在管理节点安装mysql shell 和 mysql-route
    tar -zvxf mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz
    tar -zvxf mysql-router-2.1.4-linux-glibc2.12-x86-64bit.tar.gz

    mv mysql-shell-1.0.9-linux-glibc2.12-x86-64bit mysql-shell
    mv mysql-router-2.1.4-linux-glibc2.12-x86-64bit mysql-route
    mv mysql-shell /usr/local/
    mv mysql-route /usr/local/

    vim /etc/profile

    export PATH=$PATH:/usr/local/mysql-shell/bin/:/usr/local/mysql-route/bin/

    source /etc/profile

    #mysqlprovision version 2.0.0
    mysqlprovision --version

    #MySQL Shell Version 1.0.9
    mysqlsh --version

    #MySQL Router v2.1.4 on Linux (64-bit) (GPL community edition)
    mysqlrouter --version

    #2.3在三个cluster节点安装和部署Mysql5.7及 mysql-shell
    #1) 安装mysql-shell (三个节点同样操作)
    tar -zvxf mysql-shell-1.0.9-linux-glibc2.12-x86-64bit.tar.gz
    mv mysql-shell-1.0.9-linux-glibc2.12-x86-64bit mysql-shell
    mv mysql-shell /usr/local/

    echo "export PATH=$PATH:/usr/local/mysql-shell/bin/" >> /etc/profile
    source /etc/profile

    #mysqlprovision version 2.0.0
    mysqlprovision --version

    #MySQL Shell Version 1.0.9
    mysqlsh --version

    #2) 安装mysql5.7 (三个节点同样操作)
    #使用yum方式安装Mysql5.7,参考:https://www.cnblogs.com/kevingrace/p/8340690.html
    #安装MySQL yum资源库
    yum localinstall https://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm

    #安装MySQL 5.7
    yum install -y mysql-community-server

    #启动MySQL服务器和MySQL的自动启动
    systemctl start mysqld.service
    systemctl enable mysqld.service

    #设置登录密码,由于MySQL从5.7开始不允许首次安装后使用空密码进行登录
    cat /var/log/mysqld.log|grep 'A temporary password'

    #输入上面查询到的密码
    mysql -p
    set global validate_password_policy=0;
    set global validate_password_length=1;
    set password=password("123456");
    flush privileges;
    #查看mysql版本:5.7.24
    select version();

    #3) 配置my.cnf
    #先配置node1节点的my.cnf
    cp /etc/my.cnf /etc/my.cnf.bak
    vim /etc/my.cnf

    [mysqld]
    datadir = /var/lib/mysql
    socket = /var/lib/mysql/mysql.sock
    symbolic-links = 0
    log-error = /var/log/mysqld.log
    pid-file = /var/run/mysqld/mysqld.pid
    #复制框架,添加如下部分
    server_id=1
    gtid_mode=ON
    enforce_gtid_consistency=ON
    binlog_checksum=NONE
    log_bin=binlog
    log_slave_updates=ON
    binlog_format=ROW
    master_info_repository=TABLE
    relay_log_info_repository=TABLE
    #组复制设置
    #server必须为每个事务收集写集合,并使用XXHASH64哈希算法将其编码为散列
    transaction_write_set_extraction=XXHASH64
    #告知插件加入或创建组命名,UUID
    loose-group_replication_group_name="aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
    #server启动时不自启组复制,为了避免每次启动自动引导具有相同名称的第二个组,所以设置为OFF。
    loose-group_replication_start_on_boot=off
    #告诉插件使用IP地址,端口24901用于接收组中其他成员转入连接
    loose-group_replication_local_address="192.168.92.101:24901"
    #启动组server,种子server,加入组应该连接这些的ip和端口;其他server要加入组得由组成员同意
    loose-group_replication_group_seeds="192.168.92.101:24901,192.168.92.102:24901,192.168.92.103:24901"
    loose-group_replication_bootstrap_group=off
    # 使用MGR的单主模式
    loose-group_replication_single_primary_mode = on
    disabled_storage_engines = MyISAM,BLACKHOLE,FEDERATED,CSV,ARCHIVE
    report_port=3306

    #如上配置完成后, 将node1节点的/etc/my.cnf文件拷贝到其他两个节点
    rsync -e "ssh -p22" -avpgolr /etc/my.cnf root@192.168.92.102:/etc/
    rsync -e "ssh -p22" -avpgolr /etc/my.cnf root@192.168.92.103:/etc/

    #3个cluster节点除了 server_id loose-group_replication_local_address 两个参数不一样外,其他保持一致。

    #配置完成后, 要依次重启三个节点的数据库,安装MGR插件,设置复制账号(所有MGR节点都要执行)
    systemctl restart mysqld
    mysql -uroot -p123456
    show variables like '%server%';

    $2.4 创建Innodb Cluster集群
    #1) 在 node1 上创建集群,通过 node1 上的 shell 连接node1 的 mysql
    mysqlsh
    shell.connect('root@localhost:3306');

    #输入密码123456
    #直接回车, 使用默认的/etc/my.cnf配置文件
    # 接下来需要创建供其他主机访问的用户,这里选择第1项,为root用户授权, 创建供其他主机访问的用户
    #输入供其他主机访问的用户root用户授权的密码. 这里依然设置123456
    dba.configureLocalInstance();

    #Dba.configureLocalInstance: Your password does not satisfy the current policy requirements (MySQL Error 1819)
    #解决,另一个终端窗口, 登录本节点执行sql,然后重新执行configureLocalInstance即可:
    mysql -p123456
    set global validate_password_policy=0;
    set global validate_password_length=1;

    #2)通过 route1 的 mysql-shell 连接 node01 创建 cluster
    mysqlsh
    shell.connect('root@node1:3306');
    var cluster = dba.createCluster('myCluster');
    #创建成功后,查看cluster状态
    cluster.status();
    dba.getCluster();

    #注意上面这个route1的mysql-shell终端窗口就不要关闭了, 一直保持连接中,也就是一直在当前集群状态中

    #3) 添加节点 node2到上面创建的"myCluster"集群中
    #通过node2本机 mysql-shell 对 mysql 进行配置
    mysqlsh
    shell.connect('root@localhost:3306');
    dba.configureLocalInstance();

    #报错解决,参考node1节点即可:

    #然后登录node2节点的mysql, 发现上面使用root用户远程连接的授权已经有了
    mysql -p123456
    select host,user from mysql.user;

    #接着修改 my.cnf,添加配置项:
    vim /etc/my.cnf

    loose-group_replication_allow_local_disjoint_gtids_join=on

    #重启mysql服务
    systemctl restart mysqld

    #然后通过 route1节点 的 mysql-shell 添加 node2 到 "myCluster"集群中
    #接着上面的route1的mysql-shell终端窗口 (注意这个终端窗口是上面执行后, 没有关闭一直开着的)
    cluster.addInstance('root@node2:3306');
    #上面信息表示node2节点已经成功添加到"myCluster"集群中了. 如下查看集群状态
    cluster.status();

    #同样, 上面操作后, 这个route1节点的mysql-shell当前终端窗口不要关闭,继续保持在集群状态中,
    #下面添加node3节点到集群中会用到这里.(后面常用命令中会提到)

    #4) 添加节点 node3到上面创建的"myCluster"集群中
    #登录node3节点的mysql-shell, 进行配置
    mysqlsh
    shell.connect('root@localhost:3306');
    dba.configureLocalInstance();

    #报错解决,参考node1节点即可:

    #接着修改 my.cnf,添加配置项:
    vim /etc/my.cnf

    loose-group_replication_allow_local_disjoint_gtids_join=on

    #重启mysql服务
    systemctl restart mysqld

    #然后通过 route1节点的mysql-shell添加node03到"myCluster"集群中
    #接着上面的route1的mysql-shell终端窗口 (注意这个终端窗口是上面执行后, 没有关闭一直开着的)
    cluster.addInstance('root@node3:3306');
    # node1节点是主节点, 具有R/W读写权限, 其他两个节点是从节点,具有R/O 只读权限
    cluster.status();

    ----------------------------------------------------------------------------------------------------
    #系统重启后需要重启集群 q推出mysql shell
    mysqlsh
    #用localhost连接无法重启集群
    shell.connect('root@node1:3306');
    dba.rebootClusterFromCompleteOutage('myCluster')
    #ReferenceError: cluster is not defined,需要先执行cluster=dba.getCluster();
    #断开会话后需要执行
    cluster=dba.getCluster();
    cluster.status();
    #从库查看读写模式
    mysql> show global variables like "%read_only%";

    innodb_read_only OFF
    read_only ON
    super_read_only ON
    transaction_read_only OFF
    tx_read_only OFF
    #确定是否已开启单主节点组复制
    show variables like '%group_replication_single_primary_mode%';

  • 相关阅读:
    数据库创建标量值函数
    大并发大数据量请求的处理方法
    模态对话框 bootstrap-modal.js
    创建测试表,批量插入数据的存储过程,分页存储过程
    android:强大的图片下载和缓存库Picasso
    压缩图片链接
    Css3:transform变形
    css中 padding属性的数值赋予顺序为
    Css3颜色值RGBA得表示方式
    css3 box-shadow让我们实现图层阴影效果
  • 原文地址:https://www.cnblogs.com/buffercache/p/13159091.html
Copyright © 2011-2022 走看看