Going through and updating systems to newer versions of PHP8.1 and mariadb on a newly installed FreeBSD 13.1 system. When attempting to connect to the default “localhost”, I was being greeted with:
mysqli_real_connect(): (HY000/2002): No such file or directory
Any php database connection attempts to localhost were failing, which is the default for a variety of code out there, including phpMyAdmin and WordPress. Changing the connection host to “127.0.0.1” instead of “localhost” allowed it to connect, but I didn’t find this to be an acceptable solution as it should work for both. After doing some searching, I found the following solution to be the best option:
You need to modify the following file:
/usr/local/etc/php/ext-20-mysqli.ini
and add in the following two lines:
pdo_mysql.default_socket="/var/run/mysql/mysql.sock"
mysqli.default_socket="/var/run/mysql/mysql.sock"
Restarting Apache after these lines were added allowed everything to work as expected whether using “127.0.0.1” or “localhost” in the connection strings. This is because “localhost” is considered to be a sockets-based connection to mysql/mariadb, and that variable seems not to be defined in the pkg version of the mysqli extension. Hopefully this helps others who run into this.