MYSQL on Linux(Ubuntu) keeps crashing – Solution is to create Swap Space

MYSQL on Linux(Ubuntu) keeps crashing – Solution is to create Swap Space

MYSQL on Linux(Ubuntu) keeps crashing – Solution is to create Swap Space

          There are many internet users who face this problem where the MYSQL on their server getting crashed frequently. I personally faced this problem in my WordPress blog in my VPS. This problem occurs due to lack of memory(RAM) space. i.e. when your RAM usage hits the maximum. This problem can be solved by restarting the MYSQL service with the command sudo service mysql restart and this is not a feasible solution because we need execute this command each time when the MYSQL crashes. Try restarting the server itself if the above command does not solve the problem. Another way to solve the issue is adding more RAM to the servers by contacting the hosting provider which is an expensive solution.

MYSQL on Linux(Ubuntu) keeps crashing

MYSQL on Linux(Ubuntu) keeps crashing

          The best way is to add swap space(memory in harddisk) to the server which will be used by the server operating system to run the applications in efficient manner by copying the files between RAM and the swap space as required.

Steps to add Swap Space in Linux(Ubuntu)

Step 1: Check for existing swap space available in your system

Run the following commnad to display the list of swap space present in your system

sudo swapon -s

If you have an empty list, proceed the following steps.

Step 2: Check for available memory space(of hard disk) available in your system

Run the command df to list the availability of memory space in your system.

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda        20907056 1437188  18421292   8% /
udev              121588       4    121584   1% /dev

It is always to better to add swap space equal to twice the RAM or at least equal to the RAM present in your system.

Step 3: Creating the swap file

Create the swap file with the following commands

sudo dd if=/dev/zero of=/swapfile bs=1024 count=512k

sudo mkswap /swapfile


    • swapfile is the name of the swap file
    • bs represents the block size in bytes
    • count=512k will add 512MB of swap space

Now you will get the following display message.

Setting up swapspace version 1, size = 262140 KiB
no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb

Step 4:  Activating the swap space

Run the following command so that the swap space is available to use for the operating system.

sudo swapon /swapfile

Step 5: Verify the availability of swap space

Run the command sudo swapon -s to list the swap spaces added in your system. You will get message something like below.

swapon -s
Filename				Type		Size	Used	Priority
/swapfile                               file		262140	0	-1

Step 6: Making the swap file permanent

The swap space added will be retained only till the machine reboots. To make the swap permanent open the file fstab using the command sudo nano /etc/fstab and add the following to a new line

 /swapfile none swap sw 0 0

Step 7: Setting up the permission for the swap file

Run the following command to make the swap file readable and writable only by the root user of the system.

sudo chown root:root /swapfile
sudo chmod 0600 /swapfile

Step 8: Finish

Related Links:

  1. 7 Must Have Plugins/Widgets For WordPress Blog/Website
  2. Search Bar HTML Widget for WordPress Without Using Plugins
  3. Import Blogger to WordPress – Formatting Problem/Issues
  4. How to improve WordPress performance with browser caching (through .htaccess)
  5. “Error Establishing a Database Connection” in WordPress After Changing WordPress Address and Site Address
  6. MYSQL on Linux(Ubuntu) keeps crashing – Solution is to create Swap Space
  7. Blogging Tips & Guidelines – SEO, Backlinks and Design


3 thoughts on “MYSQL on Linux(Ubuntu) keeps crashing – Solution is to create Swap Space

    1. admin@280392

      Just because swap is allocated,it doesn’t mean that the swap is being used by the system. use vmstat command to get the swapin and swappout details. You can also forcefully enable or disable swap with these commands
      sudo swapoff -a
      sudo swapon -a

Leave a Reply