1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
<?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&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&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]
</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
-->
|