mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 13:57:03 +08:00
198 lines
9.9 KiB
HTML
198 lines
9.9 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<title>Connect to a MySQL server</title>
|
|
</head>
|
|
<body class="docs"><div id="layout">
|
|
<div id="layout-content"><div id="function.mysql-xdevapi-getsession" class="refentry">
|
|
<div class="refnamediv">
|
|
<h1 class="refname">getSession</h1>
|
|
<p class="verinfo">(No version information available, might only be in Git)</p><p class="refpurpose"><span class="refname">getSession</span> — <span class="dc-title">Connect to a MySQL server</span></p>
|
|
|
|
</div>
|
|
|
|
<div class="refsect1 description" id="refsect1-function.mysql-xdevapi-getsession-description">
|
|
<h3 class="title">说明</h3>
|
|
<div class="methodsynopsis dc-description">
|
|
<span class="methodname"><strong>mysql_xdevapi\getSession</strong></span>
|
|
( <span class="methodparam"><span class="type">string</span> <code class="parameter">$uri</code></span>
|
|
) : <span class="type"><span class="type mysql_xdevapi\Session">mysql_xdevapi\Session</span></span></div>
|
|
|
|
<p class="para rdfs-comment">
|
|
Connects to the MySQL server.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 parameters" id="refsect1-function.mysql-xdevapi-getsession-parameters">
|
|
<h3 class="title">参数</h3>
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">uri</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
The URI to the MySQL server, such as <em>mysqlx://user:password@host</em>.
|
|
</p>
|
|
<p class="para">
|
|
URI format:
|
|
</p>
|
|
<p class="para">
|
|
<code class="code">scheme://[user[:[password]]@]target[:port][?attribute1=value1&attribute2=value2...</code>
|
|
</p>
|
|
<ul class="itemizedlist">
|
|
<li class="listitem">
|
|
<p class="para"><em>scheme</em>: required, the connection protocol</p>
|
|
<p class="para">In mysql_xdevapi it is always 'mysqlx' (for X Protocol)</p>
|
|
</li>
|
|
<li class="listitem">
|
|
<p class="para"><em>user</em>: optional, the MySQL user account for authentication</p>
|
|
</li>
|
|
<li class="listitem">
|
|
<p class="para"><em>password</em>: optional, the MySQL user's password for authentication</p>
|
|
</li>
|
|
<li class="listitem">
|
|
<p class="para"><em>target</em>: required, the server instance the connection refers to:</p>
|
|
<p class="para">* TCP connection (host name, IPv4 address, or IPv6 address)</p>
|
|
<p class="para">* Unix socket path (local file path)</p>
|
|
<p class="para">* Windows named-pipe (local file path)</p>
|
|
</li>
|
|
<li class="listitem">
|
|
<p class="para"><em>port</em>: optional, network port of MySQL server.</p>
|
|
<p class="para">by default port for X Protocol is 33060</p>
|
|
</li>
|
|
<li class="listitem">
|
|
<p class="para">
|
|
<em>?attribute=value</em>: this element is optional and specifies a data dictionary
|
|
that contains different options, including:
|
|
</p>
|
|
<ul class="itemizedlist">
|
|
<li class="listitem">
|
|
<p class="para">
|
|
The <em>auth</em> (authentication mechanism) attribute as it relates to encrypted connections.
|
|
For additional information, see <a href="https://dev.mysql.com/doc/refman/8.0/en/encrypted_connection_options.html" class="link external">» Command
|
|
Options for Encrypted Connections</a>.
|
|
The following 'auth' values are supported: <em>plain</em>,
|
|
<em>mysql41</em>, <em>external</em>, and <em>sha256_mem</em>.
|
|
</p>
|
|
</li>
|
|
<li class="listitem">
|
|
<p class="para">
|
|
The <em>connect-timeout</em> attribute affects the connection
|
|
and not subsequent operations. It is set per connection whether on
|
|
a single or multiple hosts.
|
|
</p>
|
|
<p class="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.
|
|
</p>
|
|
<p class="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.
|
|
</p>
|
|
</li>
|
|
</ul>
|
|
</li>
|
|
</ul>
|
|
|
|
<div class="example" id="example-1926">
|
|
<p><strong>Example #1 URI examples</strong></p>
|
|
<div class="example-contents"><div class="phpcode"><pre><span style="color: #000000">
|
|
mysqlx://foobar<br />mysqlx://root@localhost?socket=%2Ftmp%2Fmysqld.sock%2F<br />mysqlx://foo:bar@localhost:33060<br />mysqlx://foo:bar@localhost:33160?ssl-mode=disabled<br />mysqlx://foo:bar@localhost:33260?ssl-mode=required<br />mysqlx://foo:bar@localhost:33360?ssl-mode=required&auth=mysql41<br />mysqlx://foo:bar@(/path/to/socket)<br />mysqlx://foo:bar@(/path/to/socket)?auth=sha256_mem<br />mysqlx://foo:bar@[localhost:33060, 127.0.0.1:33061]<br />mysqlx://foobar?ssl-ca=(/path/to/ca.pem)&ssl-crl=(/path/to/crl.pem)<br />mysqlx://foo:bar@[localhost:33060, 127.0.0.1:33061]?ssl-mode=disabled<br />mysqlx://foo:bar@localhost:33160/?connect-timeout=0<br />mysqlx://foo:bar@localhost:33160/?connect-timeout=10</span>
|
|
</pre></div></div>
|
|
|
|
</div>
|
|
|
|
<p class="para">
|
|
For related information, see MySQL Shell's
|
|
<a href="https://dev.mysql.com/doc/refman/8.0/en/mysql_shell_connection_using_uri.html" class="link external">» Connecting using a URI String</a>.
|
|
</p>
|
|
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 returnvalues" id="refsect1-function.mysql-xdevapi-getsession-returnvalues">
|
|
<h3 class="title">返回值</h3>
|
|
<p class="para">
|
|
A <strong class="classname">Session</strong> object.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 errors" id="refsect1-function.mysql-xdevapi-getsession-errors">
|
|
<h3 class="title">错误/异常</h3>
|
|
<p class="para">
|
|
A connection failure throws an <a href="class.exception.html" class="classname">Exception</a>.
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 examples" id="refsect1-function.mysql-xdevapi-getsession-examples">
|
|
<h3 class="title">范例</h3>
|
|
<div class="example" id="example-1927">
|
|
<p><strong>Example #2 <span class="function"><strong>mysql_xdevapi\getSession()</strong></span> example</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br /></span><span style="color: #007700">try {<br /> </span><span style="color: #0000BB">$session </span><span style="color: #007700">= </span><span style="color: #0000BB">mysql_xdevapi</span><span style="color: #007700">\</span><span style="color: #0000BB">getSession</span><span style="color: #007700">(</span><span style="color: #DD0000">"mysqlx://user:password@host"</span><span style="color: #007700">);<br />} catch(</span><span style="color: #0000BB">Exception $e</span><span style="color: #007700">) {<br /> die(</span><span style="color: #DD0000">"Connection could not be established: " </span><span style="color: #007700">. </span><span style="color: #0000BB">$e</span><span style="color: #007700">-></span><span style="color: #0000BB">getMessage</span><span style="color: #007700">());<br />}<br /><br /></span><span style="color: #0000BB">$schemas </span><span style="color: #007700">= </span><span style="color: #0000BB">$session</span><span style="color: #007700">-></span><span style="color: #0000BB">getSchemas</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$schemas</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$mysql_version </span><span style="color: #007700">= </span><span style="color: #0000BB">$session</span><span style="color: #007700">-></span><span style="color: #0000BB">getServerVersion</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$mysql_version</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$collection</span><span style="color: #007700">-></span><span style="color: #0000BB">find</span><span style="color: #007700">(</span><span style="color: #DD0000">"name = 'Alfred'"</span><span style="color: #007700">)-></span><span style="color: #0000BB">execute</span><span style="color: #007700">()-></span><span style="color: #0000BB">fetchOne</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程的输出类似于:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
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"
|
|
}
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
|
|
|
|
</div></div></div></body></html> |