2024 年日本 PHP 研討會

getSession

(無版本資訊,可能僅在 Git 中)

getSession連線到 MySQL 伺服器

說明

mysql_xdevapi\getSession(字串 $uri): mysql_xdevapi\Session

連線到 MySQL 伺服器。

參數

uri

MySQL 伺服器的 URI,例如 mysqlx://user:password@host

URI 格式

scheme://[user[:[password]]@]target[:port][?attribute1=value1&attribute2=value2...

  • scheme:必填,連線協定

    在 mysql_xdevapi 中,它始終是 'mysqlx'(用於 X 協定)

  • user:選用,用於驗證的 MySQL 使用者帳號

  • password:選用,用於驗證的 MySQL 使用者密碼

  • target:必填,連線所指的伺服器執行個體

    * TCP 連線(主機名稱、IPv4 位址或 IPv6 位址)

    * Unix 通訊端路徑(本地檔案路徑)

    * Windows 命名管道(本地檔案路徑)

  • port:選用,MySQL 伺服器的網路埠。

    X Protocol 的預設埠為 33060

  • ?attribute=value:此元素為選用,指定包含不同選項的資料字典,包括

    • auth(驗證機制)屬性與加密連線相關。如需更多資訊,請參閱 » 加密連線的命令選項。支援以下 'auth' 值:plainmysql41externalsha256_mem

    • connect-timeout 屬性影響連線,而不影響後續操作。無論是單一主機還是多個主機,每個連線都會設定此屬性。

      傳入正整數以定義連線逾時(以秒為單位),或傳入 0(零)以停用逾時(無限)。未定義 connect-timeout 則使用預設值 10。

      相關地,可以設定 MYSQLX_CONNECTION_TIMEOUT(逾時,以秒為單位)和 MYSQLX_TEST_CONNECTION_TIMEOUT(在執行測試時使用)環境變數,並用於取代 URI 中的 connect-timeout。connect-timeout URI 選項優先於這些環境變數。

    • 選用的 compression 屬性接受以下值:preferred(用戶端與伺服器協商以找到支援的演算法;如果找不到相互支援的演算法,則連線未壓縮)、required(類似「preferred」,但如果找不到相互支援的演算法,則終止連線)或 disabled(連線未壓縮)。預設為 preferred

      此選項已在 8.0.20 版中新增。

    • 選用的 compression-algorithms 屬性定義所需的壓縮演算法(及其偏好的使用順序):zstd_stream(別名:zstd)、lz4_message(別名:lz4)或 deflate_stream(別名:deflate 或 zlib)。預設情況下,使用的順序(取決於系統可用性)為 lz4_message、zstd_stream,然後是 deflate_stream。例如,傳入 compression-algorithms=[lz4,zstd_stream] 會在 lz4 可用時使用它,否則使用 zstd_stream。如果兩者都不可用,則行為取決於 compression 值,例如,如果 compression=required,則會因錯誤而失敗。

      此選項已在 8.0.22 版中新增。

範例 #1 URI 範例

mysqlx://foobar
mysqlx://root@localhost?socket=%2Ftmp%2Fmysqld.sock%2F
mysqlx://foo:bar@localhost:33060
mysqlx://foo:bar@localhost:33160?ssl-mode=disabled
mysqlx://foo:bar@localhost:33260?ssl-mode=required
mysqlx://foo:bar@localhost:33360?ssl-mode=required&auth=mysql41
mysqlx://foo:bar@(/path/to/socket)
mysqlx://foo:bar@(/path/to/socket)?auth=sha256_mem
mysqlx://foo:bar@[localhost:33060, 127.0.0.1:33061]
mysqlx://foobar?ssl-ca=(/path/to/ca.pem)&ssl-crl=(/path/to/crl.pem)
mysqlx://foo:bar@[localhost:33060, 127.0.0.1:33061]?ssl-mode=disabled
mysqlx://foo:bar@localhost:33160/?connect-timeout=0
mysqlx://foo:bar@localhost:33160/?connect-timeout=10&compression=required
mysqlx://foo:bar@localhost:33160/?connect-timeout=10&compression=required&compression-algorithms=[lz4,zstd_stream]

相關資訊,請參閱 MySQL Shell 的 » 使用 URI 字串連線

傳回值

一個 Session 物件。

錯誤/例外

連線失敗會擲出 Exception

範例

範例 #2 mysql_xdevapi\getSession() 範例

<?php
try {
$session = mysql_xdevapi\getSession("mysqlx://user:password@host");
} catch(
Exception $e) {
die(
"無法建立連線: " . $e->getMessage());
}

$schemas = $session->getSchemas();
print_r($schemas);

$mysql_version = $session->getServerVersion();
print_r($mysql_version);

var_dump($collection->find("name = 'Alfred'")->execute()->fetchOne());
?>

以上範例會輸出類似以下的內容

Array
(
    [0] => mysql_xdevapi\Schema Object
        (
            [name] => helloworld
        )
    [1] => mysql_xdevapi\Schema Object
        (
            [name] => information_schema
        )
    [2] => mysql_xdevapi\Schema Object
        (
            [name] => mysql
        )
    [3] => mysql_xdevapi\Schema Object
        (
            [name] => performance_schema
        )
    [4] => mysql_xdevapi\Schema Object
        (
            [name] => sys
        )
)

80012

array(4) {
  ["_id"]=>
  string(28) "00005ad66abf0001000400000003"
  ["age"]=>
  int(42)
  ["job"]=>
  string(7) "Butler"
  ["name"]=>
  string(4) "Alfred"
}
新增註解

使用者貢獻的註解

此頁面沒有使用者貢獻的註解。
To Top