Question

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: https://www.digitalocean.com/community/questions/why-can-t-i-connect-to-a-remote-mysql-server-from-a-php-application-but-can-connect-from-mysql-client

My file can be found here: https://gist.github.com/tneigerux/499931aae84441d1ee8c667cedac8c70

Results:

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: https://downloads.mysql.com/archives/community/

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

Subscribe
Share

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

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.

Hi there @tneigerux,

The script that you are using looks correct. What I could suggest is to try using 127.0.0.1 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.