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.

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
2 answers

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?

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

  • That command works. I can connect to mysql when entering it in the cli.

    I have made the password rather simple to make sure the problem is not caused by special characters.

  • I have found the problem. I had simply misspelled the password in my script and the WordPress wp-config.php. Some characters look so similar …

    • I’m glad to hear you were able to resolve the issue! It’s always the little things that get us.

      Anyway, thank you for the contribution to the community! If you ever need anything else make sure to post it here!