编辑时添加,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版本为Ver 14.14 Distrib 5.5.25,适用于Linux(x86_64),使用readline 5.1.
我不认为Moodle软件是MySQL的重度用户.目前,只有大约10名教师可以访问它,当我使用bzip2转储和压缩整个数据库时,生成的转储大小小于1 MB.
几个月前我建立了这个系统. Apache服务器一直很稳定,但是MySQL已经崩溃了好几次.我试图从网上了解最佳配置,上次更改/etc/my.cnf文件时,我使用的文件是/usr/share/doc/mysql55-server-5.5.25/my-以MySQL为例的large.cnf.该文件表明它适用于具有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引擎,它只提供来自localhost的请求.除了系统管理员(我),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
有时我可以通过命令服务mysqld restart来重启MySQL,但有时候这个命令会失败并输出:mysqld dead但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,它看起来像这样(注释行被删除以节省空间):
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插件,the Attendance module and 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: [] out_of_memory+0x8b/0x203
Sep 26 08:00:51 [machine name] kernel: [] __alloc_pages+0x27f/0x308
Sep 26 08:00:51 [machine name] kernel: [] __do_page_cache_readahead+0xc8/0x1af
Sep 26 08:00:51 [machine name] kernel: [] filemap_nopage+0x14c/0x360
Sep 26 08:00:51 [machine name] kernel: [] __handle_mm_fault+0x444/0x144f
Sep 26 08:00:51 [machine name] kernel: [] _spin_lock_irqsave+0x9/0x14
Sep 26 08:00:51 [machine name] kernel: [] lock_timer_base+0x1b/0x3c
Sep 26 08:00:51 [machine name] kernel: [] do_page_fault+0xf72/0x131b
Sep 26 08:00:51 [machine name] kernel: [] sys_io_getevents+0x311/0x359
Sep 26 08:00:51 [machine name] kernel: [] timeout_func+0x0/0x10
Sep 26 08:00:51 [machine name] kernel: [] 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重新启动消息意味着MySQL已经崩溃了.系统日志表明它是oom_killer获得进程的原因.在您之前的回答中,您写道:“首先猜测:apache子进程无法运行.”在我看来,Apache也是明显的嫌疑人.
早些时候,我找到了文章Optimizing MySQL and Apache for Low Memory Usage, Part 1.为了配置Apache,作者建议:“首先,Apache.我的第一个声明是,如果你可以避免它,尝试.Lighttpd和thttpd都是非常好的没有多余的web服务器,你可以运行使用PHP的lighttpd.即使您运行的是高容量站点,也可以通过将静态内容(通常是图像和javascript文件)传递给轻量级,超快速的HTTPd服务器(如Lighttpd)来获得一些性能.“
我正在考虑接受作者的建议,并且同意我的客户,下周末,我将用服务器上的Lighttpd替换Apache.我希望能解决问题.很可能无法使用两个虚拟服务器.
我没想到在同一台机器上使用两个稳定,成熟的开源服务器,如MySQL和Apache,具有合理的内存量,这将是一件很麻烦的事情.