最近發現 MySQL 持續佔用非常多記憶體,當然原因是因為流量增加啦!
發現原來我對 MySQL 的記憶體使用認知一直是錯的,難怪設定上有點問題,流量大會把記憶體吃光光!!
看來還是要認真看文件,不能憑感覺設定!!
簡單用這個語法,就可以知道你的 my.ini (my.cnf) 設定告訴 MySQL Server 可以使用多少 RAM
SELECT ( @@key_buffer_size + @@query_cache_size + @@innodb_buffer_pool_size + @@innodb_additional_mem_pool_size + @@innodb_log_buffer_size + @@max_connections * ( @@read_buffer_size + @@read_rnd_buffer_size + @@sort_buffer_size + @@join_buffer_size + @@binlog_cache_size + @@thread_stack + @@tmp_table_size ) ) / (1024 * 1024 * 1024) AS MAX_MEMORY_GB;
在我的 Server 上測試一下,要用掉
10864.1470 GB
10864.1470 GB
10864.1470 GB
因為很重要,所以要說三次! 難怪流量大的時候,我的 MySQL 記憶體狂飆啊!!!!!!
至於如何設定,主要應該是看下面這幾個設定了! 自己算一下吧! 或是到這邊算: http://www.mysqlcalculator.com/
- read_buffer_size
- read_rnd_buffer_size
- sort_buffer_size
- join_buffer_size
- binlog_cache_size
- thread_stack
- tmp_table_size
RAM 8G 時建議設定
[mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock default-storage-engine=MYISAM character-set-server=utf8 collation-server=utf8_general_ci user=mysql symbolic-links=0 # global settings table_cache=65535 table_definition_cache=65535 max_allowed_packet=4M net_buffer_length=1M bulk_insert_buffer_size=16M query_cache_type=1 query_cache_size=8M query_cache_limit=16M # shared key_buffer_size=64M myisam_sort_buffer_size=32M max_heap_table_size=128M tmp_table_size=128M # per-thread sort_buffer_size=2M read_buffer_size=128k read_rnd_buffer_size=128k join_buffer_size=256k thread_stack=256k binlog_cache_size=0 big-tables innodb_file_per_table = 1 skip-external-locking max_connections=3000 skip-name-resolve slow_query_log slow_query_log_file = /var/log/mysql-slow.log long_query_time = 30 group_concat_max_len=65536 # according to tuning-primer.sh thread_cache_size = 8 thread_concurrency = 16 # set variables concurrent_insert=2