使用tuning-primer 分析mysql ,看了代码后对内存的分配有了一些了解,分享一下
实际内存使用主要分为几部分
1、全局buffer (global buffer),包括$innodb_buffer_pool_size
$innodb_additional_mem_pool_size
$innodb_log_buffer_size
$key_buffer_size
$query_cache_size
2、每个连接的buffer (per_thread_buffer)$read_buffer_size
$read_rnd_buffer_size
$sort_buffer_size
$thread_stack
$join_buffer_size
$binlog_cache_size
所以最大需要内存是:per_thread_buffer * max_connections + global_buffer
现实中配置了那么大内存不一定使用了那么大内存,配置了最大连接不一定有那么多连接,所以有配置最大值和实际使用值
比如配置了max_connections=5000,实际发生的最大连接是500per_thread_buffer*max_used_connections + global_buffer
对代码的文本做了一些修改,方便查看,结果展示如下:
MEMORY USAGEConfigured Max Global Buffers
$innodb_buffer_pool_size+$innodb_additional_mem_pool_size
+$innodb_log_buffer_size+$key_buffer_size+$query_cache_size: 30.58 G
#
Configured every thread Buffers ($read_buffer_size+$read_rnd_buffer_size+$sort_buffer_size
+$thread_stack+$join_buffer_size+$binlog_cache_size): 12 M
#
Configured Max all thread Buffers * max_connections (5000) : 60.57 G
#
Really Max all thread Buffers * max_used_connections (255) : 3.08 G
#
Max Memory Ever Allocated lobal_buffers+per_thread_max_buffers : 33.67 G
#
Configured Max Memory Limit(total_memoryHR) max need buffer memory
global_buffers+per_thread_buffers: 91.16 G
#
Physical Memory : 64.00 G
可见配置还是有些不合理,这样配根本支持不了5000连接,改最大连接为2000-2500左右还是合理的。