7.8.4 Using Client Programs in a Multiple-Server Environment (original) (raw)
7.8.4 Using Client Programs in a Multiple-Server Environment
To connect with a client program to a MySQL server that is listening to different network interfaces from those compiled into your client, you can use one of the following methods:
- Start the client with--host=host_name --port=port_number to connect using TCP/IP to a remote server, with--host=127.0.0.1 --port=port_number to connect using TCP/IP to a local server, or with--host=localhost --socket=file_name to connect to a local server using a Unix socket file or a Windows named pipe.
- Start the client with--protocol=TCP to connect using TCP/IP,--protocol=SOCKET to connect using a Unix socket file,--protocol=PIPE to connect using a named pipe, or--protocol=MEMORY to connect using shared memory. For TCP/IP connections, you may also need to specify --host and--port options. For the other types of connections, you may need to specify a--socket option to specify a Unix socket file or Windows named-pipe name, or a--shared-memory-base-name option to specify the shared-memory name. Shared-memory connections are supported only on Windows.
- On Unix, set the
MYSQL_UNIX_PORT
andMYSQL_TCP_PORT
environment variables to point to the Unix socket file and TCP/IP port number before you start your clients. If you normally use a specific socket file or port number, you can place commands to set these environment variables in your.login
file so that they apply each time you log in. SeeSection 6.9, “Environment Variables”. - Specify the default Unix socket file and TCP/IP port number in the
[client]
group of an option file. For example, you can useC:\my.cnf
on Windows, or the.my.cnf
file in your home directory on Unix. See Section 6.2.2.2, “Using Option Files”. - In a C program, you can specify the socket file or port number arguments in themysql_real_connect() call. You can also have the program read option files by callingmysql_options(). SeeC API Basic Function Descriptions.
- If you are using the Perl
DBD::mysql
module, you can read options from MySQL option files. For example:
$dsn = "DBI:mysql:test;mysql_read_default_group=client;"
. "mysql_read_default_file=/usr/local/mysql/data/my.cnf"; <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>d</mi><mi>b</mi><mi>h</mi><mo>=</mo><mi>D</mi><mi>B</mi><mi>I</mi><mo>−</mo><mo>></mo><mi>c</mi><mi>o</mi><mi>n</mi><mi>n</mi><mi>e</mi><mi>c</mi><mi>t</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">dbh = DBI->connect(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">d</span><span class="mord mathnormal">bh</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.02778em;">D</span><span class="mord mathnormal" style="margin-right:0.05017em;">B</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord">−</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">></span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal">co</span><span class="mord mathnormal">nn</span><span class="mord mathnormal">ec</span><span class="mord mathnormal">t</span><span class="mopen">(</span></span></span></span>dsn, <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>u</mi><mi>s</mi><mi>e</mi><mi>r</mi><mo separator="true">,</mo></mrow><annotation encoding="application/x-tex">user, </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.625em;vertical-align:-0.1944em;"></span><span class="mord mathnormal">u</span><span class="mord mathnormal" style="margin-right:0.02778em;">ser</span><span class="mpunct">,</span></span></span></span>password);
See Section 31.9, “MySQL Perl API”.
Other programming interfaces may provide similar capabilities for reading option files.