Cannot connect to database

Discussion in 'General' started by rsteuer, Dec 13, 2023.

  1. rsteuer

    rsteuer Member

    I'm pulling my hair out. The home page on our site responds to queries, but any link returns a 404 NOT Found error.
    We are running the Control Panel, a web server, 2 DNS servers and a database server in cluster mode. The db is running on the db server and has been flawless until recently. I can't define when the issue occurred because we have a monitoring system that expects a page to return. If it fails, we receive immediate notification. Because the home page loaded, there was never a failure to be reported. Problem is, none of the database related pages will load.
    In troubleshooting, I've tested connection from the web server to the db server using CLI with the mysql credentials and all works fine. Access to the db is successful and I can run queries against the db. However, if I try to test connection from a browser using a php file, I receive the error: Could not open the db 'database_db'.
    I've also moved the database to the web server so that it was a localhost connection with the same results - I can connect locally via the CI but the php file fails.
    I have scoured what log files I can find, but nothing indicates any error.
    I am at a loss. Any suggestions would be greatly appreciated.
     
  2. pyte

    pyte Well-Known Member HowtoForge Supporter

    Can you try to run it with a testfile?

    Code:
    <?php
    $servername = "localhost";
    $database = "database";
    $username = "username";
    $password = "password";
    
    $conn = mysqli_connect($servername, $username, $password, $database);
    
    if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
    }
    
    echo “Connected successfully”;
    
    mysqli_close($conn);
    ?>
    Change the settings at the top accordingly and see what the result is. Does it work when you open it via the browser? Does it work when running it on the cli with "php scriptname.php"?
     
  3. Taleman

    Taleman Well-Known Member HowtoForge Supporter

    Have you looked at what the database contains now? There are jolly crackers that empty database and demand ransom.
    Check database with the tool that verifies consistency, can't remember what it's called but PHPMyAdmin can do it.
     
    Last edited: Dec 13, 2023
  4. rsteuer

    rsteuer Member

    Hi pyte, thanks for the reply. Yes, I can connect using a php script and can also connect using mysql on the localhost. 1reponse from the test php script:
    upload_2023-12-13_11-22-47.png
    It appears to be an issue with the wp-config.php, although the settings are identical. What is odd, is that the home page loads fine (parts are pulled from the db, as far as i know), but when clicking on any menu items, I get a 404 error. No entries in any of the logs that I can find.
    Also, I can access the database and tables using the credentials from the mysql cli.

    Thanks, Taleman, too. the database is present and accounted for.

    Still at a loss.
     
  5. pyte

    pyte Well-Known Member HowtoForge Supporter

    There are a lot of articles and guides and videos available online on how to debug Wordpress database issues and even repair them.
    There even is a builtin "Autorepair" kinda thingy, search for "WP_ALLOW_REPAIR".
    May have a read through these articles and check your installation.

    The database connection seems fine. This seems to be a Wordpress related issue. Maybe database or file corruption while moving the site?
     
  6. rsteuer

    rsteuer Member

    Thanks pyte. I've already tried the repair with no errors found. I've been through more articles than you can imagine, but nothing comes up with the type of issue I'm dealing with. When I connect to the database through mysql, all of the queries return info that is expected. I have never run into a corrupt database before, so I'm assuming some of the queries would fail, unless I'm not hitting any data that's actually corrupt. Again, not sure what to expect.

    I may try recovering the database after trying a few more things.
     
  7. pyte

    pyte Well-Known Member HowtoForge Supporter

    These 404 issues are usually related to misconfigured .htacces, misconfigured SSL or http/HTTPS linking or misconfigured shortlinks in Wordpress.

    Thats at least my very limited expirience
     
  8. rsteuer

    rsteuer Member

    Thanks again pyte. Your note struck a chord, so I replaced the existing .htaccess with a default file and he site came back online. No idea what caused it, as the .htaccess file was timestamped from 4 years ago and has been working fine. There were upgrades just applied to WordPress and the website theme. I can only assume something in one of those broke the db connection.

    I'm going to try to troubleshoot the original .htaccess to see if I can identify the root cause, but I don't hold out a lot of hope.
     
  9. pyte

    pyte Well-Known Member HowtoForge Supporter

    The .htaccess file has nothing to do with that. I assume there never was an issue with the database at all. The redirections in the .htaccess file seem to be messed up which resulted in url rewrites that ended up pointing to a directory/file on the webspace that didn't exists hence the 404.

    But i'm glad it's working now.
     
  10. rsteuer

    rsteuer Member

    Evidently, the original .htaccess must have been corrupt. I modified the replacement default .htaccess so that it was identical to the problem child and all was fine. I replaced that with the original and the site would not come up. Went back to the replacement and everything came up without issue. All is good in the world again.

    Thanks for your help. Greatly appreciate it.
     

Share This Page