Error establishing a database connection wordpress

July 21, 2017 511 views
Applications Ubuntu

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.

4 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? http://imgur.com/a/EEEKt

        • @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): '127.0.0.1'; port: 3306
    170725 3:24:17 [Note] - '127.0.0.1' resolves to '127.0.0.1';
    170725 3:24:17 [Note] Server socket created on IP: '127.0.0.1'.
    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

@matchartproduction

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: https://www.digitalocean.com/community/tutorials/how-to-protect-wordpress-from-xml-rpc-attacks-on-ubuntu-14-04

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 another answer? Share your knowledge.