Can't connect to mariaDB, don't know the hostname?

I’m trying to use this to connect to my DB:

<?php
$servername = "localhost";
$username = "root";
$password = "{hidden}";

try {
  $conn = new PDO("mysql:host=$servername;dbname=testdb", $username, $password);
  // set the PDO error mode to exception
  $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  echo "Connected successfully";
} catch(PDOException $e) {
  echo "Connection failed: " . $e->getMessage();
}
?>

but I’m getting the connection failure error:

Connection failed: SQLSTATE[HY000] [2002] No such file or directory

What am I doing wrong?

How is Mariadb installed, where is the executing host installed ? … If it’s via VM or containers, it’s probably not localhost.(certainly not portless)

instead of “localhost” did you try 127.0.0.1 ?

is the php file on the NAS or on a different server?

do you have phpMyAdmin installed so you can easily check the user permissions?