Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PRESTASHOP: How to tell PDO class to connect through a socket instead of 'localhost'?

In Prestashop 1.6.1 in the /classes/db/DbPDO.php in the function _getPDO I see that the PDO the dsn connection string is built conditionally. In particular the following snippet is of interest:

$dsn = 'mysql:';
if ($dbname)
        $dsn .= 'dbname='.$dbname.';';
if (preg_match('/^(.*):([0-9]+)$/', $host, $matches))
        $dsn .= 'host='.$matches[1].';port='.$matches[2];
elseif (preg_match('#^.*:(/.*)$#', $host, $matches))
        $dsn .= 'unix_socket='.$matches[1];
else
        $dsn .= 'host='.$host;

So, what is it expecting to see in the $host in order to set the connection type to unix_socket?

I imagine it's looking at the _DB_SERVER_ variable in the settings.inc.php. Is that right? Currently I have it set to 'localhost'. What would I need to change it to so that PDO connects using a socket /var/lib/mysql/mysql.sock?

Thank you for any suggestions, Raine

like image 418
hndcrftd Avatar asked Dec 03 '25 11:12

hndcrftd


1 Answers

Looking through the code, this appears to be the trick:

define('_DB_SERVER_', 'unix:/var/lib/mysql/mysql.sock');

What comes before the colon is irrelevant. The important thing is the string start at the beginning of a line, have a literal colon then slash (":/"), and everything thereafter is the absolute path to the socket file. This pattern, therefore, requires an absolute path to the socket.

The _DB_SERVER_ constant appears to be the canonical way to configure PrestaShop's database based on the docs.

like image 168
bishop Avatar answered Dec 05 '25 01:12

bishop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!