How to create Swap partition in CentOS 7? or MySQL frequently stopped



Hello Friends,


I am writing this post because one of my friend was facing the issue of MySQL or MariaDB service frequently stop every 2-3 days. 


I have also faced this issue several time in the past. Hope this post will help you, if you are also facing the similar issue.


First thing first, If you are using AWS and t2-micro free-tier or Digital Ocean smaller droplet or any least configuration machine. You need to upgrade your server also please keep eye on your disk space.


As this post is all about to Add SWAP space and increase SWAP memory.


Let's understand What is SWAP space or memory


Swap space or memory is a space in the Hard Disk of your computer that Operating Systems will use to put the info that is actually on the RAM to free it for another application. This should be done when the system needs memory for a new process.


Now come to the point.


To Check available SWAP space



sudo swapon -s
        or
sudo swapon --show
There are two way to accomplish this -
  1. Creating SWAP Partition
  2. Creating SWAP file

1. SWAP partition, we can create either with fdisk command or with parted command. 

But we are not with this method rather we are going with the second method which is SWAP space.

2. The user you are logged in as must have sudo privileges to be able to activate swap. In this guide, we will add 1G of swap, if you want to add more swap, replace 1G with the size of the swap space you need.

Follow the steps below to add swap space on a CentOS 7 system.


First, create a file which will be used as swap space:


sudo fallocate -l 1G /swapfile
If the fallocate utility is not available on your system or you get an error message saying fallocate failed: Operation not supported, use the following command to create the swap file:


sudo dd if=/dev/zero of=/swapfile bs=1024 count=104857
Ensure that only the root user can read and write the swap file by setting the correct permissions:


sudo chmod 600 /swapfile
Next, set up a Linux swap area on the file:


sudo mkswap /swapfile
Run the following command to activate the swap:


sudo swapon /swapfile
Make the change permanent by opening the /etc/fstab file:


sudo vi /etc/fstab
and pasting the following line in -

/etc/fstab file add the below line at the end


/swapfile swap swap defaults 0 0
Verify that the swap is active by using either the swapon or the free command as shown below:


sudo swapon --show
free -mh

Adjusting the Swappiness Value

Swappiness is a Linux kernel property that defines how often the system will use the swap space. Swappiness can have a value between 0 and 100. A low value will make the kernel to try to avoid swapping whenever possible while a higher value will make the kernel to use the swap space more aggressively.



The default swappiness value on CentOS 7 is 30. You can check the current swappiness value by typing the following command:



cat /proc/sys/vm/swappiness
Output: 
30 

While the swappiness value of 30 is OK for desktop and development machines, for production servers you may need to set a lower value.


For example, to set the swappiness value to 10, type:



sudo sysctl vm.swappiness=10

To make this parameter persistent across reboots append the following line to the /etc/sysctl.conf file:

modify /etc/sysctl.conf file and add the line given below -



vm.swappiness=10

The optimal swappiness value depends on your system workload and how the memory is being used. You should adjust this parameter in small increments to find an optimal value.



Removing a Swap File

To deactivate and remove the swap file, follow these steps:


Start by deactivating the swap space by typing:sudo swapoff -v /swapfile



sudo swapoff -v /swapfile
Next, remove the swap file entry /swapfile swap swap defaults 0 0 from the /etc/fstab file.

Finally, delete the actual swapfile file with rm:



sudo rm /swapfile
Credit: linuxize

Popular posts from this blog

Firewalld Command - Useful firewall-cmd Examples (RHEL based)

How to Convert PEM KeyFile into PPK KeyFile and vice versa?