我有一个用于 wordpress 1gb 服务器的 lemp 堆栈。Mysql 不断终止进程,其中一个是 netdata。因此,我不得不不断进入 ssh 控制台来重新启动它。
这是我在错误日志中看到的内容:
Out of memory: Kill process 29383 (mysqld) score 244 or sacrifice child
Killed process 29383 (mysqld) total-vm:1169884kB, anon-rss:288068kB, file-
rss:0kB, shmem-rss:0kB
我需要更多内存吗?目前,我的美食博客平均每天有 70-100 位访客。
Nginx 配置
user www-data;
worker_processes 1;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 1024;
use epoll;
multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
keepalive_requests 100000;
types_hash_max_size 2048;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
#from ngninx optimize linode page
client_body_buffer_size 128k;
client_max_body_size 10m;
client_header_buffer_size 1k;
large_client_header_buffers 4 4k;
output_buffers 1 32k;
postpone_output 1460;
client_header_timeout 3m;
client_body_timeout 3m;
send_timeout 3m;
open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 5;
open_file_cache_errors off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_min_length 1000;
gzip_types text/html application/x-javascript text/css application/javascript text/javascript text/plain text/xml application/json application/vnd.ms-fontobject application/x-font-opentype application/x-font-truetype application/x-font-ttf application/xml font/eot font/opentype font/otf image/svg+xml image/vnd.microsoft.icon;
#gzip_disable "msie6";
gzip_disable "MSIE [1-6]\.";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
MYSQL 配置
[mysqld]
max_allowed_packet = 1M
thread_stack = 128K
max_connections = 50
table_open_cache = 32M
key_buffer_size = 64M
最佳结果 mysqld Ver 5.7.19-0ubuntu0.17.04.1 适用于 x86_64 上的 Linux ((Ubuntu)
KiB Mem : 82.9/1013064
KiB Swap:100.0/262140
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
23755 www-data 20 0 445704 70616 9572 S 18.9 7.0 0:03.49 php-fpm7.0
11958 root 20 0 727764 10704 2036 S 3.0 1.1 46:25.45 fail2ban-se+
14667 www-data 20 0 145976 4768 2976 S 1.3 0.5 7:03.61 nginx
22808 mysql 20 0 1116544 204516 3360 S 0.7 20.2 0:14.66 mysqld
22868 netdata 20 0 179348 21292 1116 S 0.3 2.1 0:05.70 netdata
答案1
不,您不需要更多内存。我在 AWS ec2.nano 上运行了五种大小的 Wordpress,它有 512MB 的 RAM - 是您使用的一半。
它实际上只使用了 363MB 的 RAM,其余的是缓存。两个 PHP 进程占用了 37% 的 RAM,MySQL 占用了 9%,Nginx 占用了 1.3%。
total used free shared buffers cached
Mem: 501296 363152 138144 127004 37892 188776
-/+ buffers/cache: 136484 364812
Swap: 524284 119740 404544
对我来说,关键是减少 MySQL 使用的 RAM,特别是在性能架构方面。我有一个低内存系统上的 MySQL 教程在这里,您可以阅读,但关键部分如下。
您可能还想为服务器添加虚拟内存。我在 EC2 上使用 EBS 磁盘(即远程 NAS)执行了此操作,效果很好。详情请见此处。
[mysqld]
# Disable performance schema to hugely reduce RAM usage
performance_schema = OFF
# I'm not sure if these two commands make any difference, but they don't seem to hurt
innodb_buffer_pool_size=30M
innodb_log_buffer_size=256K
[mysqld_safe]
# Settings to reduce RAM
innodb_buffer_pool_size=25M
innodb_log_buffer_size=256K
query_cache_size=10000
max_connections=30
key_buffer_size=80
innodb_ft_cache_size=1600000
innoinnodb_ft_total_cache_size=32000000
table_definition_cache=150
# Settings to reduce RAM: per thread or per operation settings
thread_stack=131072
sort_buffer_size=32K
read_buffer_size=8200
read_rnd_buffer_size=8200
max_heap_table_size=16K
tmp_table_size=50K
bulk_insert_buffer_size=100
join_buffer_size=128
net_buffer_length=1K
innodb_sort_buffer_size=64K
# Settings to reduce RAM: settings that relate to the binary log (if enabled)
binlog_cache_size=4K
binlog_stmt_cache_size=4K
另外,请注意。MySQL 不会终止进程,操作系统会因为 MySQL 使用的 RAM 而终止进程。您可以使用类似监控重新启动进程,但您需要先减少 RAM 使用量。
在按 M 按内存使用情况排序后,top 的输出将是有用的信息,可以包含在您的问题中。它还会告诉您从哪里开始。
更新
问题可能是 PHP 创建了大量占用 RAM 的进程,迫使操作系统终止进程。解决方案可能是限制 PHP 进程。
对我来说,这是在 /etc/php-fpm-5.6.d/www.conf 中配置的,但对你来说可能有所不同
pm.max_children = 4
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 2
pm.max_requests = 25