1 我的系统是Centos6.x 内存有32G,按理来说应该分配swap分区为8G但,但是由于历史原因,这个分配的swap分区为4G,但是这个机器上跑了mysql,占用了swap分区为1G多,导致swap消耗一般以上,zabbix监控一直在报警,所以要加大swap分区!前提是要把这个swap给下了,首先要将swap分区dump到内存中去!



步骤:

第一

sync 将系统缓冲区同步到磁盘中去

sync


第二:

echo "3" >  /proc/sys/vm/drop_caches

解释:是释放cache的操作!

As this is a non-destructive operation, and dirty objects are not freeable, the user should run "sync" first in order to make sure all cached objects are freed.


默认是0


第三:disable all swaps

swapoff -a


第四:用文件作为Swap分区


1.创建要作为swap分区的文件:增加1GB大小的交换分区,则命令写法如下,其中的count等于想要的块的数量(bs*count=文件大小)。

dd if=/dev/zero of=/root/swapfile bs=1G count=8


2.格式化为交换分区文件:

mkswap /root/swapfile

#建立swap的文件系统


3.启用交换分区文件:

swapon /root/swapfile

#启用swap文件


4.使系统开机时自启用,在文件/etc/fstab中添加一行:(将以前的注释掉)

/root/swapfile swap swap defaults 0 0



第五:还原使用cache的模式

echo "0" >  /proc/sys/vm/drop_caches




真实环境中,发现mysql自动重启了,3306有down了一会,且进程有重新启动过,然后是自动的,怀疑是swap被全部dump到内存中,而内存不足导致! 或者mysql没使用到swap会down,因为mysql配置原因吧,我也没有深入研究它!




阅读:

To free pagecache:


echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:


echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:


echo 3 > /proc/sys/vm/drop_caches

As this is a non-destructive operation, and dirty objects are not freeable, the user should run "sync" first in order to make sure all cached objects are freed.