It seems mysqld allocates too much memory as buff/cache:

free -m
              total        used        free      shared  buff/cache available
Mem:            990         448          96          36         445         326
Swap:           511         511           0

Now I cannot start httpd service because it "Failed to fork: Cannot allocate memory".

I wonder why this happens? Why the 445M buff/cache cannot be reclaimed and used for httpd?


Solution 1:

The column "buff/cache" reports the sum of memory in use for buffers and cache.

Buffers cannot be reclaimed because they are needed by the kernel to operate.

Cache could be reclaimed, as witnessed by the "available" column. This would however require writing the current content of the reclaimed memory to swap if the cache page in question is dirty. But your swap is full, as witnessed by the 0 in the "free" column of the "swap" line.

In sum, memory allocation fails because your swap is full, or in other words, because your virtual memory is exhausted.

Solution 2:

1GB is a very small machine for running MySQL in. Aside from the items already discussed, look in the my.cnf (or wherever MySQL's config file is). Look for or set (in [mysqld] section), the following:

max_connections = 10
innod_buffer_pool_size = 150M
key_buffer_size = 10M
table_open_cache = 50

Lower your values to these; it may help get things started.

What version of MySQL (or MariaDB) are you using? What other apps do you have on the same machine?