From 7a3c86a110fee7d5947ce37b9b7a6ebc7e19cbaf Mon Sep 17 00:00:00 2001 From: Luffy <52o@qq52o.cn> Date: Thu, 20 Nov 2025 23:55:43 +0800 Subject: [PATCH] Support connect to mysql using socket (#1749) * Support connect to mysql using socket * pgsql can also use unix_socket fix Deprecated: pg_escape_string(): Automatic fetching of PostgreSQL connection * Update install.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update var/Typecho/Db/Adapter/Mysqli.php Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Fix quoteValue method to correctly escape single quotes in strings --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- install.php | 6 +++- var/Typecho/Db/Adapter/Mysqli.php | 54 +++++++++++++++++++--------- var/Typecho/Db/Adapter/Pdo/Mysql.php | 9 +++-- var/Typecho/Db/Adapter/Pgsql.php | 4 +-- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/install.php b/install.php index ce0c4a2b..02051438 100644 --- a/install.php +++ b/install.php @@ -1064,7 +1064,11 @@ function install_step_2_perform() // intval port number if (isset($dbConfig['port'])) { - $dbConfig['port'] = intval($dbConfig['port']); + if (strpos($dbConfig['host'], '/') !== false && $type == 'Mysql') { + $dbConfig['port'] = null; + } else { + $dbConfig['port'] = intval($dbConfig['port']); + } } // bool ssl verify diff --git a/var/Typecho/Db/Adapter/Mysqli.php b/var/Typecho/Db/Adapter/Mysqli.php index 2f3e5688..62aecfc0 100644 --- a/var/Typecho/Db/Adapter/Mysqli.php +++ b/var/Typecho/Db/Adapter/Mysqli.php @@ -5,6 +5,7 @@ namespace Typecho\Db\Adapter; use Typecho\Config; use Typecho\Db; use Typecho\Db\Adapter; +use mysqli_sql_exception; if (!defined('__TYPECHO_ROOT_DIR__')) { exit; @@ -49,26 +50,40 @@ class Mysqli implements Adapter { $mysqli = mysqli_init(); if ($mysqli) { - if (!empty($config->sslCa)) { - $mysqli->ssl_set(null, null, $config->sslCa, null, null); + try { + if (!empty($config->sslCa)) { + $mysqli->ssl_set(null, null, $config->sslCa, null, null); - if (isset($config->sslVerify)) { - $mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, $config->sslVerify); + if (isset($config->sslVerify)) { + $mysqli->options(MYSQLI_OPT_SSL_VERIFY_SERVER_CERT, $config->sslVerify); + } } - } - $mysqli->real_connect( - $config->host, - $config->user, - $config->password, - $config->database, - (empty($config->port) ? null : $config->port) - ); + $host = $config->host; + $port = empty($config->port) ? null : $config->port; + $socket = null; + if (strpos($host, '/') !== false) { + $socket = $host; + $host = 'localhost'; + $port = null; + } - $this->dbLink = $mysqli; + $mysqli->real_connect( + $host, + $config->user, + $config->password, + $config->database, + $port, + $socket + ); - if ($config->charset) { - $this->dbLink->query("SET NAMES '{$config->charset}'"); + $this->dbLink = $mysqli; + + if ($config->charset) { + $this->dbLink->query("SET NAMES '{$config->charset}'"); + } + } catch (mysqli_sql_exception $e) { + throw new ConnectionException($e->getMessage(), $e->getCode()); } return $this->dbLink; @@ -106,8 +121,13 @@ class Mysqli implements Adapter ?string $action = null, ?string $table = null ) { - if ($resource = @$this->dbLink->query($query)) { - return $resource; + try { + if ($resource = $this->dbLink->query($query)) { + return $resource; + } + } catch (mysqli_sql_exception $e) { + /** 数据库异常 */ + throw new SQLException($e->getMessage(), $e->getCode()); } /** 数据库异常 */ diff --git a/var/Typecho/Db/Adapter/Pdo/Mysql.php b/var/Typecho/Db/Adapter/Pdo/Mysql.php index 3e2f4c1d..3bc86fa3 100644 --- a/var/Typecho/Db/Adapter/Pdo/Mysql.php +++ b/var/Typecho/Db/Adapter/Pdo/Mysql.php @@ -61,9 +61,14 @@ class Mysql extends Pdo } } + $dsn = !empty($config->dsn) + ? $config->dsn + : (strpos($config->host, '/') !== false + ? "mysql:dbname={$config->database};unix_socket={$config->host}" + : "mysql:dbname={$config->database};host={$config->host};port={$config->port}"); + $pdo = new \PDO( - !empty($config->dsn) - ? $config->dsn : "mysql:dbname={$config->database};host={$config->host};port={$config->port}", + $dsn, $config->user, $config->password, $options diff --git a/var/Typecho/Db/Adapter/Pgsql.php b/var/Typecho/Db/Adapter/Pgsql.php index d26c9fbc..4d282026 100644 --- a/var/Typecho/Db/Adapter/Pgsql.php +++ b/var/Typecho/Db/Adapter/Pgsql.php @@ -119,7 +119,7 @@ class Pgsql implements Adapter /** * @param resource $resource - * @return array|null + * @return array */ public function fetchAll($resource): array { @@ -146,6 +146,6 @@ class Pgsql implements Adapter */ public function quoteValue($string): string { - return '\'' . pg_escape_string($string) . '\''; + return '\'' . str_replace('\'', '\'\'', $string) . '\''; } }