Adding Swap Space to Linux without Rebooting

Today I got an error in MySQL "InnoDB: Error: connot allocate   bytes... you should increase swap"
 
I wanted to add more swap you can do it with a file like below or if you have space create a partition or logical volume (lv).

So, here's a simple step to add a swap space (Read more for details):

[1] create a new zeros file using dd command, since I'm adding more 1GB, the count parameter on dd should be 1024*1024=1048576 , here's the dd command (I named new swap file to /swapfile1):

[root@ns1 /]# dd if=/dev/zero of=/swapfile1 bs=1024 count=1048576
1048576+0 records in
1048576+0 records out
1073741824 bytes (1.1 GB) copied, 45.5356 seconds, 23.6 MB/s

[2] zeros file has been created, now we make it as swap file:

[root@ns1 /]# mkswap /swapfile1
Setting up swapspace version 1, size = 1073737 kB

[3] make it on!

[root@ns1 /]# swapon /swapfile1

[4] check it using free

[root@ns1 /]# free
             total       used       free     shared    buffers     cached
Mem:       1026788     940420      86368          0       4096     363916
-/+ buffers/cache:     572408     454380
Swap:      2097136     803060    1294076

Yup, it's activated now.. you may add entry on your /etc/fstab so it will automounted when your server is rebooted, add this line

/swapfile1              swap                    swap    defaults        0 0

That's it! Have fun with your server!
 

For a partion

# mkswap /dev/dm-2
# swapon /dev/dm-2
 

For a lv

# mkswap /dev/vg_server/lv_swap
# swapon /dev/vg_server/lv_swap
 

Subject: