<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->

<refentry xml:id="function.mysql-xdevapi-getsession" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>getSession</refname>
  <refpurpose>Connect to a MySQL server</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <type>mysql_xdevapi\Session</type><methodname>mysql_xdevapi\getSession</methodname>
   <methodparam><type>string</type><parameter>uri</parameter></methodparam>
  </methodsynopsis>
  <para>
   Connects to the MySQL server.
  </para>
 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <variablelist>
   <varlistentry>
    <term><parameter>uri</parameter></term>
    <listitem>
     <para>
      The URI to the MySQL server, such as <literal>mysqlx://user:password@host</literal>.
     </para>
     <para>
      URI format:
     </para>
     <para>
      <code>scheme://[user[:[password]]@]target[:port][?attribute1=value1&amp;attribute2=value2...</code>
     </para>
     <itemizedlist>
      <listitem>
        <para><literal>scheme</literal>: required, the connection protocol</para>
        <para>In mysql_xdevapi it is always 'mysqlx' (for X Protocol)</para>
      </listitem>
      <listitem>
        <para><literal>user</literal>: optional, the MySQL user account for authentication</para>
      </listitem>
      <listitem>
        <para><literal>password</literal>: optional, the MySQL user's password for authentication</para>
      </listitem>
      <listitem>
        <para><literal>target</literal>: required, the server instance the connection refers to:</para>
        <para>* TCP connection (host name, IPv4 address, or IPv6 address)</para>
        <para>* Unix socket path (local file path)</para>
        <para>* Windows named-pipe (local file path)</para>
      </listitem>
      <listitem>
        <para><literal>port</literal>: optional, network port of MySQL server.</para>
        <para>by default port for X Protocol is 33060</para>
      </listitem>
      <listitem>
        <para>
          <literal>?attribute=value</literal>: this element is optional and specifies a data dictionary 
          that contains different options, including:
        </para>
        <itemizedlist>
          <listitem>
          <para>
            The <literal>auth</literal> (authentication mechanism) attribute as it relates to encrypted connections.
            For additional information, see <link xlink:href="&url.mysql.docs.connect.encryption;">Command
            Options for Encrypted Connections</link>.
            The following 'auth' values are supported: <literal>plain</literal>,
            <literal>mysql41</literal>, <literal>external</literal>, and <literal>sha256_mem</literal>.
          </para>
          </listitem>
          <listitem>
          <para>
           The <literal>connect-timeout</literal> attribute affects the connection
           and not subsequent operations. It is set per connection whether on
           a single or multiple hosts.
          </para>
          <para>
           Pass in a positive integer to define the connection timeout in seconds,
           or pass in 0 (zero) to disable the timeout (infinite). Not defining
           connect-timeout uses the default value of 10.
          </para>
          <para>
            Related, the MYSQLX_CONNECTION_TIMEOUT (timeout in seconds) and MYSQLX_TEST_CONNECTION_TIMEOUT (used while running tests)
            environment variables can be set and used instead of connect-timeout in the URI. The
            connect-timeout URI option has precedence over these environment variables.
          </para>
          </listitem>
          <listitem>
            <para>
              The optional <literal>compression</literal> attribute accepts these values: 
              <literal>preferred</literal> (client negotiates with server to find a supported algorithm; connection is uncompressed if a mutually supported algorithm is not found), 
              <literal>required</literal> (like "preferred", but connection is terminated if a mutually supported algorithm is not found), or
              <literal>disabled</literal> (connection is uncompressed). Defaults to <literal>preferred</literal>.
            </para>
            <para>
              This option was added in version 8.0.20.
            </para>
          </listitem>
          <listitem>
            <para>
              The optional <literal>compression-algorithms</literal> attribute defines 
              the desired compression algorithms (and their preferred usage order): 
              <literal>zstd_stream</literal> (alias: zstd),
              <literal>lz4_message</literal> (alias: lz4), or
              <literal>deflate_stream</literal> (aliases: deflate or zlib).
              By default, the order used (depending on system availability) is lz4_message, zstd_stream, then deflate_stream.
              For example, passing in compression-algorithms=[lz4,zstd_stream] uses lz4 if it's available, otherwise
              zstd_stream is used. If both are unavailable then behavior depends on the compression value
              e.g., if compression=required then it'll fail with an error.
            </para>
            <para>
              This option was added in version 8.0.22.
            </para>
          </listitem>
        </itemizedlist>
       </listitem>
      </itemizedlist>

  <example>
  <title>URI examples</title>
  <programlisting role="php">
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&amp;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)&amp;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&amp;compression=required
mysqlx://foo:bar@localhost:33160/?connect-timeout=10&amp;compression=required&amp;compression-algorithms=[lz4,zstd_stream]
  </programlisting>
</example>

   <para>
    For related information, see MySQL Shell's
    <link xlink:href="&url.mysql.docs.connect.shell.uri;">Connecting using a URI String</link>.
   </para>

    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
   A <classname>Session</classname> object.
  </para>
 </refsect1>

 <refsect1 role="errors">
  &reftitle.errors;
  <para>
   A connection failure throws an <classname>Exception</classname>.
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <example>
   <title><function>mysql_xdevapi\getSession</function> example</title>
   <programlisting role="php">
<![CDATA[
<?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());
?>
]]>
   </programlisting>
   &example.outputs.similar;
   <screen>
<![CDATA[
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"
}
]]>
   </screen>
  </example>
 </refsect1>


</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
