如何在my.cnf文件中设置内存限制

时间:2012-08-24 06:26:36

标签: php mysql

如何在my.cnf文件中设置内存限制。我尝试了memory_limit = 64M。但它在重启MYSQL服务器时显示错误。任何人请帮忙...

的my.cnf

[mysqld]
datadir=/home/mysql/
tmpdir=/home/mysqltmp
#max_connections = 175 #was 175
max_connections = 80
#max_connect_errors = 350 #was 250
max_connect_errors = 250
safe-show-database
skip-locking
key_buffer = 1024M # was 128M
max_allowed_packet = 6M
myisam_sort_buffer_size = 64M

#old settings, for 900 ish max maxconn
#sort_buffer_size = 32M
#read_buffer_size = 32M
#read_rnd_buffer_size = 32M

sort_buffer_size = 5M
read_buffer_size = 5M
read_rnd_buffer_size = 5M

query_cache_size= 1024M
query_cache_limit= 16M
max_heap_table_size = 128M
tmp_table_size = 128M
thread_concurrency = 16
wait_timeout = 10
innodb_file_per_table
innodb_log_file_size = 10485760
open_files_limit = 8192
low_priority_updates = 1 
#log_slow_queries = /var/log/mysql_slow.log
#log_queries_not_using_indexes = 1
#slow_queries_log_file = /var/log/mysql_slow.log
memory_limit = 64M 
# who set these? these are NOT memory settings, but rather integer settings.
#table_cache = 1024M
#thread_cache_size = 8M

table_cache = 512
thread_cache_size = 8

[mysqldump]
quick
max_allowed_packet = 16M

[mysql]
no-auto-rehash

[isamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

[myisamchk]
key_buffer = 128M
sort_buffer_size = 128M
read_buffer = 2M
write_buffer = 2M

2 个答案:

答案 0 :(得分:24)

MySQL memory_limit文件中没有像my.cnf这样的变量。您只能从MySQL server system variables添加变量。阅读此How Mysql uses memory

这取决于MySQL服务器的RAM大小。您可以根据以下MySQL内存需求计算的基本公式相应地配置my.cnf文件:

key_buffer_size + (read_buffer_size + sort_buffer_size) * max_connections = K bytes of memory

您可能需要配置这些基本参数。

my.cnf文件中的示例变量:

#MyISAM
key_buffer_size = 8G
sort_buffer_size = 1M
read_buffer_size = 1M
read_rnd_buffer_size = 2M
myisam_sort_buffer_size = 2M
join_buffer_size = 2M

#Innodb
innodb_buffer_pool_size = 16G
innodb_additional_mem_pool_size = 2G
innodb_log_file_size = 1G
innodb_log_buffer_size = 8M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 30
innodb_file_format=barracuda

答案 1 :(得分:2)

MySQL中没有memory_limit限制设置。管理或限制MySQL内存使用量的唯一方法是降低缓存,缓冲区和池大小的设置(确切的设置名取决于您使用的存储引擎。 适用于MyISAM引擎(默认引擎)的一些设置是:

table_cache=1024
record_buffer=1M
sort_buffer_size=2M
read_buffer_size=2M
read_rnd_buffer_size=2M
myisam_sort_buffer_size=64M
thread_cache_size=128
query_cache_limit=1M
query_cache_size=64M
query_cache_type=1

对于InnoDB,你有:

innodb_buffer_pool_size = 256M
innodb_additional_mem_pool_size = 20M
innodb_log_file_size = 64M
innodb_log_buffer_size = 8M

如果您要更改PHP中的内存限制,那行(memory_limit = 64M)应该在php.ini文件中,而不是my.cnf,这是MySQL的配置文件。