From c31e374a71a5109b136327b89b51a34f9bf8809f Mon Sep 17 00:00:00 2001 From: Michael Foster Date: Wed, 28 Aug 2013 20:09:30 +1000 Subject: [PATCH] Allow Unix sockets for database connection --- inc/config.php | 8 +++++--- inc/database.php | 12 ++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/inc/config.php b/inc/config.php index b6c510a7..5d572010 100644 --- a/inc/config.php +++ b/inc/config.php @@ -77,16 +77,18 @@ // Database driver (http://www.php.net/manual/en/pdo.drivers.php) // Only MySQL is supported by Tinyboard at the moment, sorry. $config['db']['type'] = 'mysql'; - // Hostname or IP address + // Hostname, IP address or Unix socket (prefixed with ":") $config['db']['server'] = 'localhost'; + // Example: Unix socket + // $config['db']['server'] = ':/tmp/mysql.sock'; // Login $config['db']['user'] = ''; $config['db']['password'] = ''; // Tinyboard database $config['db']['database'] = ''; - // Table prefix + // Table prefix (optional) $config['db']['prefix'] = ''; - // Use a persistent connection (experimental) + // Use a persistent connection (experimental; benefits unknown) $config['db']['persistent'] = false; // Anything more to add to the DSN string (eg. port=xxx;foo=bar) $config['db']['dsn'] = ''; diff --git a/inc/database.php b/inc/database.php index 24959d0c..8daa20b4 100644 --- a/inc/database.php +++ b/inc/database.php @@ -43,9 +43,17 @@ class PreparedQueryDebug { function sql_open() { global $pdo, $config; - if ($pdo) return true; + if ($pdo) + return true; - $dsn = $config['db']['type'] . ':host=' . $config['db']['server'] . ';dbname=' . $config['db']['database']; + if (isset($config['db']['server'][0]) && $config['db']['server'][0] == ':') + $unix_socket = substr($config['db']['server'], 1); + else + $unix_socket = false; + + $dsn = $config['db']['type'] . ':' . + ($unix_socket ? 'unix_socket=' . $unix_socket : 'host=' . $config['db']['server']) . + ';dbname=' . $config['db']['database']; if (!empty($config['db']['dsn'])) $dsn .= ';' . $config['db']['dsn']; try {