Applications installed in a ubuntu server is dependent upon RAM and other resources. If for some reason entire RAM memory is consumed then chances are it may impact on other applications and mysql database may crash.
Recently I had encountered with my new web application installed in Cloud hosting. Hacker with the help of bot created infinite traffic in my web application. With in couple of minutes, mysql database crashed. When I went through the log files, I found out due to entire consumption of RAM memory, mysql crashed. I recovered mysql database and the web application is live now. At this cost, I have learned the importance of Swap Space and following article is about it.
What is Swap Space?
Swap Space is a space in the hard disk or memory which can be used as RAM memory in case RAM runs out of memory. Suppose I am using a cloud hosting at digital ocean and my cloud hosting has 4GB RAM and 40 GB Hard disk. If for some reason all the RAM memory is consumed then in that case rest of web application such as MySQL database may crash. Some times it is almost impossible to recover crashed database. To protect my application from such incidence, I can use certain portion of the hard disk as RAM Memory. It is called swap space.
How much should be the Swap Space?
There is no hard rule for it. But the professionals suggest swap space should be twice to the RAM Memory. I have 4GB RAM. So I am going to use 8GB of hard disk as Swap Space.
First login with your user name and password into your ubuntu server using Putty. (If you have no idea about putty then follow the second chapter in the cloud hosting tutorial list. All the chapter list is present below this article).
Create Swap Space
First of all, first lets check whether any swap space is already created. (Because certain cloud hosting provider offer swap space by default)
test@tester:~$ swapon –show
If the output is blank it means, there is no swap space created.
Lets create the swap space with following command.
test@tester:~$ sudo fallocate -l 4G /swapspace
I have used swapspace as file name to refer to the swap space. You can use any name you want and it should not contain space or other special character. Upon successful execution of above command, it wont print anything.
For confirmation, lets check whether the swap space is created with following command.
test@tester:~$ ls -lh /swapspace
Output: -rw-r–r– 1 root root 4.0G Aug 8 13:37 /swapspace
If you are seeing similar to above output then it means swapspace is created successfully.
Any user who logins to the system may access this space. We are required to restrict root user only to access this spacce. We can achieve this with following command.
test@tester~$ sudo chmod 600 /swapspace
Upon execution of above command, nothing will print. It means the command has been executed successfully.
So we have create a space in the hard disk but has not marked the space as swap sapce. So with following command we can mark the space as swap space
test@tester~$ sudo mkswap /swapspace
Upon execution of above command, you may see following type output
Setting up swapspace version 1, size = 4 GiB (4294963200 bytes)
no label, UUID=77a9bc8885-a412-4dde-k5baccw-k94761opnd53bc
It means swap space is successfully created.
Now its time to turn on the swap space so that system can use the swap space in case it need it.
test@tester~$ sudo swapon /swapspace
For confirmation whether the swap space is recognised by the system or not, we use following command.
test@tester~$ sudo swapon –show
NAME TYPE SIZE USED PRIO
/swapspace file 4G 0B -2