high memory and mysql disconnecting issues

November 30, 2017 3.3k views


I careated LEMP 512MB droplet with ubuntu 17.04 and php 7.1. and i uploaded my laravel project on it.
i see high memory usage between 85% and 95%, after that I upgraded the droplet to 1GB.

after that i make swap to enable me to install Redis.

Now i face 2 problems:
First one is memory is still high. there are 2 attached images appear this issue with top process in memory.

Image 1 link

Image 2 link

Second problem mysql is disconnect usually and can’t restart mysql, i reboot the droplet to re work.
this problem related with cannot connect to mysql (DB_SOCKET=/var/run/mysqld/mysqld.sock).
how i can solve those?

1 comment
  • @kamaln7 @emestee @alastairc

    Thank you for reply and assistance.

    I like to tell you, I found the mistake in high memory, and I need your suggestion.

    I make crontab to run queue:work in laravel

    * * * * * /usr/bin/php7.1 /var/www/html/artisan schedule:run >> /dev/null 2>&1

    I comment it since 3 hours, here is a screen shot of droplet status:

    Droplet Status

    Now, how i can run queue:work in laravel without errors?


3 Answers

If the php process is taking up way too much RAM, it’s possible that your code has a memory leak of some sorts. I would suggest using https://github.com/barryvdh/laravel-debugbar to profile your application’s memory usage and see what’s taking up so much RAM.

  • Sorry for late in reply, but I want to give you a biggest thanks on your reply, I installed debugbar and I reduced all User-Side queries (in one page from 59 to 14 queries).

    Yesterday, I upgrade the droplet to 3 GB RAM after reducing queries. But also mysql is crashed today.

Examine dmesg and log files for signs of mysql crashing. Very likely it’s killed for out of memory errors.

Post memory profile like the output of free(1).

Ensure you have swap enabled.

In my experience at least 2Gb RAM is required for running PHP-FPM in production along with mysql/redis.

  • Thank you for reply, Yesterday I upgrade droplet to 3 GB RAM. i enabled swap when it was 1 GB. I want to edit swap now.

  • i follow this link to add swap:

    I make it 6GB as I understand the swap is double of RAM, And now this is my result:
    ~# free -m
    total used free shared buff/cache available
    Mem: 3004 1532 1025 29 446 1291
    Swap: 6143 0 6143

    Is it right?

    by Justin Ellingwood
    One of the easiest way of increasing the responsiveness of your server and guarding against out of memory errors in your applications is to add some swap space. In this guide, we will cover how to add a swap file to an Ubuntu 16.04 server. <$>[warning] [label...
    • No. There is absolutely no reason for the swap to be 2x the size of your RAM, and on droplets that are restrained in available disk space it’s perfectly fine to go with less swap. How much you need is determined by your peak load.

      • Hi ,
        I wrote comment since 2 days My comment

        and I solve my problem with installing supervisor :)

        Thank you :)

        • Automatically restarting a crashing service is not a solution. You still have the problem; now you’ve just made it invisible.

As another option you could run mysql on a second droplet and enable local networking for both of them. That way when your php application hogs too much memory the OOM killer will only murder your webserver workers and not your mysql database. (You’ll have to connect to your database via tcp instead of unix sockets though).

As a plus you can also increase the size of each droplet as each sides needs grow, rather than hoping they don’t step on each others’ toes, there’s also less risk of mysql losing data when it’s randomly killed.

Have another answer? Share your knowledge.