"Error establishing database connection" in subdirectory

Posted August 1, 2021 200 views

I am following this tutorial for three WordPress sites:

The first two were set up with no issues. The third, however, is in a subdirectory. I tested that there is a connection with
mysql -u <DB_USER> -p<DB_PASSWORD> -h <DB_HOST> <DB_NAME>
I also tested that it is reading my wp-config.php by adding
die(“Loading this file now”);
to the top. So it does display that line now.
I also looked at How To Debug the WordPress “Error Establishing Database Connection”
but none of those suggestions helped.

I am thinking it is perhaps related to the fact that I putting WordPress in a subdirectory:
Maybe some permission issue?

Any ideas?

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

Hi @leorawe,

It’s possible there is a permissions issue. You need to make your Folders 755 and your Files 644 permissions.

Additionally, what I think it’s mostly possible is your Password being wrong, at least what you’ve typed in your wp.config.php. Another common thing that occurs is the password containing the single quote symbol - ’ which is not being interpreted as part of the password.

Hello, @leorawe

First, you can ensure that the MySQL service is up and running:

  • sudo systemctl status MySQL

You can double-check the database credentials in the wp-config.php file and make sure that they are added correctly in the file, e.g no blank spaces or typos.

It’s worth attempting to connect to the database from the command line, just to make sure. Copy and paste the details right from the config file into the following command:

  • mysqlshow -u database_username -p

You’ll be prompted for a password. Paste it in and press ENTER. If you get an Access denied error, your username or password is incorrect.

You can also double-check the site files/folders permissions and ownerships. to set the correct permissions on the WordPress directories and files:

sudo find /var/www/wordpress/ -type d -exec chmod 750 {} \;
sudo find /var/www/wordpress/ -type f -exec chmod 640 {} \;

Another thing to check is the salt and key values in the wp-config.php file. More information can be found in our tutorial here:

Hope that this helps!

by Lisa Tagliaferri
WordPress is an extremely popular open-source technology and WordPress sites represent 36% of all websites that are currently online. This tutorial is intended for those who are comfortable with system administration or for those who are interested in learning how to administer your own WordPress instance on the server side. In this guide, we’ll install and set up WordPress via LAMP on a Linux Ubuntu 20.04 server.
  • Thank you for all the recommendations! I did try all of these.

    I will just give up on the subdirectory and set up like the others. I have an idea that my theme will solve my issue instead of trying this subdirectory method.

    • Hello, @leorawe

      This sounds like a plan. You can update this thread with the final result.


      • So I deleted the old sub sub directory and put all the wordpress files in public_html. I restarted apache. Now it is complaining that it cannot find the old directory, even though the conf file now has the new one and I do not want the old one at all. Is this a DNS delay?

        • Hello, @leorawe

          DNS changes might take up some time in order to fully update however, this does not sound like one.

          You can open the Apache configuration file and make sure to update the document root for the domain name as this is likely the issue here.


          • Thank you, Alex. I kept editing the .conf file, but not the conf file with the SSL. Once I updated that one, it brought up the WordPress installation page. Phew.

            One more question: I did an update of Ubuntu. I got this:

            A new version (/tmp/fileEediFq) of configuration file /etc/ssh/sshd_config is available, but the 
            version installed currently has been locally modified.

            I am new to doing server work, so I have no idea what to choose. Any advice or what to read to learn more would be helpful.

Hello, @leorawe

Normally the best thing to do is to keep the currently installed. Would you mind listing all the available options that you get?


  • I backed out of the previous update. I am not sure when to update the server software requests. These are the current options:

    linux-headers-generic/focal-updates amd64 [upgradable from:]
    linux-headers-virtual/focal-updates amd64 [upgradable from:]
    linux-image-virtual/focal-updates amd64 [upgradable from:]
    linux-virtual/focal-updates amd64 [upgradable from:]
    python3-distupgrade/focal-updates 1:20.04.36 all [upgradable from: 1:20.04.35]
    ubuntu-release-upgrader-core/focal-updates 1:20.04.36 all [upgradable from: 1:20.04.35]
    • Hello, leorawe

      If your applications are not tested with the latest updates or if you have concerns that the update may cause issues, you can delay the software update until you’re sure it’s safe to update.

      The other option will be to backup your apps and take a snapshot of the droplet and then proceed with the update and in case anything goes wrong you can easily revert everything back to its last working state.