如何让 MySQL 使用更少的内存?
How to make MySQL use less memory?
我正在尝试使用 Drush 将数据库更新到只有几个节点和 85 个模块的 Drupal 7 站点...因为更新几乎每次都会消失,所以对于 VPS 512MB 的容器化服务(mysql、nginx、php-fpm 等),内存不足...
据我从日志中了解到,每次 MySQL 都被内核杀死:
Out of memory: Kill process 4310 (mysqld)
我的问题是,如何配置 MySQL 服务以避免 "assassination"? MySQL 配置文件中的哪些参数可以降低 mysqld 进程的内存消耗?
我在 DEV,所以我不介意进程变慢。我只想知道我必须调整哪些参数才能在不增加内存的情况下在更新过程中存活下来。
感谢您的帮助。
有几十个这样的参数,您可以在 mysql server system variables 文档中找到所有这些参数的详细说明。通常,查找其中包含单词 size
的变量。特别是,查看 innodb_buffer_pool_size
,因为默认值为 128MB,在专用服务器上的推荐值为物理内存的 80%。
您的虚拟机只有 512MB 的 RAM?您正在 运行 处理多项事情(Drupal,MySQL)?你会幸运地让它工作。这些设置可能足以将MySQL缩小到运行:
key_buffer_size = 5M
innodb_buffer_pool_size = 60M
同时,不要增加 my.cnf 中的任何值。如果我的建议还不够;让我们看看你所有的 my.cnf;可能还有其他可以合理减少的东西。
你运行宁MySQL哪个版本?
此配置适用于 VPS 512M,希望对您有所帮助...
[mysqld]
performance_schema = off
port = 3306
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
key_buffer_size = 16M
query_cache_size = 2M
query-cache-limit = 1M
tmp_table_size = 1M
innodb_buffer_pool_size = 1M
innodb_additional_mem_pool_size = 1M
innodb_log_buffer_size = 1M
max_connections = 25
sort_buffer_size = 512M
read_buffer_size = 256K
read_rnd_buffer_size = 512K
join_buffer_size = 128K
thread_stack = 196K
binlog_cache_size = 0M
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
我在 Vultr 的 512 Mb RAM 中使用 Fedora 29 和 MariaDB 配置。使用大约 26% 的 RAM。
[mysqld]
performance_schema = off
key_buffer_size = 16M
query_cache_size = 2M
query_cache_limit = 1M
tmp_table_size = 1M
innodb_buffer_pool_size = 1M
innodb_log_buffer_size = 1M
max_connections = 25
sort_buffer_size = 512M
read_buffer_size = 256K
read_rnd_buffer_size = 512K
join_buffer_size = 128K
thread_stack = 196K
在小型 VPS 中,不要忘记启用交换。例如,在 Vultr 中,默认是没有交换。在数字海洋中也是如此。
只有你需要:
[mysqld]
performance_schema = off
我知道这个问题与 docker 无关,但如果有人使用 mysql 和 docker,我能够在 非常便宜的服务器 中 运行 一个 mysql 容器 512 mb.
我根据@Hossein 的回答使用了这些 docker-compose 参数:mysqld --performance_schema=off
services:
mysql57:
image: mysql:5.7
command: mysqld --performance_schema=off
container_name: mysql57
restart: always
ports:
- "3306:3306"
volumes:
- /opt/mysql_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: changeme
MYSQL_USER: usr_acme
MYSQL_PASSWORD: changeme
MYSQL_DATABASE: db_acme
TZ: America/Lima
deploy:
resources:
limits:
memory: 400M
在此之前,如果没有 mysqld --performance_schema=off
,服务器会变得非常慢,几分钟后就无法连接了。
您可以在命令中添加更多参数以减少 ram/cpu 用法:
command:
- "mysqld"
- "--performance_schema=off"
- "--query_cache_size=2M"
- "--query_cache_limit=1M"
- "--foo=bar"
我正在尝试使用 Drush 将数据库更新到只有几个节点和 85 个模块的 Drupal 7 站点...因为更新几乎每次都会消失,所以对于 VPS 512MB 的容器化服务(mysql、nginx、php-fpm 等),内存不足...
据我从日志中了解到,每次 MySQL 都被内核杀死:
Out of memory: Kill process 4310 (mysqld)
我的问题是,如何配置 MySQL 服务以避免 "assassination"? MySQL 配置文件中的哪些参数可以降低 mysqld 进程的内存消耗?
我在 DEV,所以我不介意进程变慢。我只想知道我必须调整哪些参数才能在不增加内存的情况下在更新过程中存活下来。
感谢您的帮助。
有几十个这样的参数,您可以在 mysql server system variables 文档中找到所有这些参数的详细说明。通常,查找其中包含单词 size
的变量。特别是,查看 innodb_buffer_pool_size
,因为默认值为 128MB,在专用服务器上的推荐值为物理内存的 80%。
您的虚拟机只有 512MB 的 RAM?您正在 运行 处理多项事情(Drupal,MySQL)?你会幸运地让它工作。这些设置可能足以将MySQL缩小到运行:
key_buffer_size = 5M
innodb_buffer_pool_size = 60M
同时,不要增加 my.cnf 中的任何值。如果我的建议还不够;让我们看看你所有的 my.cnf;可能还有其他可以合理减少的东西。
你运行宁MySQL哪个版本?
此配置适用于 VPS 512M,希望对您有所帮助...
[mysqld]
performance_schema = off
port = 3306
datadir = /var/lib/mysql
socket = /var/lib/mysql/mysql.sock
key_buffer_size = 16M
query_cache_size = 2M
query-cache-limit = 1M
tmp_table_size = 1M
innodb_buffer_pool_size = 1M
innodb_additional_mem_pool_size = 1M
innodb_log_buffer_size = 1M
max_connections = 25
sort_buffer_size = 512M
read_buffer_size = 256K
read_rnd_buffer_size = 512K
join_buffer_size = 128K
thread_stack = 196K
binlog_cache_size = 0M
# Disabling symbolic-links is recommended to prevent assorted security risks
#symbolic-links=0
# Settings user and group are ignored when systemd is used.
# If you need to run mysqld under a different user or group,
# customize your systemd unit file for mariadb according to the
# instructions in http://fedoraproject.org/wiki/Systemd
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid
#
# include all files from the config directory
#
!includedir /etc/my.cnf.d
我在 Vultr 的 512 Mb RAM 中使用 Fedora 29 和 MariaDB 配置。使用大约 26% 的 RAM。
[mysqld]
performance_schema = off
key_buffer_size = 16M
query_cache_size = 2M
query_cache_limit = 1M
tmp_table_size = 1M
innodb_buffer_pool_size = 1M
innodb_log_buffer_size = 1M
max_connections = 25
sort_buffer_size = 512M
read_buffer_size = 256K
read_rnd_buffer_size = 512K
join_buffer_size = 128K
thread_stack = 196K
在小型 VPS 中,不要忘记启用交换。例如,在 Vultr 中,默认是没有交换。在数字海洋中也是如此。
只有你需要:
[mysqld]
performance_schema = off
我知道这个问题与 docker 无关,但如果有人使用 mysql 和 docker,我能够在 非常便宜的服务器 中 运行 一个 mysql 容器 512 mb.
我根据@Hossein 的回答使用了这些 docker-compose 参数:mysqld --performance_schema=off
services:
mysql57:
image: mysql:5.7
command: mysqld --performance_schema=off
container_name: mysql57
restart: always
ports:
- "3306:3306"
volumes:
- /opt/mysql_data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: changeme
MYSQL_USER: usr_acme
MYSQL_PASSWORD: changeme
MYSQL_DATABASE: db_acme
TZ: America/Lima
deploy:
resources:
limits:
memory: 400M
在此之前,如果没有 mysqld --performance_schema=off
,服务器会变得非常慢,几分钟后就无法连接了。
您可以在命令中添加更多参数以减少 ram/cpu 用法:
command:
- "mysqld"
- "--performance_schema=off"
- "--query_cache_size=2M"
- "--query_cache_limit=1M"
- "--foo=bar"