Mycat之——通过命令行管理Mycat

在《Mycat之——搭建Mycat+Zookeeper+HAProxy+Keepalived+MySQL高可用架构》一文中,我们搭建了Mycat的高可用环境,在高可用环境中,实现了HAProxy的高可用、Mycat的高可用、MySQL的高可用、Zookeeper的高可用和Keepalived的高可用。

我们就需要实现Mycat的管理和监控了,不然出了问题,我们是没办法及时发现的。

1.Mycat管理端口的配置

我们可以通过mysql命令登录Mycat的管理端口,Mycat的管理端口是在server.xml文件中进行配置的,如下所示。

<property name="managerPort">3308</property>

上述配置将Mycat的管理端口配置为3308。

2.登录Mycat管理端口

我们可以使用如下方式登录Mycat的管理端口

[root@binghe151 ~]# mysql -umycat -pmycat -h192.168.175.151 -P3308 --default-auth=mysql_native_password
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.6.29-mycat-1.6.7.5-test-20200228205020 MyCat Server (monitor)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> 

3.查看Mycat支持的所有管理命令

查看Mycat支持的所有管理命令,如下所示。

mysql> show @@help;
+--------------------------------------------------------------+--------------------------------------------+
| STATEMENT                                                    | DESCRIPTION                                |
+--------------------------------------------------------------+--------------------------------------------+
| show @@time.current                                          | Report current timestamp                   |
| show @@time.startup                                          | Report startup timestamp                   |
| show @@version                                               | Report Mycat Server version                |
| show @@server                                                | Report server status                       |
| show @@threadpool                                            | Report threadPool status                   |
| show @@database                                              | Report databases                           |
| show @@datanode                                              | Report dataNodes                           |
| show @@datanode where schema = ?                             | Report dataNodes                           |
| show @@datasource                                            | Report dataSources                         |
| show @@datasource where dataNode = ?                         | Report dataSources                         |
| show @@datasource.synstatus                                  | Report datasource data synchronous         |
| show @@datasource.syndetail where name=?                     | Report datasource data synchronous detail  |
| show @@datasource.cluster                                    | Report datasource galary cluster variables |
| show @@processor                                             | Report processor status                    |
| show @@command                                               | Report commands status                     |
| show @@connection                                            | Report connection status                   |
| show @@cache                                                 | Report system cache usage                  |
| show @@backend                                               | Report backend connection status           |
| show @@session                                               | Report front session details               |
| show @@connection.sql                                        | Report connection sql                      |
| show @@sql.execute                                           | Report execute status                      |
| show @@sql.detail where id = ?                               | Report execute detail status               |
| show @@sql                                                   | Report SQL list                            |
| show @@sql.high                                              | Report Hight Frequency SQL                 |
| show @@sql.slow                                              | Report slow SQL                            |
| show @@sql.resultset                                         | Report BIG RESULTSET SQL                   |
| show @@sql.sum                                               | Report  User RW Stat                       |
| show @@sql.sum.user                                          | Report  User RW Stat                       |
| show @@sql.sum.table                                         | Report  Table RW Stat                      |
| show @@parser                                                | Report parser status                       |
| show @@router                                                | Report router status                       |
| show @@heartbeat                                             | Report heartbeat status                    |
| show @@heartbeat.detail where name=?                         | Report heartbeat current detail            |
| show @@slow where schema = ?                                 | Report schema slow sql                     |
| show @@slow where datanode = ?                               | Report datanode slow sql                   |
| show @@sysparam                                              | Report system param                        |
| show @@syslog limit=?                                        | Report system mycat.log                    |
| show @@white                                                 | show mycat white host                      |
| show @@white.set=?,?                                         | set mycat white host,[ip,user]             |
| show @@directmemory=1 or 2                                   | show mycat direct memory usage             |
| show @@check_global -SCHEMA= ? -TABLE=? -retry=? -interval=? | check mycat global table consistency       |
| switch @@datasource name:index                               | Switch dataSource                          |
| kill @@connection id1,id2,...                                | Kill the specified connections             |
| stop @@heartbeat name:time                                   | Pause dataNode heartbeat                   |
| reload @@config                                              | Reload basic config from file              |
| reload @@config_all                                          | Reload all config from file                |
| reload @@route                                               | Reload route config from file              |
| reload @@user                                                | Reload user config from file               |
| reload @@sqlslow=                                            | Set Slow SQL Time(ms)                      |
| reload @@user_stat                                           | Reset show @@sql  @@sql.sum @@sql.slow     |
| rollback @@config                                            | Rollback all config from memory            |
| rollback @@route                                             | Rollback route config from memory          |
| rollback @@user                                              | Rollback user config from memory           |
| reload @@sqlstat=open                                        | Open real-time sql stat analyzer           |
| reload @@sqlstat=close                                       | Close real-time sql stat analyzer          |
| offline                                                      | Change MyCat status to OFF                 |
| online                                                       | Change MyCat status to ON                  |
| clear @@slow where schema = ?                                | Clear slow sql by schema                   |
| clear @@slow where datanode = ?                              | Clear slow sql by datanode                 |
+--------------------------------------------------------------+--------------------------------------------+
59 rows in set (0.00 sec)

这里,列出了59个Mycat的管理命令。

4.管理Mycat

(1)重新加载配置文件

之前,我们修改Mycat的配置文件后,都是重启Mycat使配置生效的。修改了配置文件后,其实我们可以通过reload命令来重新加载配置文件。例如,我们修改了配置文件之后,可以使用如下命令使配置生效。

mysql> reload @@config;
Query OK, 1 row affected (2.33 sec)
Reload config success

需要注意的是:执行上述命令期间,Mycat是不可使用的,并且使用reload @@config只能重新加载Mycat常用的配置文件,如果修改了一些特殊的配置文件,则还是需要重启Mycat。

(2)查看逻辑库

扫描二维码关注公众号,回复: 9880980 查看本文章

可以使用如下命令查看Mycat的逻辑库,如下所示。

mysql> show @@databases;
+----------+
| DATABASE |
+----------+
| shop     |
+----------+
1 row in set (0.04 sec)

(3)查看数据节点

如果想查看逻辑库对应的物理数据库所在的数据节点,则可以使用如下命令进行查看

mysql> show @@datanode;
+-----------+-----------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| NAME      | DATHOST               | INDEX | TYPE  | ACTIVE | IDLE | SIZE | EXECUTE | TOTAL_TIME | MAX_TIME | MAX_SQL | RECOVERY_TIME |
+-----------+-----------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
| custdb    | binghe154/customer_db |     0 | mysql |      0 |    0 | 1000 |       1 |          0 |        0 |       0 |            -1 |
| mycat     | binghe151/mycat       |     0 | mysql |      0 |    0 | 1000 |       1 |          0 |        0 |       0 |            -1 |
| ordb      | binghe152/order_db    |     0 | mysql |      0 |    0 | 1000 |       1 |          0 |        0 |       0 |            -1 |
| orderdb01 | binghe152/orderdb01   |     0 | mysql |      0 |    0 | 1000 |       0 |          0 |        0 |       0 |            -1 |
| orderdb02 | binghe152/orderdb02   |     0 | mysql |      0 |    0 | 1000 |       0 |          0 |        0 |       0 |            -1 |
| orderdb03 | binghe153/orderdb03   |     0 | mysql |      0 |    0 | 1000 |       0 |          0 |        0 |       0 |            -1 |
| orderdb04 | binghe153/orderdb04   |     0 | mysql |      0 |    0 | 1000 |       1 |          0 |        0 |       0 |            -1 |
| prodb     | binghe153/product_db  |     0 | mysql |      0 |    0 | 1000 |       0 |          0 |        0 |       0 |            -1 |
+-----------+-----------------------+-------+-------+--------+------+------+---------+------------+----------+---------+---------------+
8 rows in set (0.17 sec)

在Mycat中,支持使用\G来格式化结果输出,如下所示。

mysql> show @@datanode \G
*************************** 1. row ***************************
         NAME: custdb
      DATHOST: binghe154/customer_db
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 1
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 2. row ***************************
         NAME: mycat
      DATHOST: binghe151/mycat
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 1
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 3. row ***************************
         NAME: ordb
      DATHOST: binghe152/order_db
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 1
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 4. row ***************************
         NAME: orderdb01
      DATHOST: binghe152/orderdb01
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 0
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 5. row ***************************
         NAME: orderdb02
      DATHOST: binghe152/orderdb02
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 0
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 6. row ***************************
         NAME: orderdb03
      DATHOST: binghe153/orderdb03
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 0
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 7. row ***************************
         NAME: orderdb04
      DATHOST: binghe153/orderdb04
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 1
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 8. row ***************************
         NAME: prodb
      DATHOST: binghe153/product_db
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 0
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
8 rows in set (0.00 sec)

查看shop逻辑库对应的物理数据库所在的数据节点,如下所示。

mysql> show @@datanode where schema=shop \G
*************************** 1. row ***************************
         NAME: custdb
      DATHOST: binghe154/customer_db
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 1
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 2. row ***************************
         NAME: ordb
      DATHOST: binghe152/order_db
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 1
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 3. row ***************************
         NAME: orderdb01
      DATHOST: binghe152/orderdb01
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 0
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 4. row ***************************
         NAME: orderdb02
      DATHOST: binghe152/orderdb02
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 0
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 5. row ***************************
         NAME: orderdb03
      DATHOST: binghe153/orderdb03
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 0
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 6. row ***************************
         NAME: orderdb04
      DATHOST: binghe153/orderdb04
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 1
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
*************************** 7. row ***************************
         NAME: prodb
      DATHOST: binghe153/product_db
        INDEX: 0
         TYPE: mysql
       ACTIVE: 0
         IDLE: 0
         SIZE: 1000
      EXECUTE: 0
   TOTAL_TIME: 0
     MAX_TIME: 0
      MAX_SQL: 0
RECOVERY_TIME: -1
7 rows in set (0.00 sec)

(4)查看哪些数据节点可用

可以使用如下命令查看Mycat中哪些数据节点是可用的。

mysql> show @@heartbeat \G
*************************** 1. row ***************************
            NAME: binghe53
            TYPE: mysql
            HOST: 192.168.175.153
            PORT: 3306
         RS_CODE: 1
           RETRY: 0
          STATUS: idle
         TIMEOUT: 30000
    EXECUTE_TIME: 3,11,5
LAST_ACTIVE_TIME: 2020-03-16 14:50:26
            STOP: false
*************************** 2. row ***************************
            NAME: binghe54
            TYPE: mysql
            HOST: 192.168.175.154
            PORT: 3306
         RS_CODE: 1
           RETRY: 0
          STATUS: idle
         TIMEOUT: 30000
    EXECUTE_TIME: 3,11,5
LAST_ACTIVE_TIME: 2020-03-16 14:50:26
            STOP: false
*************************** 3. row ***************************
            NAME: binghe51
            TYPE: mysql
            HOST: 192.168.175.151
            PORT: 3306
         RS_CODE: 1
           RETRY: 0
          STATUS: idle
         TIMEOUT: 30000
    EXECUTE_TIME: 2,11,5
LAST_ACTIVE_TIME: 2020-03-16 14:50:26
            STOP: false
*************************** 4. row ***************************
            NAME: binghe52
            TYPE: mysql
            HOST: 192.168.175.152
            PORT: 3306
         RS_CODE: 1
           RETRY: 0
          STATUS: idle
         TIMEOUT: 30000
    EXECUTE_TIME: 2,11,5
LAST_ACTIVE_TIME: 2020-03-16 14:50:26
            STOP: false
4 rows in set (0.00 sec)

其中,每行输出结果的 RS_CODE值为1代表连接后端的MySQL正常;为-1代表连接出错;为2代表连接超时;初始化的状态为0。我们可以根据RS_CODE值来判断后端的MySQL节点是否正常。

(5)查看上层应用连接Mycat的所有连接信息

查看上层应用连接Mycat的所有连接信息,如下所示。

mysql> show @@connection \G
*************************** 1. row ***************************
    PROCESSOR: Processor0
           ID: 2
         HOST: 192.168.175.151
         PORT: 3308
   LOCAL_PORT: 47682
         USER: mycat
       SCHEMA: NULL
      CHARSET: utf8:45
       NET_IN: 343
      NET_OUT: 8158
ALIVE_TIME(S): 2344
  RECV_BUFFER: 4096
   SEND_QUEUE: 0
      txlevel: 
   autocommit: 
1 row in set (0.00 sec)

我们可以通过kill @@connection ID的方式来杀死连接Mycat的进程,例如,我们需要杀死ID为4的连接,可以使用如下命令。

mysql> kill @@ connection 4;

(6)查看Mycat连接后端数据库的信息

可以使用如下命令来查看Mycat连接后端数据库的信息。

mysql> show @@backend \G
*************************** 1. row ***************************
 processor: Processor0
        id: 1238
   mysqlId: 626
      host: 192.168.175.154
      port: 3306
    l_port: 36852
    net_in: 78
   net_out: 0
      life: 1
    closed: false
  borrowed: false
SEND_QUEUE: 0
    schema: customer_db
   charset: utf8:45
   txlevel: 2
autocommit: true
*************************** 2. row ***************************
 processor: Processor0
        id: 1240
   mysqlId: 626
      host: 192.168.175.153
      port: 3306
    l_port: 40001
    net_in: 78
   net_out: 0
      life: 1
    closed: false
  borrowed: false
SEND_QUEUE: 0
    schema: orderdb04
   charset: utf8:45
   txlevel: 2
autocommit: true
*************************** 3. row ***************************
 processor: Processor1
        id: 1237
   mysqlId: 688
      host: 192.168.175.151
      port: 3306
    l_port: 35146
    net_in: 78
   net_out: 0
      life: 1
    closed: false
  borrowed: false
SEND_QUEUE: 0
    schema: mycat
   charset: utf8:45
   txlevel: 2
autocommit: true
*************************** 4. row ***************************
 processor: Processor1
        id: 1239
   mysqlId: 2495
      host: 192.168.175.152
      port: 3306
    l_port: 56900
    net_in: 78
   net_out: 0
      life: 1
    closed: false
  borrowed: false
SEND_QUEUE: 0
    schema: order_db
   charset: utf8:45
   txlevel: 2
autocommit: true
4 rows in set (0.13 sec)

(7)查看Mycat中的缓存状态

mysql> show @@cache \G
*************************** 1. row ***************************
      CACHE: ER_SQL2PARENTID
        MAX: 1000
        CUR: 0
     ACCESS: 0
        HIT: 0
        PUT: 0
LAST_ACCESS: 0
   LAST_PUT: 0
*************************** 2. row ***************************
      CACHE: SQLRouteCache
        MAX: 10000
        CUR: 0
     ACCESS: 0
        HIT: 0
        PUT: 0
LAST_ACCESS: 0
   LAST_PUT: 0
*************************** 3. row ***************************
      CACHE: TableID2DataNodeCache.TESTDB_ORDERS
        MAX: 50000
        CUR: 0
     ACCESS: 0
        HIT: 0
        PUT: 0
LAST_ACCESS: 0
   LAST_PUT: 0
3 rows in set (0.00 sec)

其中,各缓存的类型说明如下所示。

  • ER_SQL2PARENTID:缓存ER分片中,表与父表之间的关系
  • SQLRouteCache:缓存SQL的路由信息。
  • TableID2DataNodeCache.TESTDB_ORDERS:缓存表与表的主键与分片的对应关系。如果我们的主键不为分片键时,为了加快查询的速度,可以缓存主键与分片键的对应关系。

(8)查看数据节点所在的主机节点

mysql> show @@datasource;

Mycat命令行管理方式常用于管理单台Mycat服务,如果需要同时管理Mycat集群中的多个Mycat服务,就需要使用Mycat-Web进行管理了。

发布了1343 篇原创文章 · 获赞 2093 · 访问量 525万+

猜你喜欢

转载自blog.csdn.net/l1028386804/article/details/104910505