1

I am running a website in WordPress which run without error. But when I refresh page or site multiple times (using F5) then it shows Error establishing a database connection other websites in that server or all other subdomain also goes down.

So My WordPress is not showing any error but refreshing multiple times cause Error establishing a database connection

How can I solve this problem.

because if there is any error like hostname, database name etc the it should not run website at all but it cause problem only when multiple refresh.

  • Just a guess. If a custom theme is being used, it could be that the theme developer hard coded in the database credentials into the website. If this where done the website would load fine the first time, but fail the second time it is loaded. I don't know why anyone would code it that way though. Anyhow, this is what I would do. I would deactivate the theme and all of the plugins, and then reactivate everything one by one until you find what plugin/theme is causing the issue. If it's not a theme or plugin, It's something with WordPress core which can be reinstalled. – hostingutilities.com Dec 27 '16 at 05:33
  • @ Mr. Me In that server I am running 4 wordpress website. All four have different theme and plugin with All in one seo pack common. This problem is occuring with all wordpress websites. Another website in bootstrap does not have any problem. – Ghanshyamji Gupta Dec 27 '16 at 05:38
  • Are there any php.ini files or user.ini files that could be messing with the way PHP is configured. If you already have four websites running, then you probably had some of these websites for some time before this problem arose. What where you doing when you started receiving these database connection errors? Do you have to completely restart the server to make the problem go away? – hostingutilities.com Dec 27 '16 at 06:28

1 Answers1

0

Short answer:

If none of the solutions on the web help you, try restarting your database server or allocating more RAM for it.


Extended answer:

I got the same problem a couple hours ago. It was all of a sudden, while I was editing some content in Wordpress admin panel.

In my case I am running a dev database server on my local machine.

Googling for the solution, I created a new script dbconnection.php in the site root directory with the content as follows:

<?php

$link = mysqli_connect('localhost', 'root', '');

if (!$link) {
    die('Could not connect: ' . mysqli_error($link));
}
echo 'Connected successfully';

mysqli_close($link);

Every third time (exactly) I was refreshing the page with that script I was getting message Could not connect: or No input file specified.

I opened my server logs (Apache) and this is what I saw:

[Thu Feb 06 14:04:51.101731 2020] [:error] [pid 30890:tid 139935881332480] [client 127.0.0.1:42729] FastCGI: server "/usr/lib/cgi-bin/php5.fcgi" stderr: PHP message: PHP Warning: fopen(/www/MY_VIRTUAL_HOST_DOMAIN/wp-content/languages/ru_RU.mo): failed to open stream: Too many open files in /www/MY_VIRTUAL_HOST_DOMAIN/wp-includes/pomo/streams.php on line 148, referer: http://MY_VIRTUAL_HOST_DOMAIN/wp-admin/post.php?post=250&action=edit ...

And a full stack trace.

So I understood that for some reason my database can't create more connections.

Defining a constant define( WP_ALLOW_REPAIR, true ) in the wp-config.php didn't help.

Disconnecting all of my plugins didn't help either. I did a quick search if any of the PHP scripts don't do fclose() after doing fopen().

Didn't try to extend maximum database connection limit, because I wanted to find a permanent solution.

So what I did is I shut down my virtual machine (the dev environment I am working in), restarted my PC, launched everything again and the problem didn't appear the second time.

No more errors in the logs.

So I guess my VM ran out of RAM exactly when I added some more data to the database. Which is very uncommon, because I have allocated a lot of RAM for it (11 GB out of 16 GB I have).

Hope this helps.

Vasiliy Artamonov
  • 939
  • 2
  • 8
  • 24
  • 1
    Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) – Dharman Feb 13 '20 at 13:50
  • @Dharman Good note, but the main point of the code snippet above was to check the database connection itself, not the exact error, so it doesn't really matter – Vasiliy Artamonov Feb 15 '20 at 12:46
  • 1
    It does matter. You should not check whether the connection is successful or not. Enable error reporting and PHP will tell you this automatically. `mysqli_error($link)` of course will not give you any errors. – Dharman Feb 15 '20 at 12:48
  • @Dharman Thanks for the comment, I realized what you were trying to say. – Vasiliy Artamonov Sep 29 '20 at 17:43