Recently my blog often inexplicable database, then see the discovery is due to lack of memory caused the system to shut down the MySQL process, input command free-m, found that the system does not set swap, as shown in the following figure:
So I thought I was giving the system a swap.
First, create a new folder in the root directory swap
$ mkdir/swap
We then use the DD command to generate an empty file, because my VPS memory is 1G, so I give swap a 2G size space, the command is as follows:
$ cd/swap
$ dd If=/dev/zero of=swap.img bs=1m count=2048
# Implementation results are as follows:
2048+0 Records in
2048+0 Records out
2147483648 bytes (2.1 GB) copied, 4.16096, 516 MB/s
This time we use the MKSWAP command to format the generated empty files into swap format
$ Mkswap swap.img
# Implementation results are as follows:
Setting up Swapspace version 1, size = 2097148 KiB
No label, uuid=3a5d676d-41e9-4945-becd-b8e9fef18118
Now all we have to do is activate swap.
$ swapon swap.img
Then execute the free-m command to see the results as shown in the following illustration:
So we have manually added swap, but we need to manually mount the machine after reboot, so we also need to write the partition information to/etc/fstab, add the following information
/swap/swap.img Swap Defaults 0 0
Now we're not afraid to lose swap space even if we reboot the server.