如果启用innoDb,如何在java中获取mysql磁盘写入

时间:2017-12-21 19:32:02

标签: java mysql innodb

我正在使用以下代码来检查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);}  
}

2 个答案:

答案 0 :(得分:0)

您可以使用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个OS文件写入。上面的数字很小,因为我从笔记本电脑上运行的沙盒MySQL实例获取了这些信息,并且它运行时间不长。

正如上面JimGarrison评论的那样,INNODB STATUS报告的大部分信息也可以作为INFORMATION_SCHEMA.INNODB_METRICS表中的各个行使用。这比使用SHOW ENGINE INNODB STATUS更容易在Java中使用并尝试解析文本。

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语句运行。

答案 1 :(得分:0)

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"
+---------------+--------+

注意:"请求"包括需要点击磁盘的写入和不需要的写入。

相关问题