<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="multiple-server-clients">
      </a>
      7.8.4 Using Client Programs in a Multiple-Server Environment
     </h3>
    </div>
   </div>
  </div>
  <p>
   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:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Start the client with
      <a class="link" href="connection-options.html#option_general_host">
       <code class="option">
        --host=
        <em class="replaceable">
         <code>
          host_name
         </code>
        </em>
       </code>
      </a>
      <a class="link" href="connection-options.html#option_general_port">
       <code class="option">
        --port=
        <em class="replaceable">
         <code>
          port_number
         </code>
        </em>
       </code>
      </a>
      to connect using TCP/IP to a remote server, with
      <a class="link" href="connection-options.html#option_general_host">
       <code class="option">
        --host=127.0.0.1
       </code>
      </a>
      <a class="link" href="connection-options.html#option_general_port">
       <code class="option">
        --port=
        <em class="replaceable">
         <code>
          port_number
         </code>
        </em>
       </code>
      </a>
      to connect using TCP/IP to a local server, or with
      <a class="link" href="connection-options.html#option_general_host">
       <code class="option">
        --host=localhost
       </code>
      </a>
      <a class="link" href="connection-options.html#option_general_socket">
       <code class="option">
        --socket=
        <em class="replaceable">
         <code>
          file_name
         </code>
        </em>
       </code>
      </a>
      to connect to a local server using a Unix socket file or a
          Windows named pipe.
     </p>
    </li>
    <li class="listitem">
     <p>
      Start the client with
      <a class="link" href="connection-options.html#option_general_protocol">
       <code class="option">
        --protocol=TCP
       </code>
      </a>
      to connect
          using TCP/IP,
      <a class="link" href="connection-options.html#option_general_protocol">
       <code class="option">
        --protocol=SOCKET
       </code>
      </a>
      to connect
          using a Unix socket file,
      <a class="link" href="connection-options.html#option_general_protocol">
       <code class="option">
        --protocol=PIPE
       </code>
      </a>
      to connect
          using a named pipe, or
      <a class="link" href="connection-options.html#option_general_protocol">
       <code class="option">
        --protocol=MEMORY
       </code>
      </a>
      to connect
          using shared memory. For TCP/IP connections, you may also need
          to specify
      <a class="link" href="connection-options.html#option_general_host">
       <code class="option">
        --host
       </code>
      </a>
      and
      <a class="link" href="connection-options.html#option_general_port">
       <code class="option">
        --port
       </code>
      </a>
      options. For the other
          types of connections, you may need to specify a
      <a class="link" href="connection-options.html#option_general_socket">
       <code class="option">
        --socket
       </code>
      </a>
      option to specify a
          Unix socket file or Windows named-pipe name, or a
      <a class="link" href="connection-options.html#option_general_shared-memory-base-name">
       <code class="option">
        --shared-memory-base-name
       </code>
      </a>
      option to specify the shared-memory name. Shared-memory
          connections are supported only on Windows.
     </p>
    </li>
    <li class="listitem">
     <p>
      <a class="indexterm" name="idm46045253890288">
      </a>
      <a class="indexterm" name="idm46045253889200">
      </a>
      <a class="indexterm" name="idm46045253888112">
      </a>
      <a class="indexterm" name="idm46045253886624">
      </a>
      On Unix, set the
      <code class="literal">
       MYSQL_UNIX_PORT
      </code>
      and
      <code class="literal">
       MYSQL_TCP_PORT
      </code>
      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
      <code class="filename">
       .login
      </code>
      file
          so that they apply each time you log in. See
      <a class="xref" href="environment-variables.html" title="6.9 Environment Variables">
       Section 6.9, “Environment Variables”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      Specify the default Unix socket file and TCP/IP port number in
          the
      <code class="literal">
       [client]
      </code>
      group of an option file. For
          example, you can use
      <code class="filename">
       C:\my.cnf
      </code>
      on
          Windows, or the
      <code class="filename">
       .my.cnf
      </code>
      file in your home
          directory on Unix. See
      <a class="xref" href="option-files.html" title="6.2.2.2 Using Option Files">
       Section 6.2.2.2, “Using Option Files”
      </a>
      .
     </p>
     <a class="indexterm" name="idm46045253878016">
     </a>
     <a class="indexterm" name="idm46045253876944">
     </a>
     <a class="indexterm" name="idm46045253875456">
     </a>
     <a class="indexterm" name="idm46045253874384">
     </a>
    </li>
    <li class="listitem">
     <p>
      In a C program, you can specify the socket file or port number
          arguments in the
      <a class="ulink" href="/doc/c-api/8.4/en/mysql-real-connect.html" target="_top">
       <code class="literal">
        mysql_real_connect()
       </code>
      </a>
      call. You
          can also have the program read option files by calling
      <a class="ulink" href="/doc/c-api/8.4/en/mysql-options.html" target="_top">
       <code class="literal">
        mysql_options()
       </code>
      </a>
      . See
      <a class="ulink" href="/doc/c-api/8.4/en/c-api-function-descriptions.html" target="_top">
       C API Basic Function Descriptions
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      If you are using the Perl
      <code class="literal">
       DBD::mysql
      </code>
      module, you can read options from MySQL option files. For
          example:
     </p>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-perl"><div class="docs-select-all right" id="sa76111401"><div class="copy-help left">Press ⌘+C to copy</div> <div class="right"><button class="clipboard-btn" title="Copy to Clipboard"><span class="icon-clipboard"></span></button></div></div><code class="language-perl"><span class="token variable">$dsn</span> <span class="token operator">=</span> <span class="token string">"DBI:mysql:test;mysql_read_default_group=client;"</span>
        <span class="token operator">.</span> <span class="token string">"mysql_read_default_file=/usr/local/mysql/data/my.cnf"</span><span class="token punctuation">;</span>
<span class="token variable">$dbh</span> <span class="token operator">=</span> DBI<span class="token operator">-&gt;</span>connect<span class="token punctuation">(</span><span class="token variable">$dsn</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token variable">$password</span><span class="token punctuation">)</span><span class="token punctuation">;</span></code></pre>
     </div>
     <p>
      See
      <a class="xref" href="apis-perl.html" title="31.9 MySQL Perl API">
       Section 31.9, “MySQL Perl API”
      </a>
      .
     </p>
     <p>
      Other programming interfaces may provide similar capabilities
          for reading option files.
     </p>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
