2

I am trying to connect to a mariadb database using PHP without success. Each attempt I get the following:

Error: Unable to connect to MySQL. Debugging errno: 1045 Debugging error: Access denied for user 'gt_user'@'localhost' (using password: YES)

I have tried all many attempts. Have even reverted to a simple index.php test which never connects.

Connecting from the command line with the same user credentials works fine

mysql -u USER -p -h localhost database

I have even tried giving the user all privileges as well as created the same user with @'%' rather than @'localhost'

Here is the simple test php code I am trying:

<?php
$link = mysqli_connect("localhost", "gt_user", "passpass", "gt_monitor");

if (!$link) {
    echo "Error: Unable to connect to MySQL." . PHP_EOL;
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL;
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL;
    exit;
}

echo "Success: A proper connection to MySQL was made! The my_db database is great." . PHP_EOL;
echo "Host information: " . mysqli_get_host_info($link) . PHP_EOL;

mysqli_close($link);
?>

I have also tried from browser on local host as well as remote. Same error.

Im thinking it has to do with missing dependencies. Not sure how to thoroughly check. Running PHP 7.0.33 and mariadb 10.1.37 on Raspberry pi on latest Rasbian Stretch.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • When you connect from the CLI, are you actually using `-u USER` or `-u gt_user`? That error message typically means either your username and / or password is incorrect. It has nothing to do with dependencies – Phil Apr 05 '19 at 05:34
  • Also, have you executed `FLUSH PRIVILEGES` after making changes to the user table? – Phil Apr 05 '19 at 05:35
  • yes I am using gt_user and have flushed privileges. – sir_ripsalot Apr 05 '19 at 11:38

2 Answers2

0

Managed to resolve this.

Basically removed mysql cleanly, reinstalled and setup database again. Inserted some data and retested the test index.php which worked first go.

For future reference, these are the steps I took below to get back on track:

 sudo apt-get remove --purge mysql-server mysql-client mysql-common -y
 sudo apt-get autoremove -y
 sudo apt-get autoclean

And the to reinstall (also to get working with raspberry pi GPIO and Python:

sudo apt update
sudo apt install mariadb-server
sudo apt-get -y install python-mysql.connector
pip install mysql-connector
pip install mysql-connector-repackaged
pip install mysql-connector-python
pip install mysql-connector-python-dd 
pip install mysql-connector-python-rf

I did limit the gt_user password to upper/lower case letters and numbers only. No special characters.

Hope this helps someone in the future.

  • Oh and the command for creating the user against the table in the database GRANT ALL ON gt_monitor.* TO gt_user@localhost IDENTIFIED BY 'password'; – sir_ripsalot Apr 05 '19 at 12:42
0

I had identical problem.

Finally what worked for me was using PDO to connect instead of mysqli_connect().

$myPDO = new PDO('mysql:host=localhost;dbname=dbname', 'username', 'password');

INTERESTING AND FRUSTRATING PROBLEM. for sure.