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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12104185/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:49:35  来源:igfitidea点击:

How to set memory limit in my.cnf file

phpmysql

提问by Juice

How to set memory limit in my.cnffile. I tried with memory_limit = 64M. But its showing error while restarting MYSQL server. Any one please help...

如何在my.cnf文件中设置内存限制。我试过memory_limit = 64M。但是它在重新启动 MYSQL 服务器时显示错误。有谁请帮忙...

my.cnf

我的.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

回答by Omesh

There is no such variables like memory_limitin MySQL my.cnffile. You can add variables only from MySQL server system variables. Read this How Mysql uses memory

没有像memory_limitMySQLmy.cnf文件中那样的变量。您只能从MySQL 服务器系统变量添加变量。阅读本文Mysql 如何使用内存

It depends on RAM size of your MySQL server. You can configure your my.cnffile accordingly based on following basic formula for MySQL memory requirement calculation:

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

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

you may need to configure these basic parameters.

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

Sample variables from my.cnf file:

来自 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

回答by Pelmered

There is no memory_limit limit setting in MySQL. The only way you can manage or limit the memory usage of MySQL is to lower the settings for cache, buffer and pool sizes(the exact settingnames depend on what storage engine you are using. Some settings that apply for the MyISAM engine (the default engine) is:

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

For InnoDB you have:

对于 InnoDB,您有:

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

If you are looking to change the memory limit in PHP that line(memory_limit = 64M) should be in the php.ini file, not my.cnf which is the configuration file for MySQL.

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