Maddening Intermittent Wordpress: Error establishi

2019-07-13 17:52发布

Every day or so our Wordpress sites stop responding, the pages begin returning the dreaded 'Error establishing database connection'. There is nothing in the MySQL logs, and I'm at a loss as to what could be causing the issue. We don't have a lot of site visitors, and the machine is a Medium EC2 instance. Anyone have any ideas on how to resolve this?

3条回答
贪生不怕死
2楼-- · 2019-07-13 18:02

There's not a whole lot to work with here. But ... I had the same issue with my micro instance. My problem was that the server kept running out of memory and then the mysql server would stop. It would start again when restarting the computer but it was only a matter of time before it would crash again.

Here's what I was getting in my MySQL logs.

151023  6:15:44 InnoDB: Initializing buffer pool, size = 128.0M
InnoDB: mmap(137363456 bytes) failed; errno 12
151023  6:15:44 InnoDB: Completed initialization of buffer pool
151023  6:15:44 InnoDB: Fatal error: cannot allocate memory for the buffer pool
151023  6:15:44 [ERROR] Plugin 'InnoDB' init function returned error.
151023  6:15:44 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.
151023  6:15:44 [ERROR] Unknown/unsupported storage engine: InnoDB
151023  6:15:44 [ERROR] Aborting

You might want to check for something similar. I use Ubuntu and the log is at /var/log/mysql/ by default.

I solved the problem by setting up a swap file as per Amazon EC2, mysql aborting start because InnoDB: mmap (x bytes) failed; errno 12. The AWS instances don't come with a swap space setup by default (whereas the install I downloaded from Ubuntu back in the day did). You need to set it up manually. Here's the method -

ssh into your AWS instance. Then:

  1. Run dd if=/dev/zero of=/swapfile bs=1M count=1024
  2. Run mkswap /swapfile
  3. Run swapon /swapfile
  4. Add this line /swapfile swap swap defaults 0 0 to /etc/fstab

Read the linked question for more details. Hope that helps!

查看更多
太酷不给撩
3楼-- · 2019-07-13 18:10

When I tried to install local wordpress the same error

error establishing database connection

occurred because I forget to stop the SQL and Apache which was started in xampp. I stopped it and reinstalled the wordpress for Windows and it worked.

查看更多
The star\"
4楼-- · 2019-07-13 18:16

I had a similar issue with intermittent crashing of MySQL. It turned out to be Apache configurations. Bots were brute forcing the site and eventually caused Apache to crash (check your logs: $ cat /var/log/apache2/access.log). Apache automatically restarts, but there isn't enough spare memory to restart MySQL too, hence the Database Connection error. The short fix is to reduce the number of RequestWorkers in Apache to better fit the amount of RAM you have.

You can run a diagnostic on your apache configuration using Apache2Buddy. It'll calculate how many Apache Workers you can run given the amount of RAM you have and how big your application is: $ curl -L http://apache2buddy.pl/ | perl

It's probably going to recommend changing MaxRequestWorkers (or MaxClients on older Apache systems) in your MPM-Prefork configurations. That file is at /etc/apache2/mods-available/mpm_prefork.conf on my system. After changing the value to what Apache2Buddy recommends, just restart Apache and you should be good to go.

I wrote an article on this situation if you want a deeper explanation, a method to stress test, or ideas on how to block some of the bot traffic: http://brunzino.github.io/blog/2016/05/21/solution-how-to-debug-intermittent-error-establishing-database-connection/

查看更多
登录 后发表回答