Error establishing a database connection wordpress

I running a wordpress website in 512 MB Memory / 20 GB Disk /Ubuntu LAMP on 14.04,everything working fine for 2 weeks,then suddenly appear Error establishing a database connection this is what I try to solve this problem

  1. increase server size to 1GB memory,still the same
  2. increase swap,still cant solve

Now I cant even go in phpmyadmin,it show #2002 Cannot log in to the MySQL server in my phpmyadmin page even my username and password is correct.

Please suggest me a solution to solve this,cause I really dont want to configure the whole server and build the site all over again.


Submit an answer
You can type!ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

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.

Hello, all

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

You will most definitely need to upgrade your droplet as you’re running out of memory and your application/website needs more resources in order to continue to operate.

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 -O
  • Then execute it:

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 a worry-free MySQL hosting and focus on your application, I would recommend trying out the DigitalOcean Managed Databases:

This was mini tutorial was posted from bobbyiliev in this question in our community:

You can also create a simple bash script to check if MySQL is running and if not to restart it.


# Check if MySQL is running
sudo service mysql status > /dev/null 2>&1

# Restart the MySQL service if it's not running.
if [ $? != 0 ]; then
    sudo service mysql restart

Run this script every 5 minutes using a cron job like this one:

 */5 * * * * /home/user/scripts/ > /dev/null 2>&1

Hope that this helps! Regards, Alex

Have you checked the MySQL error_log in order to see if the server is running out of memory or if the issue is with to many MySQL connections? As per the tutorial you can run this command in order to check if the server have recently run out of memory:

zgrep -a "allocate memory" /var/log/mysql/error.log*

zgrep will search through log files, including older log files that have been archived as compressed .tar.gz files. We’re searching for lines that contain allocate memory, in any error.log* file in the /var/log/mysql/ directory.

You can always login and check the MySQL error_log when the issue occurs using tail to either check the last 100 entries in the log or monitor the log in real time:

tail -n 100 /var/log/mysql/error.log

and monitor it in real time:

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

Hope that this helps! Regards, Alex

I noticed a large number of requests coming to my site. Looking at the log file located at /var/log/apache2/access.log I noticed a large number of requests each second to /xmlrpc.php.

This causes the server to run out of memory pretty quickly and the mysql service eventually either fails or is restarted for some reason.

Maybe the same thing is happening to you.

If that’s your case look at:

I am having a similar problem. This is what my log file shows:

2017-07-22T15:26:09.153240Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.8 2017-07-22T15:26:09.153247Z 0 [Note] InnoDB: Using Linux native AIO 2017-07-22T15:26:09.155154Z 0 [Note] InnoDB: Number of pools: 1 2017-07-22T15:26:09.157889Z 0 [Note] InnoDB: Using CPU crc32 instructions 2017-07-22T15:26:09.165356Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M 2017-07-22T15:26:09.165839Z 0 [ERROR] InnoDB: mmap(137428992 bytes) failed; errno 12 2017-07-22T15:26:09.165857Z 0 [ERROR] InnoDB: Cannot allocate memory for the buffer pool 2017-07-22T15:26:09.165870Z 0 [ERROR] InnoDB: Plugin initialization aborted with error Generic error 2017-07-22T15:26:09.165884Z 0 [ERROR] Plugin ‘InnoDB’ init function returned error. 2017-07-22T15:26:09.165891Z 0 [ERROR] Plugin ‘InnoDB’ registration as a STORAGE ENGINE failed. 2017-07-22T15:26:09.165916Z 0 [ERROR] Failed to initialize plugins. 2017-07-22T15:26:09.165920Z 0 [ERROR] Aborting

2017-07-22T15:26:09.165937Z 0 [Note] Binlog end 2017-07-22T15:26:09.167271Z 0 [Note] Shutting down plugin ‘MyISAM’ 2017-07-22T15:26:09.167486Z 0 [Note] Shutting down plugin ‘CSV’ 2017-07-22T15:26:09.168462Z 0 [Note] /usr/sbin/mysqld: Shutdown complete


It is probably memory issue, try to increase the RAM for the droplet.

Hi @matchartproduction

Please post the last 40 lines from your MySQL error log:

tail -40 /var/log/mysql/error.log