Error establishing a database connection wordpress

Posted July 21, 2017 5.5k views

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.

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
6 answers

Hi @matchartproduction

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

tail -40 /var/log/mysql/error.log
  • after insert this command tail -40 /var/log/mysql/error.log in putty,there is no any log print out..can u help??

    • Try running this command, which will try to find the log file in MySQL:

      sudo tail -40 $(mysql -Nse "SELECT @@GLOBAL.log_error")

      My guess would be that the error would simply just say that MySQL ran out of RAM and was crashed, which doesn’t mean it’s the root cause of the problem.
      Your main problem is probably your server being attacked or you’re using CloudFlare’s offline-cache (disable that).

      Try to watch your access log for high number of requests, so we can see if it’s an attack or something else making your server goes to it’s knees.

      # If you're using Apache, then this is the default path
      tail -f /var/log/apache2/access.log
      # If you're using Nginx, then this is the default path
      tail -f /var/log/nginx/access.log
      # Press CTRL+C to exit the monitoring
      • Bro,I getting this after insert the command..
        Can you tell me what it means?

        • @matchartproduction

          That’s the access log of each request to your web server.
          But that’s only what’s happening right now - you need to have a look in the access log when the server runs out of memory, since that’s when the interesting things happens.

          I can see a lot of request to Jetpack, it’s a WordPress plugin, so I’m guessing you’re actively using that?

          Do you have any security plugins in WordPress? Or are you using something like Fail2ban? That will help you mitigate your problem.

          If you look here in the forum, then you’ll see a lot of people asking questions the past week about WordPress/MySQL crashing.

  • @hansen
    Hey after insert the command I getting this log as below :

    170725 3:24:15 [Warning] Using unique option prefix myisam-recover instead of m yisam-recover-options is deprecated and will be removed in a future release. Ple ase use the full name instead.
    170725 3:24:15 [Note] Plugin ‘FEDERATED’ is disabled.
    170725 3:24:15 InnoDB: The InnoDB memory heap is disabled
    170725 3:24:15 InnoDB: Mutexes and rw_locks use GCC atomic builtins
    170725 3:24:15 InnoDB: Compressed tables use zlib 1.2.8
    170725 3:24:15 InnoDB: Using Linux native AIO
    170725 3:24:15 InnoDB: Initializing buffer pool, size = 128.0M
    170725 3:24:15 InnoDB: Completed initialization of buffer pool
    170725 3:24:15 InnoDB: highest supported file format is Barracuda.
    170725 3:24:16 InnoDB: Waiting for the background threads to start
    170725 3:24:17 InnoDB: 5.5.57 started; log sequence number 96411819
    170725 3:24:17 [Note] Server hostname (bind-address): '’; port: 3306
    170725 3:24:17 [Note] - '’ resolves to '’;
    170725 3:24:17 [Note] Server socket created on IP: '’.
    170725 3:24:17 [Note] Event Scheduler: Loaded 0 events
    170725 3:24:17 [Note] /usr/sbin/mysqld: ready for connections.
    Version: '5.5.57-0ubuntu0.14.04.1’ socket: ’/var/run/mysqld/mysqld.sock’ port: 3306 (Ubuntu)

    So what should I do next?thanks


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

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

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:

by Jon Schwenn
WordPress is a popular and powerful CMS (content management system) platform. Its popularity can bring unwanted attention in the form of malicious traffic specially targeted at a WordPress site. There are many instances where a server that has not been protected or optimized could experience issues or errors after receiving a small amount of malicious traffic. This guide will show you how to protect WordPress from XML-RPC attacks on an Ubuntu 14.04 system.

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!

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!