查询mysql已使用的内存:
SELECT SUBSTRING_INDEX(event_name,'/',2) AS
code_area, FORMAT_BYTES(SUM(current_alloc))
AS current_alloc
FROM sys.x$memory_global_by_current_bytes
GROUP BY SUBSTRING_INDEX(event_name,'/',2)
ORDER BY SUM(current_alloc) DESC;
查询mysql事件使用的内存
SELECT * FROM sys.x$memory_global_by_current_bytes where event_name like 'memory/performance%' ORDER BY current_avg_alloc DESC
SELECT event_name, SUM(CAST(replace(current_alloc,'MiB','') as DECIMAL(10, 2)) ) FROM sys.memory_global_by_current_bytes WHERE current_alloc like '%MiB%' GROUP BY event_name ORDER BY SUM(CAST(replace(current_alloc,'MiB','') as DECIMAL(10, 2)) ) DESC ;
查询关键参数:
show variables like 'key_buffer_size';
show variables like 'query_cache_size';
show variables like 'tmp_table_size';
show variables like 'innodb_buffer_pool_size';
show variables like 'innodb_additional_mem_pool_size';
show variables like 'innodb_log_buffer_size';
show variables like 'max_connections';
show variables like 'sort_buffer_size';
show variables like 'read_buffer_size';
show variables like 'read_rnd_buffer_size';
show variables like 'join_buffer_size';
show variables like 'thread_stack';
show variables like 'binlog_cache_size';
show variables like 'performance_schema';
解决办法:
在配置文件中新增
performance_schema=OFF
关闭性能监测,重启后观察
2、glibc的内存管理器自身缺陷导致
gdb --batch --pid `pidof mysqld` --ex 'call malloc_trim(0)'
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
45305 mysql 20 0 28.4g 5.2g 8288 S 2.7 17.0 64:56.82 mysqld
3、使用jemalloc解决此问题
wget https://github.com/jemalloc/jemalloc/releases/download/4.4.0/jemalloc-4.4.0.tar.bz2
tar xjf jemalloc-4.4.0.tar.bz2
cd jemalloc-4.4.0
./configure
make && make install
find / -name "*jemalloc.so*"
export LD_PRELOAD=/usr/local/lib/libjemalloc.so
lsof -n | grep jemalloc
4、mysqldump 备份数据库优化
mysqldump在备份大表时,如果不叫-q参数,会将查询的数据写入内存中,加上-q参数,直接写入文件当中,以避免mysqld占用内存过大且不释放的问题