我有数据库(仅限30mb),这个数据库使用4gb内存!在我的网站上最多有10人在线。我认为这是不好 cnf.my。
MySQL>显示像'%size%'这样的变量;+---------------------------------+----------------------+|Variable_name |价值 |+---------------------------------+----------------------+|binlog_cache_size |32768 ||bulk_insert_buffer_size |8388608 ||delayed_queue_size |1000 ||innodb_additional_mem_pool_size |1048576 ||innodb_buffer_pool_size |8388608 ||innodb_log_buffer_size |1048576 ||innodb_log_file_size |5242880 ||join_buffer_size |4194304 ||key_buffer_size |268435456 ||key_cache_block_size |1024 ||large_page_size |0 ||max_binlog_cache_size |18446744073709547520 ||max_binlog_size |1073741824 ||max_heap_table_size |268435456 ||max_join_size |18446744073709551615 ||max_long_data_size |1048576 ||max_relay_log_size |0 ||myisam_data_pointer_size |6 ||myisam_max_sort_file_size |9223372036853727232 ||myisam_mmap_size |18446744073709551615 ||myisam_sort_buffer_size |8388608 ||preload_buffer_size |32768 ||profiling_history_size |15 ||query_alloc_block_size |8192 ||query_cache_size |268435456 ||query_prealloc_size |8192 ||range_alloc_block_size |4096 ||read_buffer_size |131072 ||read_rnd_buffer_size |262144 ||sort_buffer_size |20971520 ||sql_max_join_size |18446744073709551615 ||thread_cache_size |4 ||tmp_table_size |41943040 ||transaction_alloc_block_size |8192 ||transaction_prealloc_size |4096 |+---------------------------------+----------------------+35 行(0.00 秒)
和我的.cnf:
[mysqld]
local-infile=0
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
long_query_time=1
log-slow-queries=/var/log/mysql/log-slow-queries.log
interactive_timeout=180
wait_timeout=180
max_connections=100
innodb_buffer_pool_size=256M
query_cache_size=256M
key_buffer_size=256M
sort_buffer_size=20M
innodb_flush_log_at_trx_commit=0
innodb_flush_method=O_DIRECT
query_cache_type=1
query_cache_limit=2M
table_cache=1024
join_buffer_size=4M
thread_cache_size=4
tmp_table_size=40M
max_heap_table_size=256MB
user=mysql
symbolic-links=0
#bind-address=127.0.0.1
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
在我的 vps 中,我有 5.7 GB 内存
如何减少内存使用量?
我怀疑sort_buffer_size和join_buffer_size是这里内存使用过多的主要原因。 首先在配置中注释掉这些值,以便使用默认值。
这些值可能会影响与工作负载相关的内存使用情况。
对你的系统知之甚少,似乎你的key_buffer_size和innodb_buffer_pool_size可能是需要的两倍(一个或另一个可能是完全不必要的,这取决于你使用的存储引擎),你的query_cache_size可能是需要的2到16倍。 与上述设置不同,这些设置可能会因工作负载而异内存影响,无论工作负载如何,这些设置都将减少服务器使用的基线内存量。
下次系统的内存使用率高于预期时,SET GLOBAL THREAD_CACHE_SIZE = 0;
然后观察服务器的内存分配是否随着客户端断开连接和重新连接而更改,也可能很有趣。
my.cnf 要求
innodb_buffer_pool_size=256M
但尺寸列表显示您只有 8M 用于 IBPS。需要找出你为什么没有256M,检查日志。
和
max_heap_table_size=256MB needs to be
max_heap_table_size=40M # from 256MB to correct MB and match tmp_table_size