Tee*_*sti 39 mysql innodb mysql-5.5 crash
添加于编辑,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,适用于使用 readline 5.1 的 Linux (x86_64)。
我不认为 Moodle 软件是 MySQL 的重度用户。目前,只有大约十位教师可以访问它,当我使用 bzip2 转储和压缩整个数据库时,生成的转储大小小于 1 MB。
我几个月前设置了系统。Apache服务器一直稳定,但MySQL崩溃了几次。尝试从网上学习了最佳配置,上次改/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
Run Code Online (Sandbox Code Playgroud)
正如您在配置中看到的,设置使用 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
Run Code Online (Sandbox Code Playgroud)
有时我可以通过命令重新启动 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)
Run Code Online (Sandbox Code Playgroud)
这是命令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
Run Code Online (Sandbox Code Playgroud)
通常“免费”列在 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
Run Code Online (Sandbox Code Playgroud)
我没有更改任何 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
Run Code Online (Sandbox Code Playgroud)
(但是,我已经安装了两个 Moodle 插件,即出勤模块和块,但我不知道它们如何与此问题有关。)
即使在我/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).
Run Code Online (Sandbox Code Playgroud)
然后是与 11:42 重启相关的行。
添加编辑#2:
我试图对迈克尔的回答发表评论,但我违反了评论的字数限制,所以我在这里回答。
谢谢你的回答,迈克尔。我刚刚编辑了我的问题,以包含崩溃时机器系统日志的内容。(CentOS 似乎称其为 system log /var/log/messages。)
是的,MySQL 和系统日志与您链接到的问题中的日志几乎相同。既然你提到了它,很明显这条mysql restarted消息意味着 MySQL 已经崩溃了。系统日志表明它oom_killer是获取进程的原因。在您之前的回答中,您写道:“首先猜测:apache 子进程运行异常。” 在我看来,Apache 也是这里的明显嫌疑人。
早些时候,我找到了文章Optimizing MySQL and Apache for Low Memory Usage, Part 1。对于配置 Apache,作者建议:“首先,Apache。我的第一个声明是,如果您可以避免它,请尝试。Lighttpd 和 thttpd 都是非常好的无装饰网络服务器,并且您可以使用 PHP 运行 lighttpd。即使您'正在运行一个高容量站点,您可以通过将静态内容(通常是图像和 javascript 文件)传递到轻量级、超快速的 HTTPd 服务器(例如 Lighttpd)来获得一些性能。”
我正在考虑接受作者的建议,并与我的客户达成一致,下周末我将在服务器上用 Lighttpd 替换 Apache。我希望这能解决问题。使用两个虚拟服务器很可能是不可能的。
没想到在同一台机器上使用两个稳定、成熟的开源服务器,比如MySQL和Apache,内存合理,会这么麻烦。
Mic*_*bot 36
此时不要更改您的 MySQL 配置,因为 MySQL 不是问题——这只是问题的一个症状……这就是您的系统似乎具有少量内存和零交换空间。
您的服务器没有崩溃,“因为”无法为缓冲池分配内存。您的服务器崩溃了……然后由于系统内存不可用而无法随后重新启动。在 mysql 启动时从系统请求为 InnoDB 缓冲池配置的所有内存。
当您看到此日志消息时...
120926 08:00:51 mysqld_safe Number of processes running now: 0
Run Code Online (Sandbox Code Playgroud)
...你的服务器已经死了。如果在此之前没有记录任何内容,则不会记录有关第一次崩溃的任何信息。后续日志来自自动尝试重新启动之后。
检查您的系统日志,您应该会发现内核因极端内存不足而寻找要杀死的进程的消息。
如果可能的话,第 1 步可能是添加一些交换空间和/或分配 RAM。
如果这是不可能的,您实际上可能会考虑减少配置中的 innodb-buffer-pool 大小。(我从没想过我真的会听到自己这么说)。只要你的数据库很小并且你的流量很小,你可能不需要那么大的缓冲池......而且由于 InnoDB 缓冲池内存在启动时全部分配,无论是否需要,这将释放你的一些系统的内存,用于需要它的任何其他东西。(用于调整缓冲池大小的 75% 到 80% 的总 RAM 建议仅在整个服务器专用于 MySQL 时才适用。)
第 2 步是查看 Apache 的分叉模型以及您可能需要在配置中做哪些不同的事情以防止它使您的服务器不堪重负。Apache 子进程的数量或内存需求不受控制的增长很可能会引发一连串事件,导致内核杀死 MySQL 以试图避免整个服务器的完全崩溃。
根据您拥有的灵活性,您甚至可以考虑为 Apache 和 MySQL 使用两个独立的虚拟机。
| 归档时间: |
|
| 查看次数: |
56244 次 |
| 最近记录: |