【问题标题】:how to get mysql disk writes in java if innoDb is enabled如果启用了innoDb,如何在java中获取mysql磁盘写入
【发布时间】:2018-06-04 12:34:50
【问题描述】:

我正在使用以下代码检查是否在 mysql 服务器中启用了 innoDb,但我想获取 mysql 的磁盘写入总数。请帮助以下程序

public class connectToInnoDb {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    try{  
        Class.forName("com.mysql.cj.jdbc.Driver");  
        Connection con=DriverManager.getConnection(  
        "jdbc:mysql://localhost:3310/INFORMATION_SCHEMA","root","root");   
        Statement stmt=con.createStatement();  
        ResultSet rs=stmt.executeQuery("SELECT * FROM ENGINES");  
        while(rs.next())  {
            if(rs.getString(1) == "Innodb")
                System.out.println("Yes");

        }
        con.close(); 
        }catch(Exception e){ System.out.println(e);}  
}

【问题讨论】:

  • 你的代码有什么问题。注意:比较像 rs.getString(1).equals("Innodb") 这样的字符串。
  • 上面的代码没有问题。我只想添加功能以获取 mysql 使用 Innodb 的磁盘写入总数。
  • 所以你想使用 Innodb 获取表的磁盘大小?
  • 您是否对 InnoDB INFORMATION_SCHEMA 表(如 METRICS 表)进行过研究?
  • @JimGarrison 不,我没有经历太多

标签: java mysql innodb


【解决方案1】:

您可以通过SHOW ENGINE INNODB STATUS 获得大量 InnoDB 信息,包括 I/O 计数:

mysql> SHOW ENGINE INNODB STATUS\G

...
--------
FILE I/O
--------
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
 ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
431 OS file reads, 69 OS file writes, 53 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.00 writes/s, 0.00 fsyncs/s
...

我看到上面有 69 次操作系统文件写入。上面的数字很小,因为我从笔记本电脑上运行的沙盒 MySQL 实例中获得了这些信息,而且它运行时间不长。

正如上面 JimGarrison 所说,INNODB STATUS 报告的大部分信息也可以作为 INFORMATION_SCHEMA.INNODB_METRICS 表中的单独行提供给您。这比在 Java 中使用 SHOW ENGINE INNODB STATUS 并尝试解析文本要容易得多。

mysql> SELECT * FROM INFORMATION_SCHEMA.INNODB_METRICS
       WHERE NAME = 'os_data_writes'\G

           NAME: os_data_writes
      SUBSYSTEM: os
          COUNT: 69
      MAX_COUNT: 69
      MIN_COUNT: NULL
      AVG_COUNT: 0.0034979215248910067
    COUNT_RESET: 69
MAX_COUNT_RESET: 69
MIN_COUNT_RESET: NULL
AVG_COUNT_RESET: NULL
   TIME_ENABLED: 2017-12-22 10:27:50
  TIME_DISABLED: NULL
   TIME_ELAPSED: 19726
     TIME_RESET: NULL
         STATUS: enabled
           TYPE: status_counter
        COMMENT: Number of writes initiated (innodb_data_writes)

阅读https://dev.mysql.com/doc/refman/5.7/en/innodb-information-schema-metrics-table.html

我不会展示 Java 代码,您已经知道如何运行查询并获取结果。这些语句可以像运行 SELECT 查询一样作为 SQL 语句运行。

【讨论】:

    【解决方案2】:
    mysql> SHOW GLOBAL STATUS LIKE 'Innodb%write%';
    +-----------------------------------+-------+
    | Variable_name                     | Value |
    +-----------------------------------+-------+
    | Innodb_buffer_pool_write_requests | 5379  |
    | Innodb_data_pending_writes        | 0     |
    | Innodb_data_writes                | 416   |
    | Innodb_dblwr_writes               | 30    |
    | Innodb_log_write_requests         | 1100  |
    | Innodb_log_writes                 | 88    |
    | Innodb_os_log_pending_writes      | 0     |
    | Innodb_truncated_status_writes    | 0     |
    +-----------------------------------+-------+
    
    mysql> SHOW GLOBAL STATUS LIKE 'Uptime';
    +---------------+--------+
    | Variable_name | Value  |
    +---------------+--------+
    | Uptime        | 4807   |  -- divide by this to get "per second"
    +---------------+--------+
    

    注意:“请求”包括需要写入磁盘的写入和不需要写入的写入。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-13
      • 2014-09-17
      • 1970-01-01
      • 2013-08-10
      • 2012-04-13
      • 1970-01-01
      • 2013-03-25
      相关资源
      最近更新 更多