Question

WordPress: Cannot establish connection to database

Followed the instructions of the related tutorial (https://www.digitalocean.com/community/tutorials/how-to-install-wordpress-on-ubuntu-20-04-with-a-lamp-stack) to the letter, ended up WordPress not being able to establish a connection to the database. WordPress and database are on the same server (Ubuntu 20.04 with LAMP stack). I can login to mysql using the WordPress user and password, even when specifying -h localhost to mysql. DB info is properly given in wp-admin.php. WordPress resides in /var/www/wordpress, with a subdomain pointing at that folder on my server.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

Hi @karx11erx,

Following your question and after that the information in the answer you gave, I take it, you are able to connect to mysql using the following command:

mysql -u wordpress_user -p wordpress_db -h localhost

correct?

Can you please try using the above command and see if it works as it’s supposed to?

From the error you are receiving it seems like your database user does not have the permissions to access your website’s database. The above command should provide it that’s the case.

Another solution might be a special character you might have in your password. Be careful with quotes or double quotes - ’ and ", they might cause issues as well.

Regards, KFSys

I used the following script to locally test php access to mysql:

<?php
$dbname = 'wordpress_db';
$dbuser = 'wordpress_user';
$dbpass = 'my password';
$dbhost = "localhost";

try
{
    if ($db = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname))
    {
        echo ("successfully connected to database \n");
   }
    else
    {
        throw new Exception('Unable to connect');
    }
}
catch(Exception $e)
{
    echo $e->getMessage();
}
?>

When I ran the script from the cli, it produced the following message:

PHP Warning:  mysqli_connect(): (HY000/1045): Access denied for
user 'wordpress_user'@'localhost' (using password: YES) in
/var/www/wordpress/mysql-test.php on line 13
Unable to connect

However, when I connect to mysql with user “wordpress_user” and enter the correct password (which is also given in my actual script), I have no problems. Querying all available databases also shows that database “wordpress_db” is available.

Why can’t I connect when running a local php script?