Need help with using localhost on php scripts to connect to MariaDB

SYSTEM INFORMATION
OS type and version Debian Linux 12
Virtualmin version 7.20.2

Hello to everyone here,

First of all, sorry if my English is not too good.

I am facing a problem that I couldn’t solve browsing the web nor the forums :confused:

I set up a simple Virtualmin server, and created a few virtual servers. Http browsing, going to phpMyAdmin goes well, I can browse databases from there no issues.
I used a simple test script to test PHP connection to MySQL on one of the site which is :

try
{
$bdd = new PDO(‘mysql:host=’.$host.‘;dbname=’.$base,$user,$passe);
}
catch(Exception $e)
{
die('Erreur : '.$e->getMessage());
}

When $host value is localhost, I get the error : SQLSTATE[HY000] [2002] No such file or directory
When i put 127.0.0.1, the connection works.

Already tried :
I already looked at php configs to set the pdo and mysqli default sockets to /run/mysqld/mysqld.sock, restarted Apache, PHP-FPM, MariaDB

I checked file permissions and ownership on the socket file which seems correct to me

srwxrwxrwx 1 mysql mysql 0 28 nov. 15:52 /run/mysqld/mysqld.sock

From browsing and searching for a solution, it seems I need to find a way for my site on my virtual server to be able to connect through localhost which is a socket, and not the TCP way with 127.0.0.1. But getting no success.

Could someone help me understand what I’m doing wrong and solve this ?

Let me know if any further info is needed.

Thanks !

Update, disabling the Jailkit for the domain makes it work. But I still would like to enable it. Shoud I edit the ini file to add the path to the mysqld.sock to make it work ?

@InsaneBen,

Check the file:

Is “127.0.0.1” mapped to “localhost”?

Hello @tpnsolutions

Yes it is.

From what I keep on seeing, it seems it’s a problem that php in a jailed virtual server cannot access the MySQL socket which is in /var/run/mysqld/mysqld.sock.

@InsaneBen,

It seems the solution is to use the IP address instead of Hostname which causes the connection to make a “TCP” connection rather than a “socket” connection.

If their jail doesn’t have /etc/hosts in it, it obviously can’t work.

A jail must have every file that is needed, because a jailed program can’t go outside the jail to find the system /etc/hosts file, or whatever else it needs.

I ended up removing the jailkit from the virtual servers. What I was afraid doing that was that connecting through FTP would allow browsing directories or viewing homes they shouldn’t. But ProFTPd does the job of limiting users to their homes.
Since they only use FTP to upload files and phpMyAdmin to manage DB, no SSH or anything, the jailkit on the Virtual Server is overkill and not needed.

Took me few days to finally understand this.

1 Like

Yes, chroot jails do not apply to ProFTPd logins, but ProFTPd has its own chroot features unrelated to jailkit (and safer).

chroot jails are for the specific case where you must provide ssh access, for some reason, but you also must restrict the user severely in what they can do.

1 Like