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

Posted June 5, 2020 5k views
MySQLPHPUbuntu 18.04

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.

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
3 answers
tneigerux June 7, 2020
Pinned Answer
Show answer This answer has been marked as resolved by tneigerux.

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!

  • No change from except:

    Unable to Connect to ''

    Suggested query output:

    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 5
    Server version: 5.7.29 MySQL Community Server (GPL)
    Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    mysql> SELECT user,host FROM mysql.user;
    | user          | host      |
    | mysql.session | localhost |
    | mysql.sys     | localhost |
    | root          | localhost |
    3 rows in set (0.00 sec)
    • Hi there @tneigerux,

      So this looks correct, what I could suggest then is to actually use try-catch to get the exact error as the die message might be misleading.

      Try using this script here:

      # Fill our vars and run on cli
      # $ php -f db-connect-test.php
      $dbname = 'REDACTED';
      $dbuser = 'root';
      $dbpass = 'REDACTED';
      $dbhost = "localhost";
          if ($db = mysqli_connect($dbhost, $dbuser, $dbpass))
              //do something
              throw new Exception('Unable to connect');
      catch(Exception $e)
          echo $e->getMessage();

      Let me know how it goes! Feel free to share the error here as well.


      • Output

        Warning: mysqli_connect(): (HY000/1698): Access denied for user 'root'@'localhost' in /var/www/html/public/dbtest.php on line 12
        Warning: mysqli_select_db() expects parameter 1 to be mysqli, boolean given in /var/www/html/public/dbtest.php on line 13
        Warning: mysqli_query() expects parameter 1 to be mysqli, boolean given in /var/www/html/public/dbtest.php on line 16
        Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /var/www/html/public/dbtest.php on line 19
        There are no tables

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.