1

I'm having a bit of an issue with a MariaDB I'm running. I'm trying to connect to a database from a PHP page running on a client device, but keep receiving the error message: [Warning] Access denied for user 'rphsp'@'raspberrypi.lan' (using password: YES). I've double checked my grants and PHP code and have even tried creating the MariaDB users 'rphsp'@'raspberrypi.lan' and 'rphsp'@'%' without any luck. Is there something wrong with the way I've defined the "rphsp" MariaDB user? I'm able to read and write to the same database with that user in a Python script running on the same client device as the PHP page.

I've included all of the information I could think to include below, but please let me know if anything else would be useful. Any ideas or guidance is welcome and please let me know if you have any questions. Thanks in advance!

  • Client: raspberrypi.lan (192.168.1.125)
  • MariaDB Server: piserver.lan (192.168.1.250)
select host, user, password from mysql.user;
+---------------------------+-------+----------+
| host                      | user  | password |
+---------------------------+-------+----------+
| localhost                 | root  |          |
| localhost                 | pi    |          |
| 192.168.1.0/255.255.255.0 | rphsp |          |
+---------------------------+-------+----------+
show grants for rphsp@'192.168.1.0/255.255.255.0';
+--------------------------------------------------------------------------+
| Grants for rphsp@192.168.1.0/255.255.255.0                               |
+--------------------------------------------------------------------------+
| GRANT USAGE ON *.* TO 'rphsp'@'192.168.1.0/255.255.255.0'                |
| GRANT ALL PRIVILEGES ON `rphsp`.* TO 'rphsp'@'192.168.1.0/255.255.255.0' |
+--------------------------------------------------------------------------+
<?php
  echo "Content from MariaDB";
  echo "<br>";

  $servername = "piserver.lan";
  $username = "rphsp";
  $database = "rphsp";

  // Create connection
  $conn = new mysqli($servername, $username, $database);

  // Check connection
  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  }
  echo "Connected successfully";
  echo "<br>";

  $sql = "SELECT * from sensors";
  $result = $conn->query($sql);

  $conn->close();
?>
Kevin Tate
  • 45
  • 3
  • it looks like a CIDR notation is used instead of the 255.255.x.x notation in the [create user](https://mariadb.com/kb/en/create-user/). Try `192.168.1.0/24` – danblack Apr 11 '20 at 04:40
  • Thanks for the suggestion. I tried the following and reloaded the MariaDB service, but unfortunately it didn't fix the issue. I'm still getting the same error. `create user 'rphsp'@'192.168.1.0/24';` `grant all privileges on rphsp.* TO 'rphsp'@'192.168.1.0/24;` – Kevin Tate Apr 11 '20 at 05:08
  • 1
    shouldn't the create statement contain a password? like `create user 'rphsp'@'192.168.1.0/24' identified by '123';` – Taggart Comet Apr 11 '20 at 06:20
  • From everything that I read, you should be able to use a MariaDB user without a password (which is working fine with my Python program). However, I added a password to this account to test and lo-and-behold that corrected the error and everything is working now. Thanks for the suggestion. I'm a little confused as to why Python doesn't have any issues connecting to the MariaDB database with a passwordless user account, but PHP does. Any thoughts on that? – Kevin Tate Apr 11 '20 at 06:54
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Apr 11 '20 at 11:07

0 Answers0