Skip to main content

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

WordPress Site is not loading properly behind Google Cloud/AWS Load Balancer

Hello Guys, Today we are going to understand how can we fix a WordPress loading issue (CSS and JS loading issue) behind the Google Cloud Load Balancer or AWS Load Balancer. Generally, When we host a WordPress site directly with Google Cloud Compute Engine VM instance or AWS EC2 instance. It's loading perfectly fine. But once we added this WordPress server behind any Load Balancer either from GCP Load Balancer or AWS Load Balancer, you site will completely broken, means the CSS and JS of your site not loaded properly. The reason for this is - When you put a load balancer in front of WordPress, you need to modify wp-config.php to process the HTTP header HTTP_X_FORWARDED_PROTO to detect the protocol that the user is using and not the protocol the load balancer is using to connect to your backend. To fix this issue, we have to make following changes in the  wp-config.php  file and add the below code snippet on the top of  wp-config.php file - Google Cloud Platform Load Bal...

Git and GitHub Commands Mastery

1. Setup Git and GitHub Global Configuration  git config --global user.email "gautamthakur1983@gmail.com" git config --global user.name "Gautam Thakur" git config --global list git config --list 2. Git Lifecycle | Initilize, Status, Add, Commit git status git init git add git commit -m "Commit Message" git log git log --oneline 3. Git Difference between last commit changes and current version changes git diff 4. Git Compare between 2 different Git Commits git diff eac4c5b 82485b1 5. Git Statsh - To save some changes for temporary purpose ## Pop take out stash and clear but apply take out stash but not clear git stash (To save current changes for temporary) git stash pop (To take out all stash contents) git stash list git stash clear (To clear all the stash changes) git stash save "NAME"  git stash save "NAME1" (Working with multiple stash) git stash sapply 0 or 1 (0 for name, 1 for about) after that run git stash clear git stash clear 6...

How to Setup Kubernetes Cluster in Google Cloud Virtual Machine using "kubeadm"? | Ubuntu 20.04/22.04 LTS

  Hello Friends, In this post, we are going to setup Kubernetes Cluster on Virtual Machine in Google Cloud Platform using kubeadm tool. Hope this post will help you in Kubernetes learning Hand-On Labs (HOL). Requirements: Master Node: No. of VMs 1 Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64  Firewall Rule - Ingress Allow 6443 (API Server) | 2379 (ETCD) | 10251 (Scheduler) | 10252 (Controller Manager) 10250 (Kubelet), sudo access with admin access  Worker Node: No. of VMs 2 Specifications - 2 vCPUs, 4GB RAM, 20 GB HDD (Balanced PD or SSD PD), Operating System (OS) Ubuntu 20.04 LTS x86/64, amd64  Firewall Rule - Ingress Allow 30000-32767 (Services) | 10250 (Kubelet),  sudo access with admin access  Disable Swap and comment fstab entry: First, Disable Swap and remove or comment the Swap entries from fstab file : sudo swapoff -a sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab Next...