暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

关于swappiness是否可以设置0?

原创 jieguo 2023-10-19
369

直接参考oracle官方建议吧:

建议在linux环境下设置1~10之间即可,默认的60太大了。

vm.swappiness=1

vm.swappiness - 0 or 1? (Doc ID 1917687.1)

Should I use 0 or 1 for vm.swappiness?
 

SOLUTION
These instructions are valid for Red Hat Enterprise Linux and Oracle Linux systems, but should be similar for other Linux distros.

There is some confusion about using a value of 0 or 1 for the vm.swappiness kernel parameter.

The confusion about vm.swappiness comes from the fact that in older Red Hat kernels, a value of 0 for vm.swappiness resulted in the minimal amount of swapping to avoid an out of memory condition. In newer kernels (as of RHEL kernel 2.6.32-303), a value of 0 will completely disable swap, but a value of 1 will provide the minimal amount of swapping to avoid an out of memory condition.

My general recommendation is to set vm.swappiness to a value of 1 for a dedicated MySQL server.  There is not much practical difference between a value of 0 and a value of 1 on older kernels, but 1 is the safe setting to avoid an OOM condition on newer kernels (assuming that some swap space is available, of course).

To set vm.swappiness to 1 and make it persistent across reboots:

echo "vm.swappiness=1" >> /etc/sysctl.conf

sysctl -p

如何创建swap?
How to Create a Swap Device on Oracle Linux (Doc ID 432991.1)

SOLUTION
1. System has free unpartitioned disk space
Run the fdisk(8) or parted(8) utility to identify disks with sufficient unused (unpartitioned) disk space. See respective manpages (man fdisk / man parted) about how to use the tools). Create a new partition of type swap (82).

Note: The maximum disk size that fdisk supports is 2TB. For devices larger that this use parted.
Run the mkswap(8) against the device/partition - optionally assign a swap label e.g.:

# mkswap -L swap1 /dev/sda5

Modify the /etc/fstab file to mount the new/additional swap device(s) on boot e.g.:

LABEL=SWAP-sda2         swap                    swap    defaults        0 0
...
/dev/sda5               swap                    swap    defaults        0 0

Run the swapon(8) command to enable all swap devices listed in the /etc/fstab file e.g.:

# swapon -a
# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda2                               partition       2104504 0       -1
/dev/sda5                               partition       2104504 0       -2
 

2. System has no free unpartitioned disk space
If no free, unpartitioned disk space exists, a swap file (rather than an entire device/partition) can be used for swap space as a temporary e.g.:


# mkdir /swap
# cd /swap

# dd if=/dev/zero of=/swap/swapfile01 bs=1M count=2048
2048+0 records in
2048+0 records out

# ls -l
total 2099208
-rw-r--r--  1 root root 2147483648 May 20  01:10 swapfile01

Filename                                Type            Size    Used    Priority
/dev/sda2                               partition       2104504 0       -1

# mkswap -L swap2 /swap/swapfile01
Setting up swapspace version 1, size = 2147479 kB


Modify the /etc/fstab file to mount the new/additional swap file(s) on boot e.g.:

LABEL=SWAP-sda2         swap                    swap    defaults        0 0
...
/swap/swapfile01        swap                    swap    defaults        0 0

Run the swapon(8) command to enable all swap devices listed in the /etc/fstab file e.g.:

# swapon -a
# swapon -s
Filename                                Type            Size    Used    Priority
/dev/sda2                               partition       2104504 0       -1
/swap/swapfile01                        file            2097144 0       -2

最后修改时间:2023-10-19 12:40:15
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论