Question

Repeated Error establishing a database connection

Posted May 14, 2021 441 views
Networking

I recently set up a wordpress website and occasionally I get the above error message. That is initially I get connected without any problem and after a few minutes the server is not responding returning the above error. When I’m trying to log in again there is no response but after a few minutes everything works ok again. This"loop"process keep happening.

Can you please provide me with a solution to this problem?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
4 answers
KFSys May 18, 2021
Pinned Answer

Hi @app260866,

This crash is most likely due to your system running out of memory. I’d suggest that you add a swap file to give yourself a bit more of a buffer. Check out this tutorial:

How To Add Swap on Ubuntu 18.04

What you can also do is to use the MySQLTuner script.

The MySQLTuner is a script written in Perl and allows you to quickly test your MySQL configuration and it gives you suggestions for adjustments to increase performance and stability.

According to the official GitHub page, it supports 300 indicators for MySQL/MariaDB/Percona Server in this last version.

To run the script you could do the following:

  • SSH to your Droplet
  • Download the script:
wget http://mysqltuner.pl/ -O mysqltuner.pl
  • Then execute it:
perl mysqltuner.pl

The script would run multiple checks against your MySQL instance, all checks done by MySQLTuner are documented here.

Also as stated in the official documentation, it is still extremely important for you to fully understand each change you make to a MySQL database server. If you don’t understand portions of the script’s output, or if you don’t understand the recommendations, you should consult a knowledgeable DBA or system administrator that you trust.

As a good practice make sure to always test your changes on staging environments before implementing them on your production database.

On the same note, if you want to have worry-free MySQL hosting and focus on your application, I would recommend trying out the DigitalOcean Managed Databases:

https://www.digitalocean.com/products/managed-databases-mysql/

Source:
https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability

Additionally, answer taken from @bobbyiliev from this question :

https://www.digitalocean.com/community/questions/help-mysql-keeps-crashing-error-establishing-a-database-connection

  • Hi again,
    I used MySQl Tuner script and I got the following output.

    General recommendations:
    MySQL was started within the last 24 hours - recommendations may be inaccurate
    Reduce your overall MySQL memory footprint for system stability
    Dedicate this server to your database for highest performance.
    Configure your accounts with ip or subnets only, then update your configuration with skip-name-resolve=1
    Before changing innodblogfilesize and/or innodblogfilesingroup read this: https://bit.ly/2TcGgtU
    Variables to adjust:
    *** MySQL’s maximum memory usage is dangerously high ***
    *** Add RAM before increasing MySQL buffer variables ***
    innodb
    logfilesize should be (=16M) if possible, so InnoDB total log files size equals to 25% of buffer pool size.

    How can I get someone to manage my account and resolve the issue?

    Please advice.

    Thnx.

    • Hi @app260866,

      I see you’ve found the issue being the Woocomerce plugin. Nice to hear that!

      Anyway, to answer your question here. You can hire a developer to look at your Droplet and optimize it. There are a lot of websites where you can hire freelancers. Just make sure to provide them with a restricted user rather than root.

Hi @app260866,

This looks like your Database server is crashing and restarting itself.

I’ll recommend checking your MySQL/MariaDB error logs to see what’s going on. Basically, SSH to your droplet and tail the log:

tail -f /path/to/mysql/log

And when your Website displays an error establishing a database connection, you’ll see what was the error once it’s logged.

Reasons behind these failures might be memory or CPU shortage, you might need to upgrade if your Website is too heavy or maybe there is an issue with some of the Databases inside your MySQL/MariaDB instance but again this will be logged in your error_log.

Regards,
KFSys

Hello, @app260866

If you’re using Ubuntu the path to the MySQL errog_log should be /var/log/mysql/error.log so if you want to check the last 100 entries of the log you can use the following command

  • tail -n 200 /var/log/mysql/error.log

and monitor it in real-time:

  • tail -f /var/log/mysql/error.log

Hope that this helps!
Regards,
Alex

  • Ok, done. It gives the following:
    2021-05-16T10:09:07.070462Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.25-0ubuntu0.20.04.1) starting as process 919644
    2021-05-16T10:09:07.092960Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
    2021-05-16T10:09:07.709959Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
    2021-05-16T10:09:08.070412Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: ‘127.0.0.1’ port: 33060, socket: /var/run/mysqld/mysqlx.sock
    2021-05-16T10:09:08.108570Z 0 [System] [MY-010229] [Server] Starting XA crash recovery…
    2021-05-16T10:09:08.137901Z 0 [System] [MY-010232] [Server] XA crash recovery finished.
    2021-05-16T10:09:08.240152Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
    2021-05-16T10:09:08.240803Z 0 [System] [MY-013602] [Server] Channel mysql_main configured to support TLS. Encrypted connections are now supported for this channel.
    2021-05-16T10:09:08.302710Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.25-0ubuntu0.20.04.1’ socket: ’/var/run/mysqld/mysqld.sock’ port: 3306 (Ubuntu)

    So is it memory and/or CPU shortage?

    Thanks again.

    • From these lines it’s not clear. The lines you’ve posted are only from the MySQL startup. Wait until MySQL crashes and see what does the error log say.

      • Hi again,

        When I get “Error establishing a database connection” after trying to load for several minutes MySQL log gives me the following lines:

        2021-05-17T07:33:47.214046Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.25-0ubuntu0.20.04.1) starting as process 941462
        2021-05-17T07:33:47.289920Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
        2021-05-17T07:33:48.056788Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
        2021-05-17T07:33:48.967079Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Bind-address: ‘127.0.0.1’ port: 33060, socket: /var/run/mysqld/mysqlx.sock
        2021-05-17T07:33:49.312943Z 0 [System] [MY-010229] [Server] Starting XA crash recovery…
        2021-05-17T07:33:49.375920Z 0 [System] [MY-010232] [Server] XA crash recovery finished.
        2021-05-17T07:33:52.690498Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
        2021-05-17T07:33:52.694140Z 0 [System] [MY-013602] [Server] Channel mysqlmain configured to support TLS. Encrypted connections are now supported for this channel.
        2021-05-17T07:33:53.065134Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.25-0ubuntu0.20.04.1’ socket: ’/var/run/mysqld/mysqld.sock’ port: 3306 (Ubuntu).
        2021-05-17T07:42:52.064958Z 19 [Warning] [MY-013360] [Server] Plugin sha256
        password reported: “sha256password’ is deprecated and will be removed in a future release. Please use cachingsha2_password instead’

        Please advise.

        Thnx again.

        • Hello, @app260866

          This crash is most likely due to your system running out of memory. I’d suggest that you add a swap file to give yourself a bit more of a buffer. Check out this tutorial:

          How To Add Swap on Ubuntu 18.04

          What you can also do is to use the MySQLTuner script.

          The MySQLTuner is a script written in Perl and allows you to quickly test your MySQL configuration and it gives you suggestions for adjustments to increase performance and stability.

          According to the official GitHub page, it supports 300 indicators for MySQL/MariaDB/Percona Server in this last version.

          To run the script you could do the following:

          • SSH to your Droplet
          • Download the script:
          wget http://mysqltuner.pl/ -O mysqltuner.pl
          
          • Then execute it:
          perl mysqltuner.pl
          

          The script would run multiple checks against your MySQL instance, all checks done by MySQLTuner are documented here.

          Also as stated in the official documentation, it is still extremely important for you to fully understand each change you make to a MySQL database server. If you don’t understand portions of the script’s output, or if you don’t understand the recommendations, you should consult a knowledgeable DBA or system administrator that you trust.

          As a good practice make sure to always test your changes on staging environments before implementing them on your production database.

          On the same note, if you want to have worry-free MySQL hosting and focus on your application, I would recommend trying out the DigitalOcean Managed Databases:

          https://www.digitalocean.com/products/managed-databases-mysql/

          Source:
          https://www.digitalocean.com/community/questions/how-to-tweak-mysql-mariadb-configuration-for-increased-performance-and-stability

          Hope that this helps!
          Regards,
          Alex

          • Hello,

            After a lot of checks it seems the problem is caused by Woocommerce plugin. When it is deactivated everything works fine.

            Please advise how can I solve the issue.

            Thnx.

Hello, @app260866

The Woocommerce plugin is likely to consume more memory and if you’re using a smaller droplet you might run into “out of memory” issues. You can check the logs for memory exhaustion.

You can execute the following commands:

  • grep -i kill /var/log/messages
  • grep -i oom /var/log/messages

I’d suggest that you add a swap file to give yourself a bit more of a buffer. Check out this tutorial:

https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-18-04

The other option will be to upgrade the droplet memory as well.

Hope that this helps!
Regards,
Alex