hndcrftd
hndcrftd

Reputation: 3210

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

Upvotes: 3

Views: 2049

Answers (2)

felipsmartins
felipsmartins

Reputation: 13549

Not sure I've get it. But firstly you must locate the mysql unix socket file path. Usually it is in my.cnf mysql config (in my Debian: /etc/mysql/my.cnf). Then search socket entry under [client]. So:

# see my.cnf under [client]
$unix_socket = '/var/run/mysqld/mysqld.sock'; 
$user   = 'user';
$passwd = 'secret';
$dsn = 'mysql:unix_socket=' . $unix_socket . ';dbname=dbname';

$pdo = new PDO($dsn, $user, $passwd);

Well, note When the host name is set to "localhost", then the connection to the server is made thru a domain socket by default.

Upvotes: 0

bishop
bishop

Reputation: 39364

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.

Upvotes: 3

Related Questions