8

I have problem with my Wordpress site, it was working normally, Suddenly this message start to appear

Error establishing a database connection Wordpress

I tried the following things (no one is working and I still have the same problem till now)

I checked my wp_config.php for db name and credential and every thing is fine and I tried to repair db by adding WP_ALLOW_REPAIR and this scripts shows that every thing is okay.

I added test file to my website to see if the db credentials is correct like following

<?php
$link = mysql_connect('localhost', 'myUserName', 'myPassword');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);
?>

and it's print Connected successfully

I tried to disable plugins by changing the name of the plugins folder, but same problem.

11
  • Is this intermittent, or permanent? If the former, perhaps you db server is failing under load Commented Dec 2, 2013 at 14:20
  • it's permanent now, i can not access my website Commented Dec 2, 2013 at 14:22
  • Well if this happened without code changes, chances are its a server issue, what does your host say? Commented Dec 2, 2013 at 14:24
  • they said the server is working fine, logfiles do not contain any info about shutdown or any strange behavior . check your config Commented Dec 2, 2013 at 14:26
  • I'd suggest verifying that the permissions on your wp_config.php (and parent folders) are correct, and that your .htaccess files are in order. If that doesn't help, try restarting the server and see if things break in a fashion that's useful in getting your host's attention. Commented Dec 2, 2013 at 22:57

3 Answers 3

4
+25

Try to change your test file to use the wp-config file.

     <?php
     //PATH TO YOUR FILE
     require_once('path/to/wp-config.php');
     //using wp-config variables 
     $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
     if (!$link) {
          die('Could not connect: ' . mysql_error());
     }
     echo 'Connected successfully';
     mysql_close($link);
     ?>

If this is working you dont have a code issue - you have a server/connectivity issue.

Sign up to request clarification or add additional context in comments.

Comments

0

1st: Maybe MAX_CONNECTION_LIMIT is the problem. If already max connection open. new connection not accepted. So ur WP Site and other database failed to connect to MYSQL SERVER.

so you will get Error establishing a database connection error.

2nd: Uses of RAM. if your RAM IS FULL. Mysql Server failed to start.

check mysql error logs.

Comments

0

Your test demonstrates that you can successfully connect to the MySQL with the user:pass to localhost. But are you sure that the user can use the specific database? Try to add mysql_select_db( $link, 'database_name' ) to your test code, something like this:

<?php
$link = mysql_connect('localhost', 'myUserName', 'myPassword');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';

$use = mysql_select_db( $link, 'myDatabaseName' );

if( $use )
{
    echo "\n" .'Database selected!';
}

mysql_close($link);
?>

This is just a suggestion to be sure that everything is working fine. As @Nimrod007 said: if everything now is working, the problem is not in the code and could be some server/connectivity issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.