帮帮我! 我的服务器内存不足,locking,需要重新启动

我有一个VPS(拥有256MB的内存),今天已经挂了不止一次。 它只是完全冻结,不能通过SSH访问。 我必须运行我的提供商的“强制closures”程序才能恢复。 然后服务器持续了几个小时,然后再做一次。

第一次有消息说内存不足,试图杀死mysqld进程。 接下来我设法在syslog文件中find这个:

Sep 23 01:47:34: [16942.757665] [out_of_memory+0x19e/0x1e0] out_of_memory+0x19e/0x1e0 Sep 23 01:47:34: [16942.758875] Out of memory: kill process 5983 (apache2) score 57519 or a child 

我已经configuration了Apache和MySQL,对于低RAM,至less据我所知,我想知道为什么会发生这种情况。 我只需要升级,还是有什么问题?

这里是我的Apacheconfiguration摘录:

 # # Timeout: The number of seconds before receives and sends time out. # Timeout 30 # # KeepAlive: Whether or not to allow persistent connections (more than # one request per connection). Set to "Off" to deactivate. # KeepAlive On # # MaxKeepAliveRequests: The maximum number of requests to allow # during a persistent connection. Set to 0 to allow an unlimited amount. # We recommend you leave this number high, for maximum performance. # MaxKeepAliveRequests 200 # # KeepAliveTimeout: Number of seconds to wait for the next request from the # same client on the same connection. # KeepAliveTimeout 3 ## ## Server-Pool Size Regulation (MPM specific) ## # prefork MPM # StartServers: number of server processes to start # MinSpareServers: minimum number of server processes which are kept spare # MaxSpareServers: maximum number of server processes which are kept spare # MaxClients: maximum number of server processes allowed to start # MaxRequestsPerChild: maximum number of requests a server process serves <IfModule mpm_prefork_module> StartServers 1 MinSpareServers 1 MaxSpareServers 3 MaxClients 50 MaxRequestsPerChild 1000 </IfModule> 

和我的MySQLconfiguration:

 [mysqld] # # * Basic Settings # # # * IMPORTANT # If you make changes to these settings and your system uses apparmor, you may # also need to also adjust /etc/apparmor.d/usr.sbin.mysqld. # user = mysql pid-file = /var/run/mysqld/mysqld.pid socket = /var/run/mysqld/mysqld.sock port = 3306 basedir = /usr datadir = /var/lib/mysql tmpdir = /tmp language = /usr/share/mysql/english skip-external-locking skip-locking # # Instead of skip-networking the default is now to listen only on # localhost which is more compatible and is not less secure. bind-address = 127.0.0.1 # # * Fine Tuning # key_buffer = 16K max_allowed_packet = 1M thread_stack = 64K thread_cache_size = 4 sort_buffer = 64K net_buffer_length = 2K #max_connections = 100 #table_cache = 64 #thread_concurrency = 10 # # * Query Cache Configuration # query_cache_limit = 500k #was 1M query_cache_size = 8M #was 16M, lowered for RAM # # * Logging and Replication # # Both location gets rotated by the cronjob. # Be aware that this log type is a performance killer. #log = /var/log/mysql/mysql.log # # Error logging goes to syslog. This is a Debian improvement :) # # Here you can see queries with especially long duration #log_slow_queries = /var/log/mysql/mysql-slow.log #long_query_time = 2 #log-queries-not-using-indexes # # The following can be used as easy to replay backup logs or for replication. # note: if you are setting up a replication slave, see README.Debian about # other settings you may need to change. #server-id = 1 #log_bin = /var/log/mysql/mysql-bin.log expire_logs_days = 10 max_binlog_size = 100M #binlog_do_db = include_database_name #binlog_ignore_db = include_database_name # # * BerkeleyDB # # Using BerkeleyDB is now discouraged as its support will cease in 5.1.12. skip-bdb # # * InnoDB # # InnoDB is enabled by default with a 10MB datafile in /var/lib/mysql/. # Read the manual for more InnoDB related options. There are many! # You might want to disable InnoDB to shrink the mysqld process by circa 100MB. skip-innodb # # * Security Features # # Read the manual, too, if you want chroot! # chroot = /var/lib/mysql/ # # For generating SSL certificates I recommend the OpenSSL GUI "tinyca". # # ssl-ca=/etc/mysql/cacert.pem # ssl-cert=/etc/mysql/server-cert.pem # ssl-key=/etc/mysql/server-key.pem [mysqldump] quick quote-names max_allowed_packet = 16M [mysql] #no-auto-rehash # faster start of mysql but no tab completition [isamchk] key_buffer = 16M 

如果有帮助,重启后约20-30分钟,这里有一个ps aux输出:

 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.3 3992 920 ? Ss 01:53 0:00 /sbin/init root 2 0.0 0.0 0 0 ? S< 01:53 0:00 [kthreadd] root 3 0.0 0.0 0 0 ? S< 01:53 0:00 [migration/0] root 4 0.0 0.0 0 0 ? S< 01:53 0:00 [ksoftirqd/0] root 5 0.0 0.0 0 0 ? S< 01:53 0:00 [watchdog/0] root 6 0.0 0.0 0 0 ? S< 01:53 0:00 [events/0] root 7 0.0 0.0 0 0 ? S< 01:53 0:00 [khelper] root 18 0.0 0.0 0 0 ? S< 01:53 0:00 [xenwatch] root 19 0.0 0.0 0 0 ? S< 01:53 0:00 [xenbus] root 27 0.0 0.0 0 0 ? S< 01:53 0:00 [migration/1] root 28 0.0 0.0 0 0 ? S< 01:53 0:00 [ksoftirqd/1] root 29 0.0 0.0 0 0 ? S< 01:53 0:00 [watchdog/1] root 30 0.0 0.0 0 0 ? S< 01:53 0:00 [events/1] root 54 0.0 0.0 0 0 ? S< 01:53 0:00 [kblockd/0] root 55 0.0 0.0 0 0 ? S< 01:53 0:00 [kblockd/1] root 65 0.0 0.0 0 0 ? S< 01:53 0:00 [kseriod] root 104 0.0 0.0 0 0 ? S 01:53 0:00 [pdflush] root 105 0.0 0.0 0 0 ? S 01:53 0:00 [pdflush] root 106 0.0 0.0 0 0 ? S< 01:53 0:00 [kswapd0] root 107 0.0 0.0 0 0 ? S< 01:53 0:00 [aio/0] root 108 0.0 0.0 0 0 ? S< 01:53 0:00 [aio/1] root 124 0.0 0.0 0 0 ? S< 01:53 0:00 [accel_watch/0] root 125 0.0 0.0 0 0 ? S< 01:53 0:00 [accel_watch/1] root 2031 0.0 0.0 0 0 ? S< 01:53 0:00 [kjournald] root 2192 0.0 0.3 16848 944 ? S<s 01:53 0:00 /sbin/udevd --daemon syslog 3514 0.0 0.2 12296 728 ? Ss 01:53 0:00 /sbin/syslogd -u syslog root 3534 0.0 0.2 8132 592 ? S 01:53 0:00 /bin/dd bs 1 if /proc/kmsg of /var/run/klogd/kmsg klog 3537 0.0 0.8 5600 2292 ? Ss 01:53 0:00 /sbin/klogd -P /var/run/klogd/kmsg root 3556 0.0 0.4 50916 1160 ? Ss 01:53 0:00 /usr/sbin/sshd root 3776 0.0 0.8 36684 2152 ? Ss 01:53 0:00 /usr/lib/postfix/master postfix 3794 0.0 0.8 38740 2140 ? S 01:53 0:00 pickup -l -t fifo -u -c postfix 3795 0.0 0.8 38784 2228 ? S 01:53 0:00 qmgr -l -t fifo -u root 3802 0.0 0.2 12460 644 ? Ss 01:53 0:00 /usr/sbin/dovecot root 3815 0.0 1.0 71648 2876 ? S 01:53 0:00 dovecot-auth daemon 3816 0.0 0.1 16428 428 ? Ss 01:53 0:00 /usr/sbin/atd root 3829 0.0 0.3 18616 976 ? Ss 01:53 0:00 /usr/sbin/cron dovecot 3851 0.0 0.7 14148 1876 ? S 01:53 0:00 imap-login dovecot 3852 0.0 0.7 14148 1876 ? S 01:53 0:00 imap-login dovecot 3853 0.0 0.7 14148 1876 ? S 01:53 0:00 imap-login root 3875 0.0 0.2 3864 592 tty1 Ss+ 01:53 0:00 /sbin/getty 38400 tty1 root 3913 0.0 1.1 68272 3116 ? Ss 01:54 0:00 sshd: root@pts/0 postfix 3917 0.0 1.1 41248 2964 ? S 01:54 0:00 tlsmgr -l -t unix -u -c root 3924 0.0 0.7 18908 2080 pts/0 Ss 01:54 0:00 -bash root 3958 0.0 0.2 3864 592 ? Ss 01:55 0:00 /sbin/getty 38400 console root 4232 0.0 0.2 3944 608 pts/0 S 02:22 0:00 /bin/sh /usr/bin/mysqld_safe mysql 4271 0.1 5.0 124404 13244 pts/0 Sl 02:22 0:02 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --user=mysql --pi root 4272 0.0 0.2 5068 768 pts/0 S 02:22 0:00 logger -p daemon.err -t mysqld_safe -i -t mysqld root 4415 0.0 3.6 201880 9516 ? Ss 02:29 0:00 /usr/sbin/apache2 -k start www-data 4527 0.5 11.9 225720 31340 ? S 02:47 0:01 /usr/sbin/apache2 -k start www-data 4539 0.6 12.2 226492 32092 ? S 02:50 0:00 /usr/sbin/apache2 -k start www-data 4541 2.3 12.1 226492 31992 ? S 02:52 0:00 /usr/sbin/apache2 -k start root 4544 0.0 0.4 15064 1088 pts/0 R+ 02:53 0:00 ps aux 

有人可以帮我从这里出去吗?

你真的需要修剪你的服务。 更好的是, 您可能要考虑使用inetd或xinetd (以适用于您的安装为准)。 而不是留下几个服务启动和等待(和宝贵的内存)宝贵的CPU可能会更好地启动一个服务,当需要的性能命中。 而这正是inetd / xinetd的真正意图所在 – 它们旨在提供一种低内存的方式来允许服务按需启动,而不是一直挂着。 当然,你的里程可能会非常 – 你仍然可能以内存资源问题结束 – 但是,如果它让你暂时过了这个难堪的地方…

从哪里开始的后续行动(来自评论):

我会看看让Apache先移动。 减less最多的孩子数量只有2 – 除非你服务更多的用户比你自己,它没有什么帮助有3个正在运行的进程。

对于dovecot,特别是imap-auth进程,你只需要一个。 你也可以在inetd中运行它,虽然它看起来很薄。 如果您的服务器上没有使用IMAP或POP,请将其closures。 最糟糕的情况是,你回到阅读邮件的命令行 – 不漂亮,但function。

你也可以移动sshd,虽然我最初是在另一个端口号上做的,并确认它连接,然后再打开那个开关。

如果可能的话,考虑转移到Exim(这是Debian / Ubuntu设置的默认安装)。 我爱Postfix,我不使用Exim,但Debianselect它作为默认的原因 – 它只需要很less的资源,是相当安全的。

看看你的/ proc / sys / vm / swappiness设置( echo /proc/sys/vm/swappiness ),并确定它是太高还是太低。 更高的数字将有助于释放内存(通过提前交换到VM),但是如果数量太高,就会像疯了一样交换内存。 大多数安装说“60”,但对于低内存,它可能应该更像85.我不推荐100在您的设置。

最后,想一想“绿屏”。 你正在谈论使用在很久以前的系统上已经存在很长时间的技术(因为它们也是资源受到限制的)。 以这种方式做这件事有点古怪,但如果你可以在一个shell的客户端程序中完成你的工作,而不是启动服务,那么你的计算资源将进一步扩展。

密切关注正在运行的httpd进程的数量以及它们使用的内存量。 我认为你的web服务器正在交换,也许是因为MaxClients指令超出了你的服务器的最佳值。 您需要计算每个apache进程所需的平均RAM(根据ps的结果大约是30M),并将MaxClients的值设置为不超过可用RAM的总数,并为MySQL等其他进程提供足够的空间。

如果你在服务器开始减速的时候input命令'free',你会看到内存的使用情况,我敢打赌,就像HD所说的那样,你的服务器正在打开交换空间。 通过你的PS输出,你几乎在256M。

您可能希望将MaxSpareServers降至2甚至1.此外,还有其他方法可用于Apache,例如lighttpd ,它应该是轻量级的,而且占用的内存要less一些。

我有个人意见,我相信有些人会不同意,你不应该有一个在服务器上less于512M或1G RAM。

在256 MB的VPS下运行Apache,MySQL和PHP可能是一种紧张的游戏。 256 MB内存对于运行这些服务并不是很大,相反,它是非常非常less的。 首先要做的事情是考虑如果没有获得更好的VPS是一种select。

那么,如果得到一个更好的VPS不是一个选项,一个严重的忠告:完全沟通Apache!

Apache当时已经变得相当有记忆力,而且由于你没有大量的免费内存,所以Apache确实应该去。

取而代之的是Nginx,这也是相当强大的,但需要更less的资源才能交付!

当然,在使用PHP的情况下,您需要将PHP-FPM与Nginx一起安装,然后您需要正确configuration您的池。

您可能还想调整MySQL中的各种caching,这意味着可能会减小它们的大小!