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.

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.

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

  • No change from 127.0.0.1 except:

    Unable to Connect to '127.0.0.1'
    

    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
    owners.
    
    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:

      <?php
      # Fill our vars and run on cli
      # $ php -f db-connect-test.php
      
      $dbname = 'REDACTED';
      $dbuser = 'root';
      $dbpass = 'REDACTED';
      $dbhost = "localhost";
      
      
      try
      {
          if ($db = mysqli_connect($dbhost, $dbuser, $dbpass))
          {
              //do something
          }
          else
          {
              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.

      Regards,
      Bobby

      • 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
        
Submit an Answer