<div id="docs-body">
 <div class="section">
  <div class="titlepage">
   <div>
    <div>
     <h3 class="title">
      <a name="multiple-unix-servers">
      </a>
      7.8.3 Running Multiple MySQL Instances on Unix
     </h3>
    </div>
   </div>
  </div>
  <a class="indexterm" name="idm46045253960384">
  </a>
  <div class="note" style="margin-left: 0.5in; margin-right: 0.5in;">
   <div class="admon-title">
    Note
   </div>
   <p>
    The discussion here uses
    <a class="link" href="mysqld-safe.html" title="6.3.2 mysqld_safe — MySQL Server Startup Script">
     <span class="command">
      <strong>
       mysqld_safe
      </strong>
     </span>
    </a>
    to
        launch multiple instances of MySQL. For MySQL installation using
        an RPM distribution, server startup and shutdown is managed by
        systemd on several Linux platforms. On these platforms,
    <a class="link" href="mysqld-safe.html" title="6.3.2 mysqld_safe — MySQL Server Startup Script">
     <span class="command">
      <strong>
       mysqld_safe
      </strong>
     </span>
    </a>
    is not installed because it is
        unnecessary. For information about using systemd to handle
        multiple MySQL instances, see
    <a class="xref" href="using-systemd.html" title="2.5.9 Managing MySQL Server with systemd">
     Section 2.5.9, “Managing MySQL Server with systemd”
    </a>
    .
   </p>
  </div>
  <p>
   One way is to run multiple MySQL instances on Unix is to compile
      different servers with different default TCP/IP ports and Unix
      socket files so that each one listens on different network
      interfaces. Compiling in different base directories for each
      installation also results automatically in a separate, compiled-in
      data directory, log file, and PID file location for each server.
  </p>
  <p>
   Assume that an existing 8.3 server is configured for
      the default TCP/IP port number (3306) and Unix socket file
      (
   <code class="filename">
    /tmp/mysql.sock
   </code>
   ). To configure a new
      8.4.3 server to have different operating parameters,
      use a
   <span class="command">
    <strong>
     CMake
    </strong>
   </span>
   command something like this:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa92641615"><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"><span class="token prompt">$&gt; </span><span class="token command">cmake</span> <span class="token punctuation">.</span> <span class="token constant">-DMYSQL_TCP_PORT</span><span class="token attr-value"><span class="token punctuation">=</span><em class="replaceable">port_number</em></span> \
             <span class="token constant">-DMYSQL_UNIX_ADDR</span><span class="token attr-value"><span class="token punctuation">=</span><em class="replaceable">file_name</em></span> \
             <span class="token constant">-DCMAKE_INSTALL_PREFIX</span><span class="token attr-value"><span class="token punctuation">=</span>/usr/local/mysql-8.4.3</span></code></pre>
  </div>
  <p>
   Here,
   <em class="replaceable">
    <code>
     port_number
    </code>
   </em>
   and
   <em class="replaceable">
    <code>
     file_name
    </code>
   </em>
   must be different from the
      default TCP/IP port number and Unix socket file path name, and the
   <a class="link" href="source-configuration-options.html#option_cmake_cmake_install_prefix">
    <code class="option">
     CMAKE_INSTALL_PREFIX
    </code>
   </a>
   value should
      specify an installation directory different from the one under
      which the existing MySQL installation is located.
  </p>
  <p>
   If you have a MySQL server listening on a given port number, you
      can use the following command to find out what operating
      parameters it is using for several important configurable
      variables, including the base directory and Unix socket file name:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa45463704"><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"><span class="token prompt">$&gt; </span><span class="token command">mysqladmin</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">--port</span><span class="token attr-value"><span class="token punctuation">=</span><em class="replaceable">port_number</em></span> variables</code></pre>
  </div>
  <p>
   With the information displayed by that command, you can tell what
      option values
   <span class="emphasis">
    <em>
     not
    </em>
   </span>
   to use when configuring an
      additional server.
  </p>
  <p>
   If you specify
   <code class="literal">
    localhost
   </code>
   as the host name,
   <a class="link" href="mysqladmin.html" title="6.5.2 mysqladmin — A MySQL Server Administration Program">
    <span class="command">
     <strong>
      mysqladmin
     </strong>
    </span>
   </a>
   defaults to using a Unix socket file
      rather than TCP/IP. To explicitly specify the transport protocol,
      use the
   <a class="link" href="connection-options.html#option_general_protocol">
    <code class="option">
     --protocol={TCP|SOCKET|PIPE|MEMORY}
    </code>
   </a>
   option.
  </p>
  <p>
   You need not compile a new MySQL server just to start with a
      different Unix socket file and TCP/IP port number. It is also
      possible to use the same server binary and start each invocation
      of it with different parameter values at runtime. One way to do so
      is by using command-line options:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting one-line language-terminal"><div class="docs-select-all right" id="sa46374488"><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"><span class="token prompt">$&gt; </span><span class="token command">mysqld_safe</span> <span class="token constant">--socket</span><span class="token attr-value"><span class="token punctuation">=</span><em class="replaceable">file_name</em></span> <span class="token constant">--port</span><span class="token attr-value"><span class="token punctuation">=</span><em class="replaceable">port_number</em></span></code></pre>
  </div>
  <p>
   To start a second server, provide different
   <a class="link" href="server-options.html#option_mysqld_socket">
    <code class="option">
     --socket
    </code>
   </a>
   and
   <a class="link" href="server-options.html#option_mysqld_port">
    <code class="option">
     --port
    </code>
   </a>
   option values, and pass a
   <a class="link" href="server-system-variables.html#sysvar_datadir">
    <code class="option">
     --datadir=
     <em class="replaceable">
      <code>
       dir_name
      </code>
     </em>
    </code>
   </a>
   option to
   <a class="link" href="mysqld-safe.html" title="6.3.2 mysqld_safe — MySQL Server Startup Script">
    <span class="command">
     <strong>
      mysqld_safe
     </strong>
    </span>
   </a>
   so that the server uses a
      different data directory.
  </p>
  <p>
   Alternatively, put the options for each server in a different
      option file, then start each server using a
   <a class="link" href="option-file-options.html#option_general_defaults-file">
    <code class="option">
     --defaults-file
    </code>
   </a>
   option that
      specifies the path to the appropriate option file. For example, if
      the option files for two server instances are named
   <code class="filename">
    /usr/local/mysql/my.cnf
   </code>
   and
   <code class="filename">
    /usr/local/mysql/my.cnf2
   </code>
   , start the servers
      like this: command:
  </p>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa16395977"><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"><span class="token prompt">$&gt; </span><span class="token command">mysqld_safe</span> <span class="token constant">--defaults-file</span><span class="token attr-value"><span class="token punctuation">=</span>/usr/local/mysql/my.cnf</span>
<span class="token prompt">$&gt; </span><span class="token command">mysqld_safe</span> <span class="token constant">--defaults-file</span><span class="token attr-value"><span class="token punctuation">=</span>/usr/local/mysql/my.cnf2</span></code></pre>
  </div>
  <p>
   Another way to achieve a similar effect is to use environment
      variables to set the Unix socket file name and TCP/IP port number:
  </p>
  <a class="indexterm" name="idm46045253923856">
  </a>
  <a class="indexterm" name="idm46045253922768">
  </a>
  <a class="indexterm" name="idm46045253921680">
  </a>
  <a class="indexterm" name="idm46045253920192">
  </a>
  <div class="copytoclipboard-wrapper">
   <pre class="programlisting language-terminal"><div class="docs-select-all right" id="sa73795151"><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"><span class="token prompt">$&gt; </span><span class="token command">MYSQL_UNIX_PORT</span><span class="token attr-value"><span class="token punctuation">=</span>/tmp/mysqld-new.sock</span>
<span class="token prompt">$&gt; </span><span class="token command">MYSQL_TCP_PORT</span><span class="token attr-value"><span class="token punctuation">=</span>3307</span>
<span class="token prompt">$&gt; </span><span class="token command">export</span> MYSQL_UNIX_PORT MYSQL_TCP_PORT
<span class="token prompt">$&gt; </span><span class="token command">bin/mysqld</span> <span class="token property">--initialize</span> <span class="token constant">--user</span><span class="token attr-value"><span class="token punctuation">=</span>mysql</span>
<span class="token prompt">$&gt; </span><span class="token command">mysqld_safe</span> <span class="token constant">--datadir</span><span class="token attr-value"><span class="token punctuation">=</span>/path/to/datadir</span> &amp;</code></pre>
  </div>
  <p>
   This is a quick way of starting a second server to use for
      testing. The nice thing about this method is that the environment
      variable settings apply to any client programs that you invoke
      from the same shell. Thus, connections for those clients are
      automatically directed to the second server.
  </p>
  <p>
   <a class="xref" href="environment-variables.html" title="6.9 Environment Variables">
    Section 6.9, “Environment Variables”
   </a>
   , includes a list of other
      environment variables you can use to affect MySQL programs.
  </p>
  <p>
   On Unix, the
   <a class="link" href="mysqld-multi.html" title="6.3.4 mysqld_multi — Manage Multiple MySQL Servers">
    <span class="command">
     <strong>
      mysqld_multi
     </strong>
    </span>
   </a>
   script provides
      another way to start multiple servers. See
   <a class="xref" href="mysqld-multi.html" title="6.3.4 mysqld_multi — Manage Multiple MySQL Servers">
    Section 6.3.4, “mysqld_multi — Manage Multiple MySQL Servers”
   </a>
   .
  </p>
 </div>
 <br/>
</div>
