间歇性MySQL崩溃,错误为“致命错误:无法为缓冲池分配内存”


40

在编辑中添加,2013-05-29:因为这是一个很长的问题和讨论,所以这里是问题和解决方案的简短摘要。我在小型Linux服务器(1 GB内存)上运行MySQL和Apache时遇到问题。Apache一直要求更多的内存,因此,操作系统总是杀死MySQL来重新获得其内存。 解决方案是用Lighttpd替换Apache。完成此操作后,服务器上的内存使用情况已经完全稳定了几个月,而且还没有发生任何类型的崩溃。编辑结束

我是小型虚拟服务器的初学者系统管理员。服务器的主要功能是运行用PHP编写的开源Moodle课程管理系统软件。它依赖于数据库(在本例中为MySQL)和Web服务器(在本例中为Apache)。

该服务器正在运行64位CentOS 5.8版(最终版),具有1 GB内存和200 GB磁盘,内核版本为2.6.18-308.8.2.el5xen。MySQL版本是版本14.14 Distrib 5.5.25,适用于使用readline 5.1的Linux(x86_64)。

我不认为Moodle软件是MySQL的重度用户。当前,只有大约十个教师可以访问它,并且当我使用bzip2转储并压缩整个数据库时,生成的转储大小小于1 MB。

我几个月前就建立了系统。Apache服务器一直保持稳定,但是MySQL崩溃了好几次。我尝试从Web上了解最佳配置,而上次更改/etc/my.cnf文件时,我/usr/share/doc/mysql55-server-5.5.25/my-large.cnf以MySQL随附的文件为例。该文件说这是针对具有512 MB内存的系统的,所以我认为使用与内存相关的配置参数对该系统来说是安全的。(我之前用较小的数字配置了MySQL的与内存相关的参数,我认为这可能导致崩溃。尽管仍然发生崩溃,但现在系统至少要快一些。)这些是当前的内容/etc/my.cnf

# /etc/my.cfg

# The main and only MySQL configuration file on [WEBSITE ADDRESS REDACTED].
# Last updated 2012-09-23 by Teemu Leisti.

# Most of the memory settings are set to be the same as the example setting file
# /usr/share/doc/mysql55-server-5.5.25/my-large.cnf, which is meant for systems
# with 512M of memory.  This server currently has twice that, i.e. 1G of memory,
# which should make these settings safe.


[client]
default_character_set           = utf8
port                            = 3306
socket                          = /var/lib/mysql/mysql.sock

[mysqld]
character_set_filesystem        = utf8
character_set_server            = utf8
datadir                         = /var/lib/mysql
innodb_additional_mem_pool_size = 20M
innodb_buffer_pool_size         = 256M # You can set .._buffer_pool_size up to
                                       # 50..80% of RAM, but beware of setting
                                       # memory usage too high
innodb_data_file_path           = ibdata1:10M:autoextend
innodb_data_home_dir            = /var/lib/mysql
innodb_flush_log_at_trx_commit  = 1
innodb_lock_wait_timeout        = 50
innodb_log_buffer_size          = 8M
innodb_log_file_size            = 64M # Set .._log_file_size to 25% of buffer
                                      # pool size
innodb_log_group_home_dir       = /var/lib/mysql
interactive_timeout             = 60
key_buffer_size                 = 256M
long_query_time                 = 10
max_allowed_packet              = 1M
max_connections                 = 30
port                            = 3306
query_cache_limit               = 2M # see http://emergent.urbanpug.com/?p=61
query_cache_size                = 16M
read_buffer_size                = 1M
read_rnd_buffer_size            = 4M
skip_networking                 # Only local processes need to use MySQL
skip_symbolic_links             # Disabling symbolic_links is recommended to
                                # prevent assorted security risks
slow_query_log_file             = /var/log/mysql-slow-queries.log
socket                          = /var/lib/mysql/mysql.sock
sort_buffer_size                = 1M
table_open_cache                = 256
thread_cache_size               = 8
thread_concurrency              = 2 #    = number of CPUs * 2
user                            = mysql
wait_timeout                    = 10

[mysqld_safe]
log_error                       = /var/log/mysqld.log
open_files_limit                = 4096
pid_file                        = /var/run/mysqld/mysqld.pid

[mysqldump]
quick
max_allowed_packet              = 16M

[mysql]
no-auto-rehash
# Remove the next comment character if you are not familiar with SQL
safe-updates

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

[mysqlhotcopy]
interactive-timeout

如您在配置中所见,安装程序使用InnoDB引擎,并且仅处理来自本地主机的请求。除了系统管理员(me),Moodle是MySQL的唯一用户。

当MySQL崩溃时,以下内容将始终写入日志文件/var/log/mysqld.log(当然,除了时间戳):

120926 08:00:51 mysqld_safe Number of processes running now: 0
120926 08:00:51 mysqld_safe mysqld restarted
120926  8:00:53 [Note] Plugin 'FEDERATED' is disabled.
120926  8:00:53 InnoDB: The InnoDB memory heap is disabled
120926  8:00:53 InnoDB: Mutexes and rw_locks use GCC atomic builtins
120926  8:00:53 InnoDB: Compressed tables use zlib 1.2.3
120926  8:00:53 InnoDB: Using Linux native AIO
120926  8:00:53 InnoDB: Initializing buffer pool, size = 256.0M
InnoDB: mmap(274726912 bytes) failed; errno 12
120926  8:00:53 InnoDB: Completed initialization of buffer pool
120926  8:00:53 InnoDB: Fatal error: cannot allocate memory for the buffer pool
120926  8:00:53 [ERROR] Plugin 'InnoDB' init function returned error.
120926  8:00:53 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
120926  8:00:53 [ERROR] Unknown/unsupported storage engine: InnoDB
120926  8:00:53 [ERROR] Aborting

120926  8:00:53 [Note] /usr/libexec/mysqld: Shutdown complete

120926 08:00:53 mysqld_safe mysqld from pid file /var/run/mysqld/mysqld.pid ended

有时我可以通过命令重启MySQL service mysqld restart,但有时该命令将失败,并显示以下输出:mysqld dead but subsys locked。在这些情况下,我唯一能想到的恢复情况是重启服务器,然后再重启MySQL。在这些情况下,输出如下所示:

120926 11:43:48 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
120926 11:43:48 [Note] Plugin 'FEDERATED' is disabled.
120926 11:43:48 InnoDB: The InnoDB memory heap is disabled
120926 11:43:48 InnoDB: Mutexes and rw_locks use GCC atomic builtins
120926 11:43:48 InnoDB: Compressed tables use zlib 1.2.3
120926 11:43:48 InnoDB: Using Linux native AIO
120926 11:43:48 InnoDB: Initializing buffer pool, size = 256.0M
120926 11:43:48 InnoDB: Completed initialization of buffer pool
120926 11:43:48 InnoDB: highest supported file format is Barracuda.
InnoDB: The log sequence number in ibdata files does not match
InnoDB: the log sequence number in the ib_logfiles!
120926 11:43:48  InnoDB: Database was not shut down normally!
InnoDB: Starting crash recovery.
InnoDB: Reading tablespace information from the .ibd files...
InnoDB: Restoring possible half-written data pages from the doublewrite
InnoDB: buffer...
120926 11:43:51  InnoDB: Waiting for the background threads to start
120926 11:43:52 InnoDB: 1.1.8 started; log sequence number 466807107
120926 11:43:52 [Note] Event Scheduler: Loaded 0 events
120926 11:43:52 [Note] /usr/libexec/mysqld: ready for connections.
Version: '5.5.25'  socket: '/var/lib/mysql/mysql.sock'  port: 0  MySQL Community Server (GPL)

这是命令free -m当前输出的内容:

# free -m
             total       used       free     shared    buffers     cached
Mem:          1024        869        154          0         70        153
-/+ buffers/cache:        644        379
Swap:            0          0          0

通常,“免费”列在50到100 MB之间。

命令的输出ulimit -a

# ulimit -a
core file size          (blocks, -c) 0
data seg size           (kbytes, -d) unlimited
scheduling priority             (-e) 0
file size               (blocks, -f) unlimited
pending signals                 (-i) 8192
max locked memory       (kbytes, -l) 32
max memory size         (kbytes, -m) unlimited
open files                      (-n) 1024
pipe size            (512 bytes, -p) 8
POSIX message queues     (bytes, -q) 819200
real-time priority              (-r) 0
stack size              (kbytes, -s) 10240
cpu time               (seconds, -t) unlimited
max user processes              (-u) 8192
virtual memory          (kbytes, -v) unlimited
file locks                      (-x) unlimited

我没有更改Moodle的任何设置或代码文件,除了/var/www/html/moodle/config.php,它看起来像这样(删除注释行以节省空间):

<?php
unset($CFG);  // Ignore this line
global $CFG;  // This is necessary here for PHPUnit execution
$CFG = new stdClass();
$CFG->dbtype    = 'mysqli';           // 'pgsql', 'mysqli', 'mssql', 'sqlsrv' or 'oci'
$CFG->dblibrary = 'native';           // 'native' only at the moment
$CFG->dbhost    = 'localhost';        // eg 'localhost' or 'db.isp.com' or IP
$CFG->dbname    = 'moodle';           // database name, eg moodle
$CFG->dbuser    = 'moodleuser';       // your database username
$CFG->dbpass    = '[REDACTED]';       // your database password
$CFG->prefix    = 'moodle_';          // prefix to use for all table names
$CFG->dboptions = array(
    'dbpersist' => false,       // should persistent database connections be
                                //  used? set to 'false' for the most stable
                                //  setting, 'true' can improve performance
                                //  sometimes
    'dbsocket'  => true,        // should connection via UNIX socket be used?
                                //  if you set it to 'true' or custom path
                                //  here set dbhost to 'localhost',
                                //  (please note mysql is always using socket
                                //  if dbhost is 'localhost' - if you need
                                //  local port connection use '127.0.0.1')
    'dbport'    => '',          // the TCP port number to use when connecting
                                //  to the server. keep empty string for the
                                //  default port
);
$CFG->passwordsaltmain = '[REDACTED]';
$CFG->wwwroot   = 'http://[REDACTED]';
$CFG->dataroot  = '/var/moodledata';
$CFG->directorypermissions = 02777;
$CFG->admin = 'admin';
date_default_timezone_set('Europe/Helsinki');
$CFG->disableupdatenotifications = true;
require_once(dirname(__FILE__) . '/lib/setup.php'); // Do not edit

(但是,我已经安装了两个Moodle插件,Attendance模块和block,但是我看不到它们如何与这个问题有关。)

即使在/etc/my.cnf一周前我更新到当前状态后,MySQL也因上述症状而崩溃了几次。作为数据库管理的初学者,并且在对这个问题进行了大量的搜索之后,我对下一步的工作一无所知。有什么建议么?我应该发布更多配置数据吗?

加上编辑:

文件的内容/var/log/messages.1是:

Sep 23 04:02:18 [machine name] syslogd 1.4.1: restart.
Sep 26 08:00:51 [machine name] kernel: mysqld invoked oom-killer: gfp_mask=0x201d2, order=0, oomkilladj=0
Sep 26 08:00:51 [machine name] kernel:
Sep 26 08:00:51 [machine name] kernel: Call Trace:
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff802c1bd5>] out_of_memory+0x8b/0x203
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff8020fa49>] __alloc_pages+0x27f/0x308
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff802139c9>] __do_page_cache_readahead+0xc8/0x1af
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff8021423a>] filemap_nopage+0x14c/0x360
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff80208e9d>] __handle_mm_fault+0x444/0x144f
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff80263929>] _spin_lock_irqsave+0x9/0x14
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff8023f468>] lock_timer_base+0x1b/0x3c
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff80266d94>] do_page_fault+0xf72/0x131b
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff802e5f4f>] sys_io_getevents+0x311/0x359
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff802e4e56>] timeout_func+0x0/0x10
Sep 26 08:00:51 [machine name] kernel:  [<ffffffff8025f82b>] error_exit+0x0/0x6e
Sep 26 08:00:51 [machine name] kernel:
Sep 26 08:00:51 [machine name] kernel: Mem-info:
Sep 26 08:00:51 [machine name] kernel: DMA per-cpu:
Sep 26 08:00:51 [machine name] kernel: cpu 0 hot: high 0, batch 1 used:0
Sep 26 08:00:51 [machine name] kernel: cpu 0 cold: high 0, batch 1 used:0
Sep 26 08:00:51 [machine name] kernel: DMA32 per-cpu:
Sep 26 08:00:51 [machine name] kernel: cpu 0 hot: high 186, batch 31 used:117
Sep 26 08:00:51 [machine name] kernel: cpu 0 cold: high 62, batch 15 used:53
Sep 26 08:00:51 [machine name] kernel: Normal per-cpu: empty
Sep 26 08:00:51 [machine name] kernel: HighMem per-cpu: empty
Sep 26 08:00:51 [machine name] kernel: Free pages:        7256kB (0kB HighMem)
Sep 26 08:00:51 [machine name] kernel: Active:241649 inactive:0 dirty:0 writeback:0 unstable:0 free:1814 slab:4104 mapped-file:1153 mapped-anon:240592 pagetables:3298
Sep 26 08:00:51 [machine name] kernel: DMA free:3268kB min:32kB low:40kB high:48kB active:0kB inactive:0kB present:9068kB pages_scanned:0 all_unreclaimable? yes
Sep 26 08:00:51 [machine name] kernel: lowmem_reserve[]: 0 994 994 994
Sep 26 08:00:51 [machine name] kernel: DMA32 free:3988kB min:4016kB low:5020kB high:6024kB active:966596kB inactive:0kB present:1018080kB pages_scanned:6327262 all_unreclaimable? yes
Sep 26 08:00:52 [machine name] kernel: lowmem_reserve[]: 0 0 0 0
Sep 26 08:00:52 [machine name] kernel: Normal free:0kB min:0kB low:0kB high:0kB active:0kB inactive:0kB present:0kB pages_scanned:0 all_unreclaimable? no
Sep 26 08:00:52 [machine name] kernel: lowmem_reserve[]: 0 0 0 0
Sep 26 08:00:52 [machine name] kernel: HighMem free:0kB min:128kB low:128kB high:128kB active:0kB inactive:0kB present:0kB pages_scanned:0 all_unreclaimable? no
Sep 26 08:00:52 [machine name] kernel: lowmem_reserve[]: 0 0 0 0
Sep 26 08:00:52 [machine name] kernel: DMA: 1*4kB 2*8kB 1*16kB 1*32kB 2*64kB 2*128kB 1*256kB 1*512kB 2*1024kB 0*2048kB 0*4096kB = 3268kB
Sep 26 08:00:52 [machine name] kernel: DMA32: 17*4kB 2*8kB 2*16kB 1*32kB 0*64kB 0*128kB 1*256kB 1*512kB 1*1024kB 1*2048kB 0*4096kB = 3988kB
Sep 26 08:00:52 [machine name] kernel: Normal: empty
Sep 26 08:00:52 [machine name] kernel: HighMem: empty
Sep 26 08:00:52 [machine name] kernel: 1214 pagecache pages
Sep 26 08:00:52 [machine name] kernel: Swap cache: add 0, delete 0, find 0/0, race 0+0
Sep 26 08:00:52 [machine name] kernel: Free swap  = 0kB
Sep 26 08:00:52 [machine name] kernel: Total swap = 0kB
Sep 26 08:00:52 [machine name] kernel: Free swap:            0kB
Sep 26 08:00:52 [machine name] kernel: 262144 pages of RAM
Sep 26 08:00:52 [machine name] kernel: 8320 reserved pages
Sep 26 08:00:52 [machine name] kernel: 22510 pages shared
Sep 26 08:00:52 [machine name] kernel: 0 pages swap cached
Sep 26 08:00:52 [machine name] kernel: Out of memory: Killed process 1371, UID 27, (mysqld).

然后在11:42重新启动相关的行。

在编辑#2上添加:

我试图对迈克尔的答案发表评论,但是我对评论的字符限制不满意,所以我在这里回答。

迈克尔,谢谢您的回答。我刚刚编辑了问题,以包含崩溃时计算机系统日志的内容。(CentOS似乎将其称为系统日志/var/log/messages。)

是的,MySQL和系统日志与您链接到的问题中的日志几乎相同。现在,您提到它了,很明显该mysql restarted消息表明MySQL已经崩溃。系统日志表明oom_killer正是该过程。在您先前的答案中,您写道:“第一个猜测:apache子进程运行amok。” 在我看来,Apache在这里也是明显的嫌疑人。

早些时候,我发现了针对低内存使用优化MySQL和Apache的文章,第1部分。对于配置Apache,作者建议:“首先,Apache。我的第一句话是,如果可以避免的话,请尝试。Lighttpd和thttpd都是非常不错的Web服务器,您可以使用PHP运行lighttpd。即使您“在运行大量网站的情况下,您可以将静态内容(通常是图像和javascript文件)传递到轻量级,超快速的HTTPd服务器(例如Lighttpd),从而获得重要的性能。”

我正在考虑采纳作者的建议,并已与我的客户达成协议,下周末,我将在服务器上用Lighttpd替换Apache。我希望能解决问题。使用两个虚拟服务器极有可能是不可能的。

我没有想到在同一台计算机上使用两个稳定,成熟的开源服务器(例如MySQL和Apache)并具有合理的内存量会带来麻烦。

Answers:


36

请查看最近这个问题的回答。我相信情况是一样的。

此时不要更改MySQL配置,因为MySQL并不是问题,这只是问题的症状...这就是说,您似乎拥有一个内存量少且交换空间为零的系统。

您的服务器没有崩溃,因为无法为缓冲池分配内存。您的服务器崩溃了……然后由于系统内存不可用而无法随后重新启动。在mysql启动时,系统会请求为InnoDB缓冲池配置的所有内存。

当您看到此日志消息时...

120926 08:00:51 mysqld_safe Number of processes running now: 0

...您的服务器已经死了。如果在此之前未记录任何内容,则不会记录有关第一次崩溃的任何内容。后续日志来自自动尝试重新启动后的日志。

检查您的系统日志,您应该找到消息,指出内核由于极端的内存不足状况而终止了进程。

如果可能的话,第一步可能是添加一些交换空间和/或分配RAM。

如果这不可能,那么您实际上可以考虑在配置中减小 innodb-buffer-pool的大小。(我从没想过我会听到自己这么说)。只要您的数据库很小并且流量很小,您可能就不需要这么大的缓冲池...而且由于InnoDB缓冲池内存在启动时是否全部分配了,是否需要,这将释放您的一些内存。系统的内存,以满足其他需求。(仅当整个服务器专用于MySQL时,才建议使用总RAM的75%至80%来确定缓冲池的大小。)

第2步将检查Apache的派生模型,以及您可能需要在配置中进行不同的操作以防止它淹没您的服务器。Apache子进程的数量或内存需求的不受控制的增长很可能会引发一系列事件,导致内核杀死MySQL,从而试图避免整个服务器完全崩溃。

根据您拥有的灵活性,您甚至可以考虑为Apache和MySQL使用两个单独的虚拟机。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.