#出此下策# 为VPS增加Swap文件(可用于阿里云)

linux_vmvps博客增加了50X监控重启的脚本之后,总算对502和504的错误有了一定的控制,但是今天又有朋友反应出现了“数据库连接错误”的信息,通过排查kern.log,发现如下的信息,

Jan  8 02:01:05 VMVPS kernel: [232387.072765] Out of memory: Kill process 1896 (mysqld) score 265 or sacrifice child
Jan  8 02:01:05 VMVPS kernel: [232387.093931] Killed process 1896 (mysqld) total-vm:966156kB, anon-rss:134124kB, file-rss:0kB

显然,是因为内存不够用,于是Linux杀掉了MySQL的进程。当然重启一下自然也就解决了,但是“治标不治本”。

微魔想了一下,决定一个“下策”,希望能够对阿里云这512M的配置能够有点儿帮助。为什么称之为“下策”呢?主要是因为从读取速度上来讲:物理内存>Swap分区>Swap文件;可见本文的创建Swap文件的方式算是下下之策,但是好处也是显而易见的,不需要额外的进行分区操作。

顺便吐槽一下阿里云这512M套餐的配置,内存上真心的不够用,希望大家入手时多多谨慎吧,其实内存也不贵…

为VPS增加Swap文件(可用于阿里云)

1.创建Swap文件(两种方法,推荐前者,因为用DD创建的话会很慢!)

fallocate -l 1024M /swapfile

或者

dd if=/dev/zero of=/swapfile bs=1M count=1024

2.设置Swap权限

chmod 600 /swapfile

3.格式化文件为Swap

mkswap /swapfile

4.激活Swap文件

swapon /swapfile

5.编辑/etc/fstab文件,让系统重启后自动挂载swap文件

/swapfile none swap defaults 0 0

可以用free -m测试一下

创建swap前

             total       used       free     shared    buffers     cached
Mem:           494        488          6         21         51        208
-/+ buffers/cache:        227        267
Swap:            0          0          0

创建swap后

             total       used       free     shared    buffers     cached
Mem:           494        483         11         21         68         96
-/+ buffers/cache:        318        176
Swap:         1023          0       1023

删除Swap文件

1.关闭Swap

swapoff -a

2.删除Swap文件

rm -f /swapfile

这个方法有没有用微魔还处于围观的状态,目测了很久swap都没有占用,具体效果日后和大家分享吧~

2015年1月9日更新

增加Swap的使用级别

经过楼下网友的热心提示

1.查看当前系统使用Swap的使用级别(0-100),0代表只有在万不得已时才会使用swap,而100则会经常使用swap

cat /proc/sys/vm/swappiness

微魔返回的数值是0,也是为什么上面说swap没有使用的原因

2.设置swap使用级别(为10为例)

sysctl vm.swappiness=10

3.设置开机自动设置

修改/etc/sysctl.conf文件

vm.swappiness=10

猜你 喜欢

关于作者: 微魔

小微魔,大智慧!

多条评论

  1. enn……
    How To Configure Swappiness
    The operating system kernel can adjust how often it relies on swap through a configuration parameter known as swappiness.

    To find the current swappiness settings, type:

    cat /proc/sys/vm/swappiness
    60
    Swapiness can be a value from 0 to 100. Swappiness near 100 means that the operating system will swap often and usually, too soon. Although swap provides extra resources, RAM is much faster than swap space. Any time something is moved from RAM to swap, it slows down.

    A swappiness value of 0 means that the operating will only rely on swap when it absolutely needs to. We can adjust the swappiness with the sysctl command:

    sysctl vm.swappiness=10
    vm.swappiness=10
    If we check the system swappiness again, we can confirm that the setting was applied:

    cat /proc/sys/vm/swappiness
    10
    To make your VPS automatically apply this setting every time it boots up, you can add the setting to the /etc/sysctl.conf file:

    sudo nano /etc/sysctl.conf
    # Search for the vm.swappiness setting. Uncomment and change it as necessary.
    vm.swappiness=10
    By Etel Sverdlov

    1. “进程监控+自动重启”也都是治标不治本(我也加了)。swap加上也不能说没效果吧,反正一直有80M左右的占用。我后来发现这502主要是被攻击造成的,把可以的ip都给封了,算好了。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注