← Catalog Matrix

Deployment Execution Blueprint

---
title: Prevent Linux OOM Killer From Crashing MySQL and Node Services
description: A complete DevOps guide and Bash script blueprint to configure secure memory Swap spaces and adjust Linux kernel oom_score_adj properties.
category: Server Config / DevOps
slug: fixing-linux-oom-killer-swap-allocation
keywords: linux oom killer, mysql crumbles out of memory, allocate swap space ubuntu, kernel kill process fix, oom_score_adj configuration
---

When the Linux kernel runs completely out of physical RAM blocks, it invokes a safety mechanism called the Out-Of-Memory (OOM) Killer. The OOM Killer scans active system processes, assigns an internal badness score (`oom_score`), and violently terminates the heaviest process (frequently your production database or web service) to prevent a total OS kernel panic.

If your application logs suddenly vanish or display `Exit code 137` alongside kernel system alerts (`dmesg | grep -i oom`), your environment is suffering from unbuffered memory exhaustion.

## The Dual-Layer Stability Remediation

To secure a low-resource or highly volatile server from unexpected runtime terminations, your engineering strategy must deploy two specific safety buffers:
1. **Virtual Memory Page Spooling (Swap Allocation):** If your server has no Swap space allocated, the absolute moment RAM usage touches 100%, services drop. Provisioning a dedicated Swap file onto the underlying SSD acts as a non-volatile safety overflow valve, absorbing sudden memory spikes.
2. **Process Score Insulation (`oom_score_adj`):** You can explicitly direct the Linux kernel's priority matrix to prioritize or completely protect critical services (like `sshd` or `mysqld`) from being evaluated as high-priority execution targets during low-memory panics.

The automated infrastructure script below provisions an optimized 4GB system Swap space layer and details how to immunize mission-critical system daemons.

# Bash Linux Memory Optimization Script

#!/bin/bash

# ============================================================
# BLUEPRINT 1: AUTOMATED STABLE SWAP SPACE PROVISIONING ENGINE
# ============================================================
# Must be executed with root/sudo privileges: sudo ./allocate-swap.sh

SWAP_PATH="/swapfile"
SWAP_SIZE_GB=4

# Check if a Swap layer is already initialized on this machine
if swapon --show | grep -q "$SWAP_PATH"; then
    echo "[Status] Swap partition already active at $SWAP_PATH. Exiting allocation."
    exit 0
fi

echo "[Provisioning Storage] Creating a ${SWAP_SIZE_GB}GB data allocation block..."
# fallocate reserves system blocks instantly without overhead write stress
sudo fallocate -l "${SWAP_SIZE_GB}G" "$SWAP_PATH"

if [ $? -ne 0 ]; then
    echo "Fallocate failed, falling back to dd block copier..."
    sudo dd if=/dev/zero of="$SWAP_PATH" bs=1M count=$((SWAP_SIZE_GB * 1024))
fi

echo "[Securing Permissions] Locking file access down to system root user..."
# Strict permission structures are vital; unprivileged users must never read Swap memory
sudo chmod 600 "$SWAP_PATH"

echo "[Formating Schema] Mapping raw storage block into Swap space filesystem..."
sudo mkswap "$SWAP_PATH"

echo "[Activating Subsystem] Mounting Swap space engine live into runtime context..."
sudo swapon "$SWAP_PATH"

echo "[Enabling Persistence] Writing configuration parameters into system filesystem table..."
if ! grep -q "$SWAP_PATH" /etc/fstab; then
    echo "$SWAP_PATH none swap sw 0 0" | sudo tee -a /etc/fstab
fi

# Set swappiness value. Lower values (e.g., 10) tell Linux to use swap only as emergency overflow
echo "[Tuning Swappiness] Adjusting VM execution trends..."
sudo sysctl vm.swappiness=10
echo "vm.swappiness=10" | sudo tee -a /etc/sysctl.conf

echo "Success: Emergency virtual memory safety layer fully operational."


# ========================================================
# BLUEPRINT 2: PROCESS OVERRIDE IMMUNIZATION CONFIGURATION
# ========================================================
# To completely protect an essential process like MySQL from being killed by OOM,
# you can modify its systemd unit file adjustment properties.
# Run: sudo systemctl edit mysql
# Inject the following block into the system override display interface:

read -r -d '' SYSTEMD_OVERRIDE_BLUEPRINT << 'EOF'
[Service]
# Set the OOM score adjustment matrix to the absolute minimum (-1000)
# This completely exempts the service from the OOM Killer targeting pool.
OOMScoreAdjust=-1000
EOF