<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="connecting">
      </a>
      6.2.4 Connecting to the MySQL Server Using Command Options
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045322804224">
  </a>
  <a class="indexterm" name="idm46045322802768">
  </a>
  <a class="indexterm" name="idm46045322801696">
  </a>
  <a class="indexterm" name="idm46045322800208">
  </a>
  <a class="indexterm" name="idm46045322798720">
  </a>
  <a class="indexterm" name="idm46045322797648">
  </a>
  <a class="indexterm" name="idm46045322796576">
  </a>
  <p>
   This section describes use of command-line options to specify how
      to establish connections to the MySQL server, for clients such as
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   or
   <a class="link" href="mysqldump.html" title="6.5.4 mysqldump — A Database Backup Program">
    <span class="command">
     <strong>
      mysqldump
     </strong>
    </span>
   </a>
   . For
      information on establishing connections using URI-like connection
      strings or key-value pairs, for clients such as MySQL Shell, see
   <a class="xref" href="connecting-using-uri-or-key-value-pairs.html" title="6.2.5 Connecting to the Server Using URI-Like Strings or Key-Value Pairs">
    Section 6.2.5, “Connecting to the Server Using URI-Like Strings or Key-Value Pairs”
   </a>
   . For
      additional information if you are unable to connect, see
   <a class="xref" href="problems-connecting.html" title="8.2.22 Troubleshooting Problems Connecting to MySQL">
    Section 8.2.22, “Troubleshooting Problems Connecting to MySQL”
   </a>
   .
  </p>
  <p>
   For a client program to connect to the MySQL server, it must use
      the proper connection parameters, such as the name of the host
      where the server is running and the user name and password of your
      MySQL account. Each connection parameter has a default value, but
      you can override default values as necessary using program options
      specified either on the command line or in an option file.
  </p>
  <p>
   The examples here use the
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   client program,
      but the principles apply to other clients such as
   <a class="link" href="mysqldump.html" title="6.5.4 mysqldump — A Database Backup Program">
    <span class="command">
     <strong>
      mysqldump
     </strong>
    </span>
   </a>
   ,
   <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
    <span class="command">
     <strong>
      mysqladmin
     </strong>
    </span>
   </a>
   , or
   <a class="link" href="mysqlshow.html" title="6.5.6 mysqlshow — Display Database, Table, and Column Information">
    <span class="command">
     <strong>
      mysqlshow
     </strong>
    </span>
   </a>
   .
  </p>
  <p>
   This command invokes
   <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
    <span class="command">
     <strong>
      mysql
     </strong>
    </span>
   </a>
   without specifying
      any explicit connection parameters:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa11262802"><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-terminal">mysql</code></pre>
  </div>
  <p>
   Because there are no parameter options, the default values apply:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      The default host name is
      <code class="literal">
       localhost
      </code>
      . On
          Unix, this has a special meaning, as described later.
     </p>
    </li>
    <li class="listitem">
     <p>
      The default user name is
      <code class="literal">
       ODBC
      </code>
      on Windows or
          your Unix login name on Unix.
     </p>
    </li>
    <li class="listitem">
     <p>
      No password is sent because neither
      <a class="link" href="connection-options.html#option_general_password">
       <code class="option">
        --password
       </code>
      </a>
      nor
      <code class="option">
       -p
      </code>
      is given.
     </p>
    </li>
    <li class="listitem">
     <p>
      For
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      , the first nonoption argument is
          taken as the name of the default database. Because there is no
          such argument,
      <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
       <span class="command">
        <strong>
         mysql
        </strong>
       </span>
      </a>
      selects no default
          database.
     </p>
    </li>
   </ul>
  </div>
  <p>
   To specify the host name and user name explicitly, as well as a
      password, supply appropriate options on the command line. To
      select a default database, add a database-name argument. Examples:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa68706255"><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-terminal">mysql <span class="token constant">--host</span><span class="token attr-value"><span class="token punctuation">=</span>localhost</span> <span class="token constant">--user</span><span class="token attr-value"><span class="token punctuation">=</span>myname</span> <span class="token constant">--password</span><span class="token attr-value"><span class="token punctuation">=</span><em class="replaceable">password</em></span> mydb
mysql <span class="token property">-h</span> localhost <span class="token property">-u</span> myname <span class="token property">-p<em class="replaceable">password</em></span> mydb</code></pre>
  </div>
  <p>
   For password options, the password value is optional:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      If you use a
      <a class="link" href="connection-options.html#option_general_password">
       <code class="option">
        --password
       </code>
      </a>
      or
      <code class="option">
       -p
      </code>
      option and specify a password value, there
          must be
      <span class="emphasis">
       <em>
        no space
       </em>
      </span>
      between
      <a class="link" href="connection-options.html#option_general_password">
       <code class="option">
        --password=
       </code>
      </a>
      or
      <code class="option">
       -p
      </code>
      and the password following it.
     </p>
    </li>
    <li class="listitem">
     <p>
      If you use
      <a class="link" href="connection-options.html#option_general_password">
       <code class="option">
        --password
       </code>
      </a>
      or
      <code class="option">
       -p
      </code>
      but do not specify a password value, the
          client program prompts you to enter the password. The password
          is not displayed as you enter it. This is more secure than
          giving the password on the command line, which might enable
          other users on your system to see the password line by
          executing a command such as
      <span class="command">
       <strong>
        ps
       </strong>
      </span>
      . See
      <a class="xref" href="password-security-user.html" title="8.1.2.1 End-User Guidelines for Password Security">
       Section 8.1.2.1, “End-User Guidelines for Password Security”
      </a>
      .
     </p>
    </li>
    <li class="listitem">
     <p>
      To explicitly specify that there is no password and that the
          client program should not prompt for one, use the
      <a class="link" href="connection-options.html#option_general_password">
       <code class="option">
        --skip-password
       </code>
      </a>
      option.
     </p>
    </li>
   </ul>
  </div>
  <p>
   As just mentioned, including the password value on the command
      line is a security risk. To avoid this risk, specify the
   <a class="link" href="connection-options.html#option_general_password">
    <code class="option">
     --password
    </code>
   </a>
   or
   <code class="option">
    -p
   </code>
   option without any following password value:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa75330406"><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-terminal">mysql <span class="token constant">--host</span><span class="token attr-value"><span class="token punctuation">=</span>localhost</span> <span class="token constant">--user</span><span class="token attr-value"><span class="token punctuation">=</span>myname</span> <span class="token property">--password</span> mydb
mysql <span class="token property">-h</span> localhost <span class="token property">-u</span> myname <span class="token property">-p</span> mydb</code></pre>
  </div>
  <p>
   When the
   <a class="link" href="connection-options.html#option_general_password">
    <code class="option">
     --password
    </code>
   </a>
   or
   <code class="option">
    -p
   </code>
   option is given with no password value, the
      client program prints a prompt and waits for you to enter the
      password. (In these examples,
   <code class="literal">
    mydb
   </code>
   is
   <span class="emphasis">
    <em>
     not
    </em>
   </span>
   interpreted as a password because it is
      separated from the preceding password option by a space.)
  </p>
  <p>
   On some systems, the library routine that MySQL uses to prompt for
      a password automatically limits the password to eight characters.
      That limitation is a property of the system library, not MySQL.
      Internally, MySQL does not have any limit for the length of the
      password. To work around the limitation on systems affected by it,
      specify your password in an option file (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>
   ). Another workaround is to change
      your MySQL password to a value that has eight or fewer characters,
      but that has the disadvantage that shorter passwords tend to be
      less secure.
  </p>
  <p>
   Client programs determine what type of connection to make as
      follows:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      If the host is not specified or is
      <code class="literal">
       localhost
      </code>
      , a connection to the local host
          occurs:
     </p>
     <div class="itemizedlist">
      <ul class="itemizedlist" style="list-style-type: circle; ">
       <li class="listitem">
        <p>
         On Windows, the client connects using shared memory, if
              the server was started with the
         <a class="link" href="server-system-variables.html#sysvar_shared_memory">
          <code class="literal">
           shared_memory
          </code>
         </a>
         system
              variable enabled to support shared-memory connections.
        </p>
       </li>
       <li class="listitem">
        <p>
         On Unix, MySQL programs treat the host name
         <code class="literal">
          localhost
         </code>
         specially, in a way that is
              likely different from what you expect compared to other
              network-based programs: the client connects using a Unix
              socket file. The
         <a class="link" href="connection-options.html#option_general_socket">
          <code class="option">
           --socket
          </code>
         </a>
         option or the
         <code class="literal">
          MYSQL_UNIX_PORT
         </code>
         environment variable may be used to specify the socket
              name.
        </p>
        <a class="indexterm" name="idm46045322742832">
        </a>
       </li>
      </ul>
     </div>
    </li>
    <li class="listitem">
     <p>
      On Windows, if
      <code class="literal">
       host
      </code>
      is
      <code class="literal">
       .
      </code>
      (period), or TCP/IP is not enabled and
      <a class="link" href="connection-options.html#option_general_socket">
       <code class="option">
        --socket
       </code>
      </a>
      is not specified or
          the host is empty, the client connects using a named pipe, if
          the server was started with the
      <a class="link" href="server-system-variables.html#sysvar_named_pipe">
       <code class="literal">
        named_pipe
       </code>
      </a>
      system variable
          enabled to support named-pipe connections. If named-pipe
          connections are not supported or if the user making the
          connection is not a member of the Windows group specified by
          the
      <a class="link" href="server-system-variables.html#sysvar_named_pipe_full_access_group">
       <code class="literal">
        named_pipe_full_access_group
       </code>
      </a>
      system variable, an error occurs.
     </p>
    </li>
    <li class="listitem">
     <p>
      Otherwise, the connection uses TCP/IP.
     </p>
    </li>
   </ul>
  </div>
  <p>
   The
   <a class="link" href="connection-options.html#option_general_protocol">
    <code class="option">
     --protocol
    </code>
   </a>
   option enables you
      to use a particular transport protocol even when other options
      normally result in use of a different protocol. That is,
   <a class="link" href="connection-options.html#option_general_protocol">
    <code class="option">
     --protocol
    </code>
   </a>
   specifies the transport
      protocol explicitly and overrides the preceding rules, even for
   <code class="literal">
    localhost
   </code>
   .
  </p>
  <p>
   Only connection options that are relevant to the selected
      transport protocol are used or checked. Other connection options
      are ignored. For example, with
   <a class="link" href="connection-options.html#option_general_host">
    <code class="option">
     --host=localhost
    </code>
   </a>
   on Unix, the
      client attempts to connect to the local server using a Unix socket
      file, even if a
   <a class="link" href="connection-options.html#option_general_port">
    <code class="option">
     --port
    </code>
   </a>
   or
   <code class="option">
    -P
   </code>
   option is given to specify a TCP/IP port
      number.
  </p>
  <p>
   To ensure that the client makes a TCP/IP connection to the local
      server, use
   <a class="link" href="connection-options.html#option_general_host">
    <code class="option">
     --host
    </code>
   </a>
   or
   <code class="option">
    -h
   </code>
   to specify a host name value of
   <code class="literal">
    127.0.0.1
   </code>
   (instead of
   <code class="literal">
    localhost
   </code>
   ), or the IP address or name of the
      local server. You can also specify the transport protocol
      explicitly, even for
   <code class="literal">
    localhost
   </code>
   , by using the
   <a class="link" href="connection-options.html#option_general_protocol">
    <code class="option">
     --protocol=TCP
    </code>
   </a>
   option. Examples:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa274116"><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-terminal">mysql <span class="token constant">--host</span><span class="token attr-value"><span class="token punctuation">=</span>127.0.0.1</span>
mysql <span class="token constant">--protocol</span><span class="token attr-value"><span class="token punctuation">=</span>TCP</span></code></pre>
  </div>
  <p>
   If the server is configured to accept IPv6 connections, clients
      can connect to the local server over IPv6 using
   <a class="link" href="connection-options.html#option_general_host">
    <code class="option">
     --host=::1
    </code>
   </a>
   . See
   <a class="xref" href="ipv6-support.html" title="7.1.13 IPv6 Support">
    Section 7.1.13, “IPv6 Support”
   </a>
   .
  </p>
  <p>
   On Windows, to force a MySQL client to use a named-pipe
      connection, specify the
   <a class="link" href="connection-options.html#option_general_pipe">
    <code class="option">
     --pipe
    </code>
   </a>
   or
   <a class="link" href="connection-options.html#option_general_protocol">
    <code class="option">
     --protocol=PIPE
    </code>
   </a>
   option, or specify
   <code class="literal">
    .
   </code>
   (period) as the host name. If the server was
      not started with the
   <a class="link" href="server-system-variables.html#sysvar_named_pipe">
    <code class="literal">
     named_pipe
    </code>
   </a>
   system variable enabled to support named-pipe connections or if
      the user making the connection is not a member of the Windows
      group specified by the
   <a class="link" href="server-system-variables.html#sysvar_named_pipe_full_access_group">
    <code class="literal">
     named_pipe_full_access_group
    </code>
   </a>
   system variable, an error occurs. Use the
   <a class="link" href="connection-options.html#option_general_socket">
    <code class="option">
     --socket
    </code>
   </a>
   option to specify the
      name of the pipe if you do not want to use the default pipe name.
  </p>
  <p>
   Connections to remote servers use TCP/IP. This command connects to
      the server running on
   <code class="literal">
    remote.example.com
   </code>
   using
      the default port number (3306):
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa3982935"><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-terminal">mysql <span class="token constant">--host</span><span class="token attr-value"><span class="token punctuation">=</span>remote.example.com</span></code></pre>
  </div>
  <p>
   To specify a port number explicitly, use the
   <a class="link" href="connection-options.html#option_general_port">
    <code class="option">
     --port
    </code>
   </a>
   or
   <code class="option">
    -P
   </code>
   option:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa47601944"><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-terminal">mysql <span class="token constant">--host</span><span class="token attr-value"><span class="token punctuation">=</span>remote.example.com</span> <span class="token constant">--port</span><span class="token attr-value"><span class="token punctuation">=</span>13306</span></code></pre>
  </div>
  <p>
   You can specify a port number for connections to a local server,
      too. However, as indicated previously, connections to
   <code class="literal">
    localhost
   </code>
   on Unix use a socket file by default,
      so unless you force a TCP/IP connection as previously described,
      any option that specifies a port number is ignored.
  </p>
  <p>
   For this command, the program uses a socket file on Unix and the
   <a class="link" href="connection-options.html#option_general_port">
    <code class="option">
     --port
    </code>
   </a>
   option is ignored:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa39741887"><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-terminal">mysql <span class="token constant">--port</span><span class="token attr-value"><span class="token punctuation">=</span>13306</span> <span class="token constant">--host</span><span class="token attr-value"><span class="token punctuation">=</span>localhost</span></code></pre>
  </div>
  <p>
   To cause the port number to be used, force a TCP/IP connection.
      For example, invoke the program in either of these ways:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa84693728"><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-terminal">mysql <span class="token constant">--port</span><span class="token attr-value"><span class="token punctuation">=</span>13306</span> <span class="token constant">--host</span><span class="token attr-value"><span class="token punctuation">=</span>127.0.0.1</span>
mysql <span class="token constant">--port</span><span class="token attr-value"><span class="token punctuation">=</span>13306</span> <span class="token constant">--protocol</span><span class="token attr-value"><span class="token punctuation">=</span>TCP</span></code></pre>
  </div>
  <p>
   For additional information about options that control how client
      programs establish connections to the server, see
   <a class="xref" href="connection-options.html" title="6.2.3 Command Options for Connecting to the Server">
    Section 6.2.3, “Command Options for Connecting to the Server”
   </a>
   .
  </p>
  <p>
   It is possible to specify connection parameters without entering
      them on the command line each time you invoke a client program:
  </p>
  <div class="itemizedlist">
   <ul class="itemizedlist" style="list-style-type: disc; ">
    <li class="listitem">
     <p>
      Specify the connection parameters in the
      <code class="literal">
       [client]
      </code>
      section of an option file. The
          relevant section of the file might look like this:
     </p>
     <a class="indexterm" name="idm46045322698032">
     </a>
     <a class="indexterm" name="idm46045322696960">
     </a>
     <div class="copytoclipboard-wrapper">
      <pre class="programlisting language-ini"><div class="docs-select-all right" id="sa49959204"><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-ini"><span class="token selector">[client]</span>
<span class="token constant">host</span><span class="token attr-value"><span class="token punctuation">=</span><em class="replaceable">host_name</em></span>
<span class="token constant">user</span><span class="token attr-value"><span class="token punctuation">=</span><em class="replaceable">user_name</em></span>
<span class="token constant">password</span><span class="token attr-value"><span class="token punctuation">=</span><em class="replaceable">password</em></span></code></pre>
     </div>
     <p>
      For more information, 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>
    </li>
    <li class="listitem">
     <p>
      Some connection parameters can be specified using environment
          variables. Examples:
     </p>
     <a class="indexterm" name="idm46045322691232">
     </a>
     <a class="indexterm" name="idm46045322690144">
     </a>
     <a class="indexterm" name="idm46045322688656">
     </a>
     <a class="indexterm" name="idm46045322687568">
     </a>
     <div class="itemizedlist">
      <ul class="itemizedlist" style="list-style-type: circle; ">
       <li class="listitem">
        <p>
         To specify the host for
         <a class="link" href="mysql.html" title="6.5.1 mysql — The MySQL Command-Line Client">
          <span class="command">
           <strong>
            mysql
           </strong>
          </span>
         </a>
         , use
         <code class="literal">
          MYSQL_HOST
         </code>
         .
        </p>
       </li>
       <li class="listitem">
        <p>
         On Windows, to specify the MySQL user name, use
         <code class="literal">
          USER
         </code>
         .
        </p>
       </li>
      </ul>
     </div>
     <p>
      For a list of supported environment variables, see
      <a class="xref" href="environment-variables.html" title="6.9 Environment Variables">
       Section 6.9, “Environment Variables”
      </a>
      .
     </p>
    </li>
   </ul>
  </div>
 </div>
 <br/>
</div>
