Why can't I connect to a local MySQL server from a PHP application, but can connect from mysql-client

Hello -

I am very confused.

I have a single Ubuntu 18.04 LAMP server. The database is localhost, i.e. running on the same server. It’s replacing a current production Ubuntu 14 web server.

L = Ubuntu 18.04 A = Apache 2.4.43 M = MySQL 5.7.29 P = PhP 5.6.40

I can connect to the mysql-client with the credentials I established, and the schema I imported.

mysql -uroot -hlocalhost -p schema_name

However, when I install my application (the same as the one installed on the current 14 server) my error.log reports:

Access denied for user 'root'@'localhost' 

I’ve checked and double-checked that my application is receiving the same input for password, etc. that I’m passing to the cli client.

I created a new PHP file from this Q&A here:

My file can be found here:


Ubuntu 14:

There are 60 tables

Ubuntu 18

Unable to Connect to 'localhost'

I don’t think the issue has to do with Ubuntu 14 vs 18, but those are where the working and non-working examples exist.

Unfortunately this is still a PHP5.6 application, so it was harder to install the necessary LAMP version to Ubuntu 18. I did log what packages I installed. I wonder if the issue is missing software? Doesn’t make sense but I can’t think of anything else.

apt-get log:

sudo add-apt-repository ppa:ondrej/php
sudo add-apt-repository ppa:ondrej/apache2
sudo apt-get -y update
sudo apt-get -y install apache2
sudo apt-get -y install git
sudo apt-get -y install php5.6
sudo apt-get -y install php5.6-mbstring
sudo apt-get -y install libaio1
sudo apt-get -y install php5.6-mysql
sudo apachectl graceful

mysql-server 5.7.29 was also installed, but to get a compatible version, via Debian packages found here:

I’ve deployed this application to multiple servers and never had this issue. Thanks in advance for any ideas.

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.

Pinned Answer

This was resolved by creating a new mysql user. It’s strange because root typically has the broadest permissions, but in this case was being denied.

(I typically use root initially, for testing, for this exact reason, before following the principle of least privilege in creating an application user for use in PHP scripts.)

I also employed the mysql_secure_installation script, so maybe this modified root in some way that I’m not aware.

Thanks to @bobbyiliev for suggesting this fix.

Bobby Iliev
Site Moderator
Site Moderator badge
June 5, 2020

Hi there @tneigerux,

The script that you are using looks correct. What I could suggest is to try using as your database host rather than localhost.

If this does not work, what I would recommend is connecting to MySQL and running the following query:

SELECT user,host FROM mysql.user;

And then share the output here.

Let me know how this goes! Regards, Bobby

bobbyiliev said:

“what I could suggest is creating another MySQL user and use it instead of the root user.”

This resolved the issue. Something funky happened when I set up the database; the original root user account doesn’t work, but my new user does.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel