• 【 InnoDB Cluster 】安装部署 MySQL Router


    安装部署 MySQL Router

    在之前的文章中,我们使用 MySQL Shell 搭建了一个最简单的三节点 InnoDB Cluster ,虽然它利用组复制可以提供高可用、单节点容错功能、故障转移,却并未提供诸如会话重定向、负载均衡等路由器相关功能,而这些功能是通过使用 MySQL Router 来实现的。

    有关 MySQL Router 的更多详细信息,可以参阅我持续更新的 《 MySQL Router 8.0 》专栏 ,也可以参阅官方 《 MySQL Router 8.0 》文档

    https://blog.csdn.net/wudi53433927/article/details/128138647

    安装 MySQL Router 8.0

    本文仅介绍在 Linux 操作系统上使用 Yum 仓库包安装 MySQL Router 。

    更多有关 MySQL Router 8.0 安装的详细信息,请参阅 2.1 在 Linux 上安装 MySQL Router

    安装过程:

    [root@ic-source ~]# yum install -y mysql-router-community.x86_64
    已加载插件:fastestmirror
    Repository base is listed more than once in the configuration
    Loading mirror speeds from cached hostfile
     * centos-sclo-rh: ftp.sjtu.edu.cn
    base                                                                                                            | 3.6 kB  00:00:00     
    centos-sclo-rh                                                                                                  | 3.0 kB  00:00:00     
    extras                                                                                                          | 2.9 kB  00:00:00     
    mysql-connectors-community                                                                                      | 2.6 kB  00:00:00     
    mysql-tools-community                                                                                           | 2.6 kB  00:00:00     
    mysql80-community                                                                                               | 2.6 kB  00:00:00     
    updates                                                                                                         | 2.9 kB  00:00:00     
    正在解决依赖关系
    --> 正在检查事务
    ---> 软件包 mysql-router-community.x86_64.0.8.0.31-1.el7 将被 安装
    --> 解决依赖关系完成
    
    依赖关系解决
    
    =======================================================================================================================================
     Package                               架构                  版本                           源                                    大小
    =======================================================================================================================================
    正在安装:
     mysql-router-community                x86_64                8.0.31-1.el7                   mysql-tools-community                4.7 M
    
    事务概要
    =======================================================================================================================================
    安装  1 软件包
    
    总下载量:4.7 M
    安装大小:21 M
    Downloading packages:
    mysql-router-community-8.0.31-1.el7.x86_64.rpm                                                                  | 4.7 MB  00:00:01     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      正在安装    : mysql-router-community-8.0.31-1.el7.x86_64                                                                         1/1 
      验证中      : mysql-router-community-8.0.31-1.el7.x86_64                                                                         1/1 
    
    已安装:
      mysql-router-community.x86_64 0:8.0.31-1.el7                                                                                         
    
    完毕!
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    注意
    检查 MySQL 提供的 Yum 源时发现有两个可用安装包,请选择最新的那个 mysql-router-community 社区版本安装。

    [root@ic-source ~]# yum list mysql-router*
    已加载插件:fastestmirror
    Repository base is listed more than once in the configuration
    Loading mirror speeds from cached hostfile
    * centos-sclo-rh: ftp.sjtu.edu.cn
    已安装的软件包
    mysql-router-community.x86_64                                    8.0.31-1.el7                                    >@mysql-tools-community
    可安装的软件包
    mysql-router.x86_64                                              8.0.12-1.el7                                    mysql->tools-community 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    部署 MySQL Router 8.0

    我在部署时参考了 6.10 Using MySQL Router with AdminAPI, InnoDB Cluster, and InnoDB ReplicaSet 内容,选择不创建自包含目录的方式引导 MySQL Router 。

    使用setupRouterAccount(user, [options]) 操作创建 MySQL 用户帐户或升级现有帐户,以便 MySQL Router 可以使用该帐户在 InnoDB Cluster 或 InnoDB ReplicaSet 上运行。这是使用 InnoDB Cluster 和 InnoDB ReplicaSet 配置 MySQL Router 的推荐方法。

    此方法要求使用 MySQL Shell 8.0.20 及更高版本。

    创建 MySQL Router 账户

    执行如下命令,向 cluster 变量所引用的 InnoDB Cluster 中添加名为 myRouter1 的 MySQL Router 账户并为其设置密码。

     MySQL  ic-source:33060+ ssl  JS > var cluster=dba.getCluster()
     MySQL  ic-source:33060+ ssl  JS > cluster.setupRouterAccount('myRouter1')
    
    Missing the password for new account myRouter1@%. Please provide one.
    Password for new account: ************
    Confirm password: ************
    
    Creating user myRouter1@%.
    Account myRouter1@% was successfully created.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在本例中,没有指定域,因此是使用通配符(%)创建帐户,这确保创建的用户可以从任何域连接。

    引导 MySQL Router

    要基于 InnoDB Cluster 或 InnoDB ReplicaSet 引导MySQL Router,需要连接到在线实例的 URI 类型连接(字符)串。使用--bootstrap=instance运行mysqlrouter命令(instance 是连接到在线实例的 URI 类型连接串)。 MySQL Router 连接到实例,并使用包含的元数据缓存插件来检索元数据,包括服务器实例地址及其角色的列表。

    注意,下例中第一次使用操作系统root账户引导时因未指定--user=root选项而报错。

    [root@ic-source ~]# mysqlrouter --bootstrap ic_admin@ic-source:3306 --account=myRouter1
    Error: You are bootstrapping as a superuser.
    This will make all the result files (config etc.) privately owned by the superuser.
    Please use --user=username option to specify the user that will be running the router.
    Use --user=root if this really should be the superuser.
    [root@ic-source ~]# mysqlrouter --bootstrap ic_admin@ic-source:3306 --account=myRouter1 --user=root
    Please enter MySQL password for ic_admin: 
    # Bootstrapping system MySQL Router instance...
    
    Please enter MySQL password for myRouter1: 
    - Creating account(s) (only those that are needed, if any)
    - Verifying account (using it to run SQL queries that would be run by Router)
    - Storing account in keyring
    - Adjusting permissions of generated files
    - Creating configuration /etc/mysqlrouter/mysqlrouter.conf
    
    Existing configuration backed up to '/etc/mysqlrouter/mysqlrouter.conf.bak'
    
    # MySQL Router configured for the InnoDB Cluster 'myCluster'
    
    After this MySQL Router has been started with the generated configuration
    
        $ /etc/init.d/mysqlrouter restart
    or
        $ systemctl start mysqlrouter
    or
        $ mysqlrouter -c /etc/mysqlrouter/mysqlrouter.conf
    
    InnoDB Cluster 'myCluster' can be reached by connecting to:
    
    ## MySQL Classic protocol
    
    - Read/Write Connections: localhost:6446
    - Read/Only Connections:  localhost:6447
    
    ## MySQL X protocol
    
    - Read/Write Connections: localhost:6448
    - Read/Only Connections:  localhost:6449
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    通过输出信息可知:

    • 使用 MySQL Classic 协议的 读/写端口6446只读端口6447
    • 使用 MySQL X 协议的 读/写端口6448只读端口6449
    启动 MySQL Router

    可能由于我的 Linux 内核版本过低,导致无法使用 systemd 提供的systemctl命令管理 MySQL Router ,这个在后续其他文章中会加以验证。

    所以我选择使用如下命令来启动 MySQL Router :

    nohup mysqlrouter 2>&1 >/dev/null &
    
    • 1
    验证路由状态

    MySQL Shell 使用listRouters()查看路由状态。

     MySQL  ic-source:33060+ ssl  JS > cluster.listRouters()
    {
        "clusterName": "myCluster", 
        "routers": {
            "source::system": {
                "hostname": "source", 
                "lastCheckIn": "2022-12-01 23:00:47", 
                "roPort": "6447", 
                "roXPort": "6449", 
                "rwPort": "6446", 
                "rwXPort": "6448", 
                "version": "8.0.31"
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在副本服务器上,连接路由器配置的端口验证路由器功能:

    1. 使用 mysql 客户端,验证使用 MySQL Classic 协议的 6446 端口可以连接到主实例的 3306 端口上:

      [root@ic-replica1 ~]# mysql -u myRouter1 -p -P 6446 -h ic-source
      Enter password: 
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 275803
      Server version: 8.0.31 MySQL Community Server - GPL
      
      Copyright (c) 2000, 2022, Oracle and/or its affiliates.
      
      Oracle is a registered trademark of Oracle Corporation and/or its
      affiliates. Other names may be trademarks of their respective
      owners.
      
      Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
      
      mysql> select @@hostname,@@port;
      +------------+--------+
      | @@hostname | @@port |
      +------------+--------+
      | ic-source  |   3306 |
      +------------+--------+
      1 row in set (0.01 sec)
      
      mysql> \q
      Bye
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24

      可见,在副本服务器上使用路由配置的 6446 端口可以连接到主实例(原服务器)的 3306 端口。

    2. 使用 mysql 客户端,验证使用 MySQL Classic 协议的 6447 端口可以负载均衡连接到次级实例(副本服务器)的 3306 端口上:

      [root@ic-replica1 ~]# mysql -u myRouter1 -p -P 6447 -h ic-source
      Enter password: 
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 1081
      Server version: 8.0.31 MySQL Community Server - GPL
      
      Copyright (c) 2000, 2022, Oracle and/or its affiliates.
      
      Oracle is a registered trademark of Oracle Corporation and/or its
      affiliates. Other names may be trademarks of their respective
      owners.
      
      Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
      
      mysql> select @@hostname,@@port;
      +-------------+--------+
      | @@hostname  | @@port |
      +-------------+--------+
      | ic-replica1 |   3306 |
      +-------------+--------+
      1 row in set (0.00 sec)
      
      mysql> \q
      Bye
      [root@ic-replica1 ~]# mysql -u myRouter1 -p -P 6447 -h ic-source
      Enter password: 
      Welcome to the MySQL monitor.  Commands end with ; or \g.
      Your MySQL connection id is 79
      Server version: 8.0.31 MySQL Community Server - GPL
      
      Copyright (c) 2000, 2022, Oracle and/or its affiliates.
      
      Oracle is a registered trademark of Oracle Corporation and/or its
      affiliates. Other names may be trademarks of their respective
      owners.
      
      Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
      
      mysql> select @@hostname,@@port;
      +-------------+--------+
      | @@hostname  | @@port |
      +-------------+--------+
      | ic-replica2 |   3306 |
      +-------------+--------+
      1 row in set (0.00 sec)
      
      mysql> \q
      Bye
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
    3. 使用 mysqlsh 程序,验证使用 MySQL X 协议的 6448 端口可以连接到主实例的 3306 端口上:

      [root@ic-replica1 ~]# mysqlsh --sql --quiet-start=2 myRouter1@ic-source:6448
       MySQL  ic-source:6448+ ssl  SQL > select @@hostname,@@port;
      +------------+--------+
      | @@hostname | @@port |
      +------------+--------+
      | ic-source  |   3306 |
      +------------+--------+
      1 row in set (0.0010 sec)
       MySQL  ic-source:6448+ ssl  SQL > \q
      Bye!
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    4. 使用 mysqlsh 程序,验证使用 MySQL X 协议的 6449 端口可以负载均衡连接到次级实例(副本服务器)的 3306 端口上:

      [root@ic-replica1 ~]# mysqlsh --sql --quiet-start=2 myRouter1@ic-source:6449
       MySQL  ic-source:6449+ ssl  SQL > select @@hostname,@@port;
      +-------------+--------+
      | @@hostname  | @@port |
      +-------------+--------+
      | ic-replica1 |   3306 |
      +-------------+--------+
      1 row in set (0.0011 sec)
       MySQL  ic-source:6449+ ssl  SQL > \q
      Bye!
      [root@ic-replica1 ~]# mysqlsh --sql --quiet-start=2 myRouter1@ic-source:6449
       MySQL  ic-source:6449+ ssl  SQL > select @@hostname,@@port;
      +-------------+--------+
      | @@hostname  | @@port |
      +-------------+--------+
      | ic-replica2 |   3306 |
      +-------------+--------+
      1 row in set (0.0023 sec)
       MySQL  ic-source:6449+ ssl  SQL > \q
      Bye!
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20

    结语

    以上便是在 InnoDB Cluster 上简易安装、部署 MySQL Router 的过程。这仅是一个测试用例,在生产环境部署时,需要根据需求做相应调整,比如网络域名、安全认证插件等。

    此外,MySQL Shell 的 AdminAPI 提供了沙盒功能用于极简测试环境。沙盒功能仅使用一台服务器就可以部署一个 InnoDB Cluster 集群实例。有关内容,将在后续文章介绍。

  • 相关阅读:
    【继承之extends关键字和super关键字】
    唐老师讲电赛
    使用OAK-D相机跑ORB-SLAM3算法遇到的问题总结
    解决ModuleNotFoundError: No module named ‘caffe‘
    【力扣】1588. 所有奇数长度子数组的和 <前缀和>
    国民经济行业代码查询系统-公司行业代码查询
    HTML5期末考核大作业:基于Html+Css+javascript的网页制作(化妆品公司网站制作)
    Bottom-Up Temporal Action Localization with Mutual Regularization BU-TAL阅读笔记
    25 Python的collections模块
    分析训练速度慢
  • 原文地址:https://blog.csdn.net/wudi53433927/article/details/128138029