1

I know that you are probably sick of someone posting another Error establishing a database connection Question but I really did my research and found nothing about my specific Problem so please have a read.

I develop on my local machine and the page worked fine! The database in my development environment was migrated from a snapshot of the production database. In the same way I migrated my local database snapshot to my production database.

But when running Wordpress with my external production database on my server I get the common Error establishing a database connection page on my homepage.

So I did the following:

  1. Checked wp-config.php File
  2. Tried to connect from my Server via CLI to the database with the values from the wp-config.php file ( This succeeded!! )
  3. Tried to access wp-admin( This also succeeded!!!! I was able to login as well as using all subpages of wp-admin )
  4. Searched the Web
  5. Tried to fix possible inconsistencies in the database with Wordpress Tool /wp-admin/maint/repair.php( still the same behavior... )
  6. Searched the Web
  7. Searched the Web
  8. Got really frustrated
  9. Searched the web again ... I think you get the point

I also asked a friend of mine who is very experienced with Wordpress but he couldn't help me with this problem either - in fact he has never seen this Problem - so if you have any ideas I would highly appreciate it, since I'm absolutely clueless about what to do.


UPDATE

Because I thougt that the Problem must be with the database I also tried the following:

  1. droped my database
  2. created a new empty one
  3. called the URL and walked through the wordpress installation Process
  4. After the fresh installation still the same Error persists
  • Strange. Do/did you have some sort of caching plugin installed? Is there a file called .maintenance in the same directory as your wp-config.php? – DaveLak Jun 22 '18 at 20:15
  • Indeed... nope. I checked both. – Chris Nissen Jun 22 '18 at 20:28
  • It might be a DB corruption. Try define( 'WP_ALLOW_REPAIR', true ); in wp-config.php and then go to yoursite.com/wp-admin/maint/repair.php – anmari Jun 23 '18 at 5:33
  • It might also be because you have some wrong domains in the settings in the DB that don't make sense. Similar to this, but other way around. wordpress.stackexchange.com/questions/301864/… – anmari Jun 23 '18 at 5:35
  • Thank you for your comments but as I said in the list of things I already tried: I executed the /wp-admin/maint/repair.php wich finished stating everything is okay... – Chris Nissen Jun 23 '18 at 12:28
0

Make sure your database connection is correct

define('DB_NAME', 'database-name');
define('DB_USER', 'database-username');
define('DB_PASSWORD', 'database-password');
define('DB_HOST', 'localhost');

And you should test with plain php file for connecting database like below codes

<?php

$connect = mysql_connect('localhost', 'root', 'password');
if (!$connect) {
    die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($connect);

To make it flexible on localhost or production, use this way,

<?php

switch ( $_SERVER['HTTP_HOST'] ) {
    case 'yourdomain.dex': //local
        define( 'DB_NAME', 'rino' );
        define( 'DB_USER', 'root' );
        define( 'DB_PASSWORD', '' );
        define( 'DB_HOST', 'localhost' );
        define( 'WP_DEBUG', true );
        define( 'WP_DEBUG_DISPLAY', true );
        define( 'SCRIPT_DEBUG', true );
        define( 'WP_DEBUG_LOG', true );
        define( 'FS_METHOD', 'direct');
        @ini_set( 'display_errors', true );
        define( 'ENV', 'dev' );
        break;

    case 'yourdomain.staging.com': //staging
        define( 'DB_NAME', 'creati_444' );
        define( 'DB_USER', 'creatisd' );
        define( 'DB_PASSWORD', 'lPC44@8u1B' );
        define( 'DB_HOST', 'localhost' );
        define( 'WP_DEBUG', true );
        define( 'WP_DEBUG_DISPLAY', true );
        define( 'SCRIPT_DEBUG', true );
        define( 'WP_DEBUG_LOG', true );
        @ini_set( 'display_errors', true );
        define( 'ENV', 'staging' );
        break;

    default : //production
        define( 'DB_NAME', 'keikfi' );
        define( 'DB_USER', 'siekfi' );
        define( 'DB_PASSWORD', 'lPC@8#u1B' );
        define( 'DB_HOST', 'localhost' );
        define( 'WP_DEBUG', false );
        define( 'ENV', 'pro' );
        break;
}


$protocol = ( ! empty( $_SERVER['HTTPS'] ) ) ? 'https://' : 'http://';

define( 'WP_SITEURL', $protocol . $_SERVER['HTTP_HOST'] );
define( 'WP_HOME', $protocol . $_SERVER['HTTP_HOST'] );

With above codes, you don't need to care local or production, it would work because we make website domain change on the fly. Right?

And also, you should test with table repair

define('WP_ALLOW_REPAIR', true);

The above solutions doesn't work, it is time to talk with host provider :)

  • Thank you for your answer but as I said I checked my wp-config.php file and can access the complete Backend so the Database connection IS WORKING . And I don't think that it is a problem with my hosting provider either because the code is running on a AWS EC2 Instance with a PHP package wich is also recommended by Wordpress when running on EC2. – Chris Nissen Jun 23 '18 at 12:23
0

I'm not sure of your server type or if it hosts more than one site, but could some other script be trying to connect? Like a rogue script from a previous configuration.. htaccess file?

It also sounds like the port may be blocked or in use, which I know is unlikely, but it sounds like it's time to look at the unlikely.

Also, of you haven't already, check your Dns for the domain, maybe there is an A record with the wrong IP hiding in there. I know I forget to remove the forward on occasion. I'll update the IP and wonder wtf is going on oy to find I forgot to remove the forward.

  • The site is hosted on an AWS EC2 instance with only the source code of my Wordpress Site in the var/www/html folder. – Chris Nissen Jun 23 '18 at 15:11
  • I also thought about some networking issue but since the wp-admin uses the same database connection established through PHP I don't think thats the problem either – Chris Nissen Jun 23 '18 at 15:13
  • I don't think it is a DNS issue either because I'm currently using the public DNS of the EC2 instance and I would think the DNS records are correct... or do you have any suggestions on how to check this type of error with a misconfiguration of the DNS is causing the public frontend not to work? – Chris Nissen Jun 23 '18 at 15:16
  • Unfortunately, I haven't used EC2 and wouldn't know where to look. I've only briefly looked at this type of setup and decided it was more work than I wanted. I used Cloud Ways when I wanted cloud based hosting. – Lisa Baird Jun 23 '18 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.