Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support connect to mysql using socket #1749

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -1065,6 +1065,9 @@ 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;
}
}

// bool ssl verify
Expand Down
54 changes: 37 additions & 17 deletions var/Typecho/Db/Adapter/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Typecho\Config;
use Typecho\Db;
use Typecho\Db\Adapter;
use mysqli_sql_exception;

if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
Expand Down Expand Up @@ -49,26 +50,40 @@ public function connect(Config $config): \mysqli
{
$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);
}
}

$host = $config->host;
$port = empty($config->port) ? null : $config->port;
$socket = null;
if (strpos($host, '/') !== false) {
$socket = $host;
$host = 'localhost';
$port = null;
}
}

$mysqli->real_connect(
$config->host,
$config->user,
$config->password,
$config->database,
(empty($config->port) ? null : $config->port)
);
$mysqli->real_connect(
$host,
$config->user,
$config->password,
$config->database,
$port,
$socket
);

$this->dbLink = $mysqli;
$this->dbLink = $mysqli;

if ($config->charset) {
$this->dbLink->query("SET NAMES '{$config->charset}'");
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;
Expand Down Expand Up @@ -106,8 +121,13 @@ public function query(
?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());
}

/** 数据库异常 */
Expand Down
9 changes: 7 additions & 2 deletions var/Typecho/Db/Adapter/Pdo/Mysql.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ public function init(Config $config): \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
Expand Down
4 changes: 2 additions & 2 deletions var/Typecho/Db/Adapter/Pgsql.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function fetchObject($resource): ?\stdClass

/**
* @param resource $resource
* @return array|null
* @return array
*/
public function fetchAll($resource): array
{
Expand All @@ -146,6 +146,6 @@ public function affectedRows($resource, $handle): int
*/
public function quoteValue($string): string
{
return '\'' . pg_escape_string($string) . '\'';
return '\'' . str_replace("'", '"', $string) . '\'';
}
}