性能优化 | MySQL性能监控与调优

2年前 (2022) 程序员胖胖胖虎阿
305 0 0

<html> <head></head> <body> <div class="postBody"> <div id="cnblogs_post_body" class="blogpost-body "> <blockquote> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';"><span style="color: #008000;">写压力调优:</span><a href="#yi">数据库的写</a>、<a href="#er">写压力性能监控</a>、<a href="#san">写压力调优参数</a></span> </div> </blockquote> <div> <span style="font-size: 14pt; font-family: 'Microsoft YaHei';"><strong><a name="yi"></a>一、关于DB的写</strong></span> </div> <div> <p><span style="font-size: 18px; font-family: 'Microsoft YaHei';"> 1、数据库是一个写频繁的系统</span><br /><span style="font-size: 18px; font-family: 'Microsoft YaHei';"> 2、后台写、写缓存</span><br /><span style="font-size: 18px; font-family: 'Microsoft YaHei';"> 3、commit需要写入</span><br /><span style="font-size: 18px; font-family: 'Microsoft YaHei';"> 4、写缓存失效或者写满--&gt;写压力陡增--&gt;写占读的带宽</span><br /><span style="font-size: 18px; font-family: 'Microsoft YaHei';">   1、BBU失效</span><br /><span style="font-size: 18px; font-family: 'Microsoft YaHei';">   2、写入突然增加、cache满</span><br /><span style="font-size: 18px; font-family: 'Microsoft YaHei';"> 5、日志写入、脏缓冲区写入</span></p> </div> <div> &nbsp; </div> <div> <span style="font-size: 14pt; font-family: 'Microsoft YaHei';"><strong><a name="er"></a>二、写压力性能监控</strong></span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  全面剖析写压力:多维度的对写性能进行监控。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';"><span style="color: #0000ff;">1、OS层面的监控:</span><span style="color: #ff0000;">iostat -x</span></span> </div> <div> <div class="cnblogs_code"> <pre>[root@localhost mydata]# iostat -<span style="color: #000000;">x Linux </span><span style="color: #800080;">2.6</span>.<span style="color: #800080;">32</span>-<span style="color: #800080;">642</span>.el6.x86_64 (localhost.chinaitsoft.com) <span style="color: #800080;">07</span>/<span style="color: #800080;">05</span>/<span style="color: #800080;">2017</span> _x86_64_ (<span style="color: #800080;">8</span><span style="color: #000000;"> CPU)

avg</span>-cpu: %user %<span style="color: #0000ff;">nice</span> %system %iowait %steal %<span style="color: #000000;">idle </span><span style="color: #800080;">0.00</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.03</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">99.97</span><span style="color: #000000;">

Device: rrqm</span>/s wrqm/s r/s <span style="color: #0000ff;">w</span>/s rsec/s wsec/s avgrq-sz avgqu-sz await r_await w_await svctm %<span style="color: #000000;">util scd0 </span><span style="color: #800080;">0.00</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.01</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">7.72</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">1.25</span> <span style="color: #800080;">1.25</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">1.25</span> <span style="color: #800080;">0.00</span><span style="color: #000000;"> sdc </span><span style="color: #800080;">0.02</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.01</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.07</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">7.93</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.89</span> <span style="color: #800080;">0.89</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.72</span> <span style="color: #800080;">0.00</span><span style="color: #000000;"> sda </span><span style="color: #800080;">0.18</span> <span style="color: #800080;">0.13</span> <span style="color: #800080;">0.13</span> <span style="color: #800080;">0.05</span> <span style="color: #800080;">5.38</span> <span style="color: #800080;">1.43</span> <span style="color: #800080;">37.95</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">6.63</span> <span style="color: #800080;">3.99</span> <span style="color: #800080;">13.77</span> <span style="color: #800080;">2.23</span> <span style="color: #800080;">0.04</span><span style="color: #000000;"> sdb </span><span style="color: #800080;">0.03</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.01</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">0.12</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">8.72</span> <span style="color: #800080;">0.00</span> <span style="color: #800080;">1.14</span> <span style="color: #800080;">0.80</span> <span style="color: #800080;">35.89</span> <span style="color: #800080;">0.71</span> <span style="color: #800080;">0.00</span></pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  1、写入的吞吐量:wsec/s  sec=512字节=0.5K、</span> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">写入的响应时间:await</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  2、我们需要确认我们的系统是写入还是读取的系统,如果是写入为主的系统,写压力自然就大,相关状态值也就大些。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  3、监控系统的io状况,主要查看%util、r/s、w/s,一般繁忙度在70%,每秒写也在理想值了;但如果系统目前繁忙度低,每秒写很低,可以增加写入。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';"><span style="color: #0000ff;">2、DB层面监控</span>,有没有写异常:监控<span style="color: #ff0000;">各种pending(挂起)</span></span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%pend%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------------------+-------+</span> <span style="color: #808080;">|</span> Innodb_data_pending_fsyncs <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #被挂起的fsync </span><span style="color: #808080;">|</span> Innodb_data_pending_reads <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #被挂起的物理读 </span><span style="color: #808080;">|</span> Innodb_data_pending_writes <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #被挂起的写 </span><span style="color: #808080;">|</span> Innodb_os_log_pending_fsyncs <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #被挂起的日志fsync </span><span style="color: #808080;">|</span> Innodb_os_log_pending_writes <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #被挂起的日志写 </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------------------+-------+</span> <span style="color: #800000; font-weight: bold;">5</span> rows <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  写挂起次数值大于0,甭管是什么写挂起,出现挂起的话就说明出现写压力,所以值最好的是保持为0。监控“挂起”状态值,出现大于0且持续增加,报警处理。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';"><span style="color: #0000ff;">3、写入速度监控:</span>日志写、脏页写</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  1、<span style="color: #ff6600;">日志写入速度监控</span><br /></span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%log%written</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------+-------+</span> <span style="color: #808080;">|</span> Innodb_os_log_written <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">5120</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------+-------+</span> <span style="color: #800000; font-weight: bold;">1</span> row <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  2、<span style="color: #ff6600;">脏页写入速度监控</span><br /></span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%a%written</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------------+---------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------------+---------+</span> <span style="color: #808080;">|</span> Innodb_data_written <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">1073152</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #目前为止写的总的数据量,单位字节 </span><span style="color: #808080;">|</span> Innodb_dblwr_pages_written <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">7</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> Innodb_pages_written <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">58</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #写数据页的数量 </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------------+---------+</span> <span style="color: #800000; font-weight: bold;">3</span> rows <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  3、<span style="color: #ff6600;">关注比值</span>:<span style="color: #008000;">Innodb_dblwr_pages_written / Innodb_dblwr_writes</span>,表示一次写了多少页</span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%dblwr%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------------+-------+</span> <span style="color: #808080;">|</span> Innodb_dblwr_pages_written <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">7</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #已经写入到doublewrite buffer的页的数量 </span><span style="color: #808080;">|</span> Innodb_dblwr_writes <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">3</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #doublewrite写的次数 </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------------+-------+</span> <span style="color: #800000; font-weight: bold;">2</span> rows <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.00</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">    1、如果该比值是64:1,说明doublewrite每次都是满写,写的压力很大。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';"><span style="font-size: 18px; font-family: 'Microsoft YaHei';">    2、如果系统的double_write比较高的话,iostat看到的wrqm/s(每秒合并写的值)就高,因为double_write高意味着每次写基本上都是写2M,这时候就发生更多的合并,但wrqm/s高并不害怕,因为发生合并是好事,看wrqm/s和繁忙度能不能接受。 </span></span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei'; color: #0000ff;">4、脏页的量监控</span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%dirty%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-------+</span> <span style="color: #808080;">|</span> Innodb_buffer_pool_pages_dirty <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #当前buffer pool中脏页的数量 </span><span style="color: #808080;">|</span> Innodb_buffer_pool_bytes_dirty <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #当前buffer pool中脏页的总字节数 </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-------+</span> <span style="color: #800000; font-weight: bold;">2</span> rows <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span><span style="color: #000000;"> sec)

mysql</span><span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">i%total%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-------+</span> <span style="color: #808080;">|</span> Innodb_buffer_pool_pages_total <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">8192</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #buffer pool中数据页总量 </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-------+</span> <span style="color: #800000; font-weight: bold;">1</span> row <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  <span style="color: #ff6600;">关注比值</span>:<span style="color: #008000;">Innodb_buffer_pool_pages_dirty / Innodb_buffer_pool_pages_total</span>,脏页占比</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  通过比值看脏页是否多,比如脏页10%的话,可以判断系统可能不是写为主的系统。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei'; color: #0000ff;">5、写性能瓶颈</span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%t_free</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------------------+-------+</span> <span style="color: #808080;">|</span> Innodb_buffer_pool_wait_free <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------------------+-------+</span> <span style="color: #800000; font-weight: bold;">1</span> row <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span><span style="color: #000000;"> sec)

mysql</span><span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%g_waits</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------+-------+</span> <span style="color: #808080;">|</span> Innodb_log_waits <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------+-------+</span> <span style="color: #800000; font-weight: bold;">1</span> row <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.00</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  1、Innodb_buffer_pool_wait_free,如果该值大于0,说明buffer pool中已经没有可用页,等待后台往回刷脏页,腾出可用数据页,这样就很影响业务了,hang住。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  2、Innodb_log_waits,如果该值大于0,说明写压力很大,出现了日志等待。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';"><span style="color: #0000ff;">6、系统真实负载:</span>rows增删改查 、事务提交、事务回滚</span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">i%rows%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------+-------+</span> <span style="color: #808080;">|</span> Innodb_rows_deleted <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> Innodb_rows_inserted <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">145</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> Innodb_rows_read <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">233</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> Innodb_rows_updated <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">5</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------+-------+</span> <span style="color: #800000; font-weight: bold;">4</span> rows <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span><span style="color: #000000;"> sec)

mysql</span><span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%commit%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------+-------+</span> <span style="color: #808080;">|</span> Com_commit <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> Com_xa_commit <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> Handler_commit <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">16</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------+-------+</span> <span style="color: #800000; font-weight: bold;">3</span> rows <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span><span style="color: #000000;"> sec)

mysql</span><span style="color: #808080;">></span> show global status <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%rollback%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------------+-------+</span> <span style="color: #808080;">|</span> Com_rollback <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> Com_rollback_to_savepoint <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> Com_xa_rollback <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> Handler_rollback <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> Handler_savepoint_rollback <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">--------------------------+-------+</span> <span style="color: #800000; font-weight: bold;">5</span> rows <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  通过监控系统真实负载,如果业务正常,负载上升,写压力是那自然是无可厚非的。此时,就要根据业务具体情况,进行相应的调优。 </span> </div> <div>   </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';"><strong><a name="san"></a><span style="font-size: 14pt;">三、写压力调优参数</span></strong></span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  降低写压力、加大写入的力度。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  通过调整参数降低写压力时,一定要<span style="color: #ff0000;">实时关注iostat系统的各项指标。</span></span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei'; color: #0000ff;">1、脏页刷新的频率</span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show variables <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">i%depth%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------+-------+</span> <span style="color: #808080;">|</span> innodb_lru_scan_depth <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">1024</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------+-------+</span> <span style="color: #800000; font-weight: bold;">1</span> row <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  默认1024,遍历lru list刷新脏页,值越大,说明刷脏页频率越高。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';"><span style="color: #0000ff;">2、磁盘刷新脏页的量</span>:<span style="color: #008080;">磁盘io能力</span></span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show variables <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%io_c%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------------+-------+</span> <span style="color: #808080;">|</span> innodb_io_capacity <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">200</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> innodb_io_capacity_max <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">2000</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">----------------------+-------+</span> <span style="color: #800000; font-weight: bold;">2</span> rows <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.00</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  根据磁盘io能力进行调整,值越大,每次刷脏页的量越大。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei'; color: #0000ff;">3、redolog调优</span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show variables <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">innodb_log%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------------+----------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------------+----------+</span> <span style="color: #808080;">|</span> innodb_log_buffer_size <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">16777216</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> innodb_log_checksums <span style="color: #808080;">|</span> <span style="color: #0000ff;">ON</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #解决数据在io环节的出错问题,checksum值检查 </span><span style="color: #808080;">|</span> innodb_log_compressed_pages <span style="color: #808080;">|</span> <span style="color: #0000ff;">ON</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> innodb_log_file_size <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">50331648</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> innodb_log_files_in_group <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">2</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> innodb_log_group_home_dir <span style="color: #808080;">|</span> .<span style="color: #808080;">/</span> <span style="color: #808080;">|</span> <span style="color: #808080;">|</span> innodb_log_write_ahead_size <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">8192</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------------+----------+</span> <span style="color: #800000; font-weight: bold;">7</span> rows <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  logfile大小和组数可能会导致写抖动:日志切换频率需要监控(文件系统层面技巧)。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei'; color: #0000ff;">4、redolog的刷新机制</span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show variables <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%flush%commit</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-------+</span> <span style="color: #808080;">|</span> innodb_flush_log_at_trx_commit <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">1</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-------+</span> <span style="color: #800000; font-weight: bold;">1</span> row <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.00</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  默认MySQL的刷盘策略是1,最安全的,但是安全的同时,自然也就会带来一定的性能压力。在写压力巨大的情况下,根据具体的业务场景,牺牲安全性的将其调为0或2。</span> </div> <blockquote> <div> <strong><span style="font-size: 18px; font-family: 'Microsoft YaHei';">关于redolog的刷盘策略:</span></strong> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  也就是用户在commit,事务提交时,处理redolog的方式(0、1、2):</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';"><span style="font-size: 18px; font-family: 'Microsoft YaHei';"><img src="https://images2017.cnblogs.com/blog/1113510/201708/1113510-20170818194906084-1986167297.png" alt="" width="747" height="526" /></span></span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  0:当提交事务时,并不将事务的redo log写入logfile中,而是等待master thread每秒的刷新redo log。(数据库崩溃丢失数据,丢一秒钟的事务)</span> <br /> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  1:执行commit时将redo log同步写到磁盘logfile中,即伴有fsync的调用(默认是1,保证不丢失事务)</span> <br /> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  2:在每个提交,日志缓冲被写到文件系统缓存,但不是写到磁盘的刷新(数据库宕机而操作系统及服务器并没有宕机,当恢复时能保证数据不丢失;但是文件系统(OS)崩溃会丢失数据)</span> </div> </blockquote> <div> <div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei'; color: #0000ff;">5、定义每次日志刷新的时间</span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show variables <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">innodb_flush_log_at_timeout</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------------+-------+</span> <span style="color: #808080;">|</span> innodb_flush_log_at_timeout <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">1</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">---------------------------+-------+</span> <span style="color: #800000; font-weight: bold;">1</span> row <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span> sec)</pre> </div> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  默认是1,也就是每秒log刷盘,配合innodb_flush_log_at_trx_commit来设置,为了充分保证数据的一致性,一般innodb_flush_log_at_trx_commit=1,这样的话,innodb_flush_log_at_timeout的设置也就没有意义了。因此,该参数的设置只针对innodb_flush_log_at_trx_commit为0/2起作用。</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei'; color: #0000ff;">6、内存脏页占比控制</span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show variables <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%dirty%pct%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-----------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-----------+</span> <span style="color: #808080;">|</span> innodb_max_dirty_pages_pct <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">75.000000</span> <span style="color: #808080;">|</span><span style="color: #000000;"> #脏页在buffer pool中的最大占比 </span><span style="color: #808080;">|</span> innodb_max_dirty_pages_pct_lwm <span style="color: #808080;">|</span> <span style="color: #800000; font-weight: bold;">0.000000</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------------------+-----------+</span> <span style="color: #800000; font-weight: bold;">2</span> rows <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span> sec)</pre> </div> <p><span style="font-size: 18px; font-family: 'Microsoft YaHei';">  在内存buffer pool空间允许的范围下,可以调大脏页允许在内存空间的占比,可解燃眉之急,降低写压力。</span></p> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei'; color: #0000ff;">7、关闭doublewrite降低写压力</span> </div> <div> <div class="cnblogs_code"> <pre>mysql<span style="color: #808080;">></span> show variables <span style="color: #808080;">like</span> <span style="color: #ff0000;">'</span><span style="color: #ff0000;">%doub%</span><span style="color: #ff0000;">'</span><span style="color: #000000;">; </span><span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------+-------+</span> <span style="color: #808080;">|</span> Variable_name <span style="color: #808080;">|</span> Value <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------+-------+</span> <span style="color: #808080;">|</span> innodb_doublewrite <span style="color: #808080;">|</span> <span style="color: #0000ff;">ON</span> <span style="color: #808080;">|</span> <span style="color: #808080;">+</span><span style="color: #008080;">--</span><span style="color: #008080;">------------------+-------+</span> <span style="color: #800000; font-weight: bold;">1</span> row <span style="color: #808080;">in</span> <span style="color: #0000ff;">set</span> (<span style="color: #800000; font-weight: bold;">0.01</span> sec)</pre> </div> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  两次写特性,默认开启,静态参数。<span style="color: #ff6600;">关闭doublewrite适合的场景:</span></span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  1、海量DML</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  2、不惧怕数据损坏和丢失</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">  3、系统写负载成为主要负载,关闭doublewrite,降低写压力</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei'; color: #ffffff; background-color: #000000;">注意:</span> </div> <div> <span style="font-size: 18px; font-family: 'Microsoft YaHei';">关于参数调整的生效范围,如何调整(静态参数、动态参数),都是要依据官方文档,依照文档进行调参。</span> </div>

</div> <div class="clear"></div> <div id="blog_post_info_block"> <div id="blog_post_info"> <div id="author_profile"> <div class="clear"></div> <div id="author_profile_honor"></div> </div> <script type="text/javascript"> currentDiggType = 0; </script> </div> </div> </div> </body> </html>

版权声明:程序员胖胖胖虎阿 发表于 2022年9月20日 上午12:48。
转载请注明:性能优化 | MySQL性能监控与调优 | 胖虎的工具箱-编程导航

相关文章

暂无评论

暂无评论...