getSession 함수

(사용 가능한 버전 정보가 없으며 Git에만 있을 수 있음)

getSession — MySQL 서버에 연결


설명

mysql_xdevapi\getSession(string $uri): mysql_xdevapi\Session

MySQL 서버에 연결합니다.


매개변수

uri
mysqlx://user:password@host와 같은 MySQL 서버에 대한 URI입니다.

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 프로토콜의 기본 포트는 33060입니다.

  • ?attribute=value: 이 요소는 선택 사항이며 다음을 포함한 다양한 옵션이 포함된 데이터 사전을 지정합니다.
    • 암호화된 연결과 관련된 auth(인증 메커니즘) 속성입니다. 자세한 내용은 » Command Options for Encrypted Connections을 참조하세요. 지원되는 'auth' 값은 plain, mysql41, externalsha256_mem입니다.
    • connect-timeout 속성은 연결에 영향을 미치고 후속 작업에는 영향을 미치지 않습니다. 단일 또는 다중 호스트에서 연결별로 설정됩니다.

      연결 시간 초과를 초 단위로 정의하려면 양의 정수를 전달하고, 시간 초과(무한)를 비활성화하려면 0(영)을 전달합니다. connect-timeout을 정의하지 않으면 기본값 10이 사용됩니다.

      관련하여 MYSQLX_CONNECTION_TIMEOUT(초 단위 시간 제한) 및 MYSQLX_TEST_CONNECTION_TIMEOUT(테스트 실행 중 사용) 환경 변수를 URI에서 연결 제한 시간 대신 설정하여 사용할 수 있습니다. 연결 시간 초과 URI 옵션은 이러한 환경 변수보다 우선합니다.

    • 선택적 compression 속성은 다음 값을 허용합니다. preferred(클라이언트는 지원되는 알고리즘을 찾기 위해 서버와 협상합니다. 상호 지원되는 알고리즘을 찾을 수 없는 경우 연결은 압축 해제됨), required(예: "선호"이지만 상호 지원되는 알고리즘이 다음과 같은 경우 연결이 종료됩니다. 찾을 수 없음) 또는 disabled(연결이 압축되지 않음). 기본적으로 선호됩니다.

      이 옵션은 버전 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=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 개체입니다.


오류/예외

연결 실패는 예외를 발생시킵니다.


Examples

예제 #2 mysql_xdevapi\getSession() 예제

                  
<?php
try {
    $session = mysql_xdevapi\getSession("mysqlx://user:password@host");
} catch(Exception $e) {
    die("Connection could not be established: " . $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"
}