CentOS configuration swap exchange area method

CentOS is the most stable server version in the linux world. rhas5 has always had a great application area. Freebsd has been the focus of attention before. Because of the application needs, red hat has too many advantages especially in cooperation with mysql and oracle.

Mipu Technology, as a fast-growing entrepreneurial enterprise, has purchased many Alibaba Cloud and Amazon Cloud, so it will configure the swap exchange area. The specific method is as follows.

Use CentOS to replace the use of red hat as, because CentOS5 uses the source code of rhas5 to compile and distribute after removing the redhat trademark and icon. It is the most source-compiled version. I use CentOS 5.2

When installing centos 5.2, forget to set the swap partition. In the case of small memory, increasing swap is more effective.

Before configuring swap, check swap and disk space through free and df, the purpose is to compare the changes after configuring swap.

free -m

[ root@ithomer ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1008        955         53          0         17        168

df -h

[ root@ithomer ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       20G   15G  4.2G  79% /
tmpfs           505M     0  505M   0% /dev/shm
/dev/xvdb       9.9G  5.1G  4.4G  54% /home/data

The first step is to create a file block

[ root@ithomer ~]# cd /var (Enter a folder)

[ root@ithomer var]# dd if=/dev/zero of=swapfile bs=1024 count=1024000 (Create a 1GB swap, usually twice the memory)

1024000+0 records in
1024000+0 records out
1048576000 bytes (1.0 GB) copied, 23.3843 s, 44.8 MB/s

[ root@ithomer var]# /sbin/mkswap swapfile (create swap file, actually dd has already been created, no need to create it here)

mkswap: swapfile: warning: don’t erase bootbits sectors
        on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=e878f535-ea6f-4516-8a34-f1458951aa36

[ root@ithomer var]# ll swapfile

[ root@ithomer var]# /sbin/swapon swapfile (activate swap file)

[ root@ithomer var]# /sbin/swapon -s (check whether swap is correct)

Filename                Type        Size    Used    Priority
/var/swapfile                           file        1023992    0    -1

[ root@ithomer var]# vim /etc/fstab (Add to the fstab file to automatically start the system when it boots)

Add the following at the end:

/var/swapfile swap swap defaults 0 0

[ root@ithomer var]# reboot

After restarting, the top command to view, there is the size of the swap partition

free -m

[ root@ithomer ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          1008        528        480          0         25        128

df -h

[ root@ithomer ~]# df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       20G   16G  3.2G  84% /
tmpfs           505M     0  505M   0% /dev/shm
/dev/xvdb       9.9G  5.1G  4.4G  54% /home/data

According to the comparison between free and df above and before configuring swap, it is found that swap has increased from 0 to 999 (about 1GB), and the system main disk (/dev/xvda1) has been reduced from 4.2G to 3.2G About 1GB, that is, the swap space comes from the system main disk (/dev/xvda1)

Delete swapfile file

$ rm -f swapfile 
rm: cannot remove ‘swapfile’: Permission denied

When deleting the swapfile file, it prompts that permission is forbidden. The reason is that the swapfile has been mounted and is in use. You need to uninstall it first. Comment out the #

/var/swapfile swap     swap     defaults     0   0

/var/swapfile2 swap     swap     defaults     0   0

Then, sudo -s switch to root authority, rm -f swapfile is deleted successfully

/etc/fstab file parameters

cat /etc/fstab

LABEL=/     /           ext4    defaults,noatime  1   1
tmpfs       /dev/shm    tmpfs   defaults        0   0
devpts      /dev/pts    devpts  gid=5,mode=620  0   0
sysfs       /sys        sysfs   defaults        0   0
proc        /proc       proc    defaults        0   0
/var/swapfile swap     swap     defaults     0   0
/var/swapfile2 swap     swap     defaults     0   0

df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       16G  6.2G  9.5G  40% /
devtmpfs        489M   56K  489M   1% /dev
tmpfs           498M     0  498M   0% /dev/shm

Important information related to partitions is stored in fstab. Each row is a partition record, and each row can be divided into six parts:

  1. The first item is the physical location of the storage device you want to mount, such as/or /var/swapfile2

  2. The second item is the directory location you want to add it to, such as /home or /, which is actually the hook point prompted during installation

  3. The third item is the so-called local filesystem, which contains the following formats: such as ext, ext2, msdos, iso9660, nfs, swap, etc., or such as ext2, please refer to the description of /prco/filesystems

  4. The fourth item is the state you want to set when you mount, such as ro (read-only) or defaults (including other parameters such as rw, suid, exec, auto, nouser, async), see mount nfs

  5. The fifth item is to provide the DUMP function, whether it is necessary to back up the BACKUP flag when the system DUMP, its default value is 0

  6. The sixth item is to set whether this filesystem should be checked at boot time. Except for the root filesystem whose necessary check is 1, all others can be set as needed. The default value is 0

Problem and solution

1、 Format and start swap file

Next, execute the mkswap command to format the myswap file into the s file system, so that the system can use it, switch to the /tmp directory, and execute the following command:

mkswap swapfree (format the file to swap file format)

setting up swapspace version 1 , size = 262144 KB

swapon /tmp/swapfree (#Start swap partition)

To stop using the newly created swap file, just execute the swapoff /tmp/swapfree command. If the swap file is no longer in use, you can delete this file.

2、 Check swap

#swapon -s

3、 Automatically start newly added swap partition when booting

If you have to execute the swapon command to start the swap partition or file after every boot, this is too troublesome. At this time, you can use a text editor to add a line to the /etc/fstab file, so that the swap partition and files will be automatically started at boot time:

/dec/hdb5 swap swap defaults 0 0 (start this swap partition at boot time)

/tmp/swapfree swap swap defaults 0 0 (start this swap file at boot time).

Swap space size:

Under normal circumstances, the swap space should be greater than or equal to the size of the physical memory, and the minimum should not be less than 64M. Generally, the size of the swap space should be 2-2.5 times that of the physical memory. But according to different applications, there should be different configurations: if it is a small desktop system, only a small swap space is needed, while a large server system needs different sizes of swap space depending on the situation. Especially for database servers and web servers, as the amount of access increases, the requirements for swap space will also increase. Please refer to the description of each server product for specific configuration.

Amount of swap:

The number of Swap partitions also has a great impact on performance. Because the operation of Swap exchange is the operation of disk IO, if there are multiple Swap exchange areas, the allocation of Swap space will be operated on all Swaps in turn, which will greatly balance the IO load and accelerate the speed of Swap exchange. If there is only one exchange area, all exchange operations will make the exchange area very busy, making the system wait most of the time, and the efficiency is very low. Use performance monitoring tools to find that the CPU is not very busy at this time, but the system is slow. This shows that the bottleneck is IO, and the problem cannot be solved by increasing the speed of the CPU.

Recommended Posts

CentOS configuration swap exchange area method
CentOS7 system increase swap operation method example
CentOS8 yum/dnf configuration method of domestic sources
Linux CentOS regularly run script configuration method
CentOS7.0 network configuration
CentOS 7.0 network configuration
CentOS7 basic configuration
Centos MySQL8 configuration
Xfs configuration on centos7
Centos kernel compilation configuration
Mysql8.0.15 installation configuration (centos7)
Docker CentOS installation method
Centos mysql installation and configuration
Centos7.4 deployment configuration Elasticsearch5.6 cluster
Centos7.6 method to install Tomcat-8.5.39
Centos7 installation and configuration prometheus
CentOS 7 installation and configuration PPTP
CentOS installation and configuration cmake
CentOS 7 user account configuration original
Centos7.5 installation and configuration MongoDB4.0.4
CentOS 7 installation and configuration PPTP
Configure swap space on CentOS7
CentOS mysql configuration master-slave replication
Implementation of CentOS8.0 Network Configuration
CentOS 7 Redis 5.0.8 sentinel mode configuration
CentOS8 network card configuration file