MySQL 预留了太多 RAM

MySQL 预留了太多 RAM

我有一个便宜的 VPS,内存为 128Mb,最大内存为 256Mb。MySQL 启动时会保留大约 110Mb,但实际使用的内存不超过 20Mb。我的 VPS 控制面板显示我使用了 127Mb(我还运行了 nginx 和 sphinx),我知道它显示了保留的内存,但是当我的内存超过 128Mb 时,我的 VPS 每 4 小时会自动重启一次。所以我想强制 MySQL 保留较少的内存。我该怎么做?我对 my.conf 做了一些调整,但效果不大。

顶部输出:

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    1 root      15   0  2156  668  572 S  0.0  0.3   0:00.03 init
11311 root      15   0 11212  356  228 S  0.0  0.1   0:00.00 vzctl
11312 root      18   0  3712 1484 1248 S  0.0  0.6   0:00.01 bash
11347 root      18   0  2284  916  732 R  0.0  0.3   0:00.00 top
13978 root      17  -4  2248  552  344 S  0.0  0.2   0:00.00 udevd
14262 root      15   0  1812  564  472 S  0.0  0.2   0:00.03 syslogd
14293 sphinx    15   0 11816 1172  672 S  0.0  0.4   0:00.07 searchd
14305 root      25   0  7192 1036  636 S  0.0  0.4   0:00.00 sshd
14321 root      25   0  2832  836  668 S  0.0  0.3   0:00.00 xinetd
15389 root      18   0  3708 1300 1132 S  0.0  0.5   0:00.00 mysqld_safe
15441 mysql     15   0  113m  16m 4440 S  0.0  6.4   0:00.15 mysqld
15489 root      21   0 13056 1456  340 S  0.0  0.6   0:00.00 nginx
15490 nginx     18   0 13328 2388  992 S  0.0  0.9   0:00.06 nginx
15507 nginx     25   0 19520 5888 4244 S  0.0  2.2   0:00.00 php-cgi
15508 nginx     18   0 19636 4876 2748 S  0.0  1.9   0:00.12 php-cgi
15509 nginx     15   0 19668 4872 2716 S  0.0  1.9   0:00.11 php-cgi
15518 root      18   0  4492 1116  568 S  0.0  0.4   0:00.01 crond

MySQL 调整器:

 >>  MySQLTuner 1.0.1 - Major Hayden <[email protected]>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering
Please enter your MySQL administrative login: root
Please enter your MySQL administrative password:

-------- General Statistics --------------------------------------------------
[--] Skipped version check for MySQLTuner script
[OK] Currently running supported MySQL version 5.0.77
[OK] Operating on 32-bit architecture with less than 2GB RAM

-------- Storage Engine Statistics -------------------------------------------
[--] Status: -Archive -BDB -Federated +InnoDB -ISAM -NDBCluster
[--] Data in InnoDB tables: 1M (Tables: 1)
[OK] Total fragmented tables: 0

-------- Performance Metrics -------------------------------------------------
[--] Up for: 38m 43s (37 q [0.016 qps], 20 conn, TX: 4M, RX: 3K)
[--] Reads / Writes: 100% / 0%
[--] Total buffers: 28.1M global + 832.0K per thread (100 max threads)
[OK] Maximum possible memory usage: 109.4M (42% of installed RAM)
[OK] Slow queries: 0% (0/37)
[OK] Highest usage of available connections: 1% (1/100)
[OK] Key buffer size / total MyISAM indexes: 128.0K/64.0K
[OK] Query cache efficiency: 42.1% (8 cached / 19 selects)
[OK] Query cache prunes per day: 0
[!!] Temporary tables created on disk: 27% (3 on disk / 11 total)
[!!] Thread cache is disabled
[OK] Table cache hit rate: 57% (8 open / 14 opened)
[OK] Open file limit used: 1% (12/1K)
[OK] Table locks acquired immediately: 100% (22 immediate / 22 locks)
[!!] Connections aborted: 10%
[OK] InnoDB data size / buffer pool: 1.5M/8.0M

-------- Recommendations -----------------------------------------------------
General recommendations:
    MySQL started within last 24 hours - recommendations may be inaccurate
    Enable the slow query log to troubleshoot bad queries
    When making adjustments, make tmp_table_size/max_heap_table_size equal
    Reduce your SELECT DISTINCT queries without LIMIT clauses
    Set thread_cache_size to 4 as a starting value
    Your applications are not closing MySQL connections properly
Variables to adjust:
    tmp_table_size (> 32M)
    max_heap_table_size (> 16M)
    thread_cache_size (start at 4)

我认为如果我按照 MySQLtuner 所说的操作,MySQL 将会使用更多的 RAM。

答案1

Linux 上有几种机制可用于严格限制进程可用的内存。

在较新的系统上,您可以使用 cgroups,如果可用的话,这可能是最好的解决方案。(使用包中的工具libcgroup自动将 mysql 守护进程分类到内存控制组或memory.limit_in_bytes = 64M;其他。

在较旧的系统上,如果 mysql 守护进程由脚本启动,您可以简单地使用它ulimit来限制可用的内存 -ulimit -v 65536在脚本中实际启动守护进程的位置上方放置一个语句。

话虽如此,但对于现代软件来说,这确实超出了你的承受范围。你可能只是需要比你拥有的更多的 RAM。一个选择可能是切换到 SQLite 而不是 MySQL — — 那么你根本就不需要运行单独的守护进程了。

答案2

使用调谐引物或者MySQLTuner

# wget http://mysqltuner.pl/mysqltuner.pl
--2011-01-31 06:49:46--  http://mysqltuner.pl/mysqltuner.pl
Resolving mysqltuner.pl... 174.143.142.58
Connecting to mysqltuner.pl|174.143.142.58|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 39054 (38K) [text/plain]
Saving to: `mysqltuner.pl'

100%[==============================================================================================================================>] 39,054      54.6K/s   in 0.7s    

2011-01-31 06:49:49 (54.6 KB/s) - `mysqltuner.pl' saved [39054/39054]

# perl mysqltuner.pl

 >>  MySQLTuner 1.0.1 - Major Hayden <[email protected]>
 >>  Bug reports, feature requests, and downloads at http://mysqltuner.com/
 >>  Run with '--help' for additional options and output filtering
Please enter your MySQL administrative login: root
Please enter your MySQL administrative password: 

设置 max_connections = 15

128 MB 对于 nginx、php、Sphinx 和 MySQL 来说太少了。

答案3

尽量不要使用 InnoDB,而要使用 MyISAM。添加skip-innodb到 my.cnf 将节省大量内存

相关内容