Swapping occurs when system moves some data between memory and a special area on disk called swap space. The process is called swapping in or swapping out depending on the direction in which it happens. System swaps out when it makes a decision to free up some physical memory (RAM) and pushes data out to disk. It swaps in when an application needs to access data that was swapped out. MySQL is like any other application and any memory it holds can also be sent to disk. It may have severe negative impact on performance.
The foremost step to prevent swapping is ensuring that not database, not any other application can either independently or collectively use up all available memory. The peak usage may not exceed a threshold that still leaves comfortable buffer for any remaining system activity. If this condition is not met, nothing can help and swapping may occur.
Further tuning can be done on the system level. Linux comes with a configurable parameter called swappiness. Swappiness defines kernel attitude to swap. The larger the value the more willing it is to evict data pages from physical memory to a swap space on disk rather than free some memory from its own buffers. By default any Linux uses a relatively high value of sixty from the range of 0 to 100: