Issue:
Cannot connect to the localhost DB from the local apache server running PHP. I've experienced this issue with a couple of machines and with LAMPP stack and XAMPP on both Linux and Ubuntu.
This is assuming you have correctly provided the DB username, password, and the database to the application and still getting this error when connecting to the DB.
No matter what application you are trying to run Moodle, WordPress, or a PHP connection script you wrote, it doesn't connect if you give the localhost or 127.0.0.1 as the DB host.
PHP DB connection error |
WordPress DB Connection error |
Solution:
Make sure you have proper privileges given to the DB user in MySQL or MariaDB.
Note that I'm trying to set up WordPress and my DB name is wordpress1.
To check this, you have to select your DB in the PhpMyAdmin and navigate to the Privileges tab.
Privileges |
Once you are there, check the users.
DB Users |
If you have already created a user account for the DB, make sure that you have an account for the localhost as well. Ideally, if you have the hostname for the user account as "any" with the percentage mark "%" it should be sufficient. But apparently, it doesn't allow you to connect with localhost, so you must create another user account by allowing to connect from the localhost.
Comments
Post a Comment