现象是:生产环境的内存使用率经常超过85%阀值,nagios不停报警,以往的处理办法就是restart,一切memery使用率回到65%。还得研究找到问题所在,所以在测试环境进行问题测试分析。
1 先查看问题当前的内存使用情况:
[novamysqladminlt@test-by7db2 ~]$ free -m
total used free shared buffers cached
Mem: 32108 24792 7316 0 824 11019
-/+ buffers/cache: 12948 19159
Swap: 32767 42 32725
[novamysqladminlt@test-by7db2 ~]$
2 去运行一个无索引的消耗资源的sql如下
mysql> select * from tb01 where GROUP_ID>100 limit 10000000, 1000;
3 去运行一个无索引的消耗资源的sql如下
mysql> select * from tb01 where GROUP_ID>100 limit 10000000, 1000;
4 然后去检查内存使用情况:
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 25358 6750 0 824 10976
-/+ buffers/cache: 13557 18550
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 25399 6709 0 824 10976
-/+ buffers/cache: 13598 18510
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26807 5301 0 824 10943
-/+ buffers/cache: 15039 17069
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26841 5267 0 824 10939
-/+ buffers/cache: 15077 17031
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26862 5246 0 824 10931
-/+ buffers/cache: 15106 17002
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26894 5213 0 824 10931
-/+ buffers/cache: 15139 16969
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26895 5213 0 824 10931
-/+ buffers/cache: 15139 16969
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26895 5213 0 824 10931
-/+ buffers/cache: 15139 16969
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26895 5212 0 824 10932
-/+ buffers/cache: 15139 16969
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26896 5211 0 824 10932
-/+ buffers/cache: 15140 16968
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26896 5211 0 824 10932
-/+ buffers/cache: 15139 16968
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26897 5211 0 824 10932
-/+ buffers/cache: 15139 16968
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$
[mysql@test-by7db2 5.5.25a]$
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26897 5211 0 824 10933
-/+ buffers/cache: 15139 16968
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$
OK,查询跑完了,耗时如下:
......
1000 rows in set (1 min 4.15 sec)
4 检查现在运行完SQL的内存使用情况:
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26913 5195 0 824 10946
-/+ buffers/cache: 15143 16965
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26912 5196 0 824 10946
-/+ buffers/cache: 15141 16967
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26911 5196 0 824 10946
-/+ buffers/cache: 15140 16967
Swap: 32767 44 32723
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 26911 5196 0 824 10946
-/+ buffers/cache: 15140 16968
Swap: 32767 44 32723
不停的在使用着的内存不停的在增长,used值直线上升。最后等查询结束了,会稳定在26911M上面,不再动弹,但是比原来的25358要高很多。
为了验证这个,再继续执行一个慢sql:
mysql> select * from tb01 where GROUP_ID>100 limit 12000000, 1000;
used继续不停的增长,加大。
5,也许是innodb_buffer_pool_size值太大了,尝试把innodb_buffer_pool_size值调小看看:
现在把innodb_buffer_pool_size值从25000M调整到12000M,试试看效果。
innodb_buffer_pool_size = 12000M
[mysql@test-by7db2 5.5.25a]$ service mysql status
SUCCESS! MySQL running (8913)
[mysql@test-by7db2 5.5.25a]$ service mysql restart
Shutting down MySQL........... SUCCESS!
rm: cannot remove `/var/lock/subsys/mysql': Permission denied
Starting MySQL........... SUCCESS!
[mysql@test-by7db2 5.5.25a]$
[mysql@test-by7db2 5.5.25a]$
[mysql@test-by7db2 5.5.25a]$ free -m
total used free shared buffers cached
Mem: 32108 12752 19356 0 840 9681
-/+ buffers/cache: 2230 29878
Swap: 32767 26 32741
[mysql@test-by7db2 5.5.25a]$
去客户端执行SQL:
mysql> select * from identity.tb01 where GROUP_ID>100 limit 12000000, 1000;
再来看used使用情况:
[novamysqladminlt@eanltemydb5-c000utl ~]$ ssh test-by7db2.abn-sjl.ea.com
Last login: Mon Nov 25 02:55:46 2013 from eanltemydb5-c000utl.abn-sjl.ea.com
[novamysqladminlt@test-by7db2 ~]$ free -m
total used free shared buffers cached
Mem: 32108 14197 17911 0 840 9681
-/+ buffers/cache: 3675 28433
Swap: 32767 26 32741
[novamysqladminlt@test-by7db2 ~]$
used值在增加,再来执行新的SQL语句:
mysql> select * from identity.tb01 where GROUP_ID>100 limit 13000000, 1000;
[novamysqladminlt@test-by7db2 ~]$ free -m
total used free shared buffers cached
Mem: 32108 15682 16426 0 840 9681
-/+ buffers/cache: 5160 26948
Swap: 32767 26 32741
看出每执行一条不同的SQL语句,used值都在增加。
mysql> select * from identity.tb01 where GROUP_ID>100 limit 13000000, 1000;
[novamysqladminlt@test-by7db2 ~]$ free -m
total used free shared buffers cached
Mem: 32108 15682 16426 0 840 9681
-/+ buffers/cache: 5160 26948
Swap: 32767 26 32741
看出执行相同的select语句,内存使用率used没有增加。
mysql> select * from identity.tb01 where GROUP_ID>100 limit 14000000, 1000;
[novamysqladminlt@test-by7db2 ~]$ free -m
total used free shared buffers cached
Mem: 32108 15931 16177 0 840 9681
-/+ buffers/cache: 5408 26699
Swap: 32767 26 32741
使用新的sql语句,used使用的内存从15682增加到15931。
从这里可以看出,不管innodb_buffer_pool_size值设置多大多小,used值总是会增加的,看来分析的思路有误,得换个方向。
6 memory使用率判断:
mysql> SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_pages_data';
+-------------------------------+--------+
| Variable_name | Value |
+-------------------------------+--------+
| Innodb_buffer_pool_pages_data | 1322836|
+-------------------------------+--------+
1 row in set (0.00 sec)
mysql> SHOW GLOBAL STATUS LIKE 'Innodb_buffer_pool_pages_total';
+--------------------------------+---------+
| Variable_name | Value |
+--------------------------------+---------+
| Innodb_buffer_pool_pages_total | 1344000 |
+--------------------------------+---------+
1 row in set (0.00 sec)
mysql> SHOW GLOBAL STATUS LIKE 'Innodb_page_size';
+------------------+-------+
| Variable_name | Value |
+------------------+-------+
| Innodb_page_size | 16384 |
+------------------+-------+
1 row in set (0.00 sec)
Innodb_buffer_pool_pages_data' X 'Innodb_page_size' X 1.05 / (1024*1024*1024)
Innodb_buffer_pool_pages_data' X 'Innodb_page_size' X 1.05 / (1024*1024*1024)
'Innodb_buffer_pool_pages_data' X 100 / 'Innodb_buffer_pool_pages_total'
当结果 > 95% 则增加 innodb_buffer_pool_size, 建议使用 ram total 75%
当结果 < 95% 则减少 innodb_buffer_pool_size,
建议 'Innodb_buffer_pool_pages_data' X 'Innodb_page_size' X 1.05 / (1024*1024*1024)
这计算 'Innodb_buffer_pool_pages_data' X 100 / 'Innodb_buffer_pool_pages_total' 值是
mysql> select 1322836*100/1344000;
+---------------------+
| 1322836*100/1344000 |
+---------------------+
| 98.4253 |
+---------------------+
1 row in set (0.00 sec)
[...持续分析中...]