这里的讨论使用 mysqld_safe 来启动多个 MySQL 实例。对于使用 RPM 分发版安装的 MySQL,服务器的启动和关闭由多个 Linux 平台上的 systemd 管理。在这些平台上,mysqld_safe 并未安装,因为它没有必要。有关使用 systemd 管理多个 MySQL 实例的信息,请参见 第 2.5.9 节,“使用 systemd 管理 MySQL 服务器”。The discussion here uses mysqld_safe 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, mysqld_safe is not installed because it is unnecessary. For information about using systemd to handle multiple MySQL instances, see Section 2.5.9, “Managing MySQL Server with systemd”.
在 Unix 上运行多个 MySQL 实例的一种方法是使用不同的默认 TCP/IP 端口和 Unix 套接字文件编译不同的服务器,这样每个服务器就可以监听不同的网络接口。为每个安装编译在不同的基础目录中,还会自动为每个服务器生成单独的编译数据目录、日志文件和 PID 文件位置。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.
假设现有的 8.3 服务器已配置为默认的 TCP/IP 端口号(3306)和 Unix 套接字文件(/tmp/mysql.sock)。要配置一个新的 8.4.9 服务器以使用不同的操作参数,可以使用如下类似的 CMake 命令:Assume that an existing 8.3 server is configured for
the default TCP/IP port number (3306) and Unix socket file
(/tmp/mysql.sock). To configure a new
8.4.9 server to have different operating parameters,
use a CMake command something like this:
$>cmake . -DMYSQL_TCP_PORT=port_number\-DMYSQL_UNIX_ADDR=file_name\-DCMAKE_INSTALL_PREFIX=/usr/local/mysql-8.4.9
在这里,port_number 和 file_name 必须与默认的 TCP/IP 端口号和 Unix 套接字文件路径不同,并且 CMAKE_INSTALL_PREFIX 的值应指定一个不同于现有 MySQL 安装目录的安装目录。Here, port_number and
file_name must be different from the
default TCP/IP port number and Unix socket file path name, and the
CMAKE_INSTALL_PREFIX value should
specify an installation directory different from the one under
which the existing MySQL installation is located.
如果您有一个在指定端口号上监听的 MySQL 服务器,您可以使用以下命令来查找它为几个重要的可配置变量使用的操作参数,包括基目录和 Unix 套接字文件名: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:
$> mysqladmin --host=host_name --port=port_number variables
通过该命令显示的信息,你可以知道在配置额外服务器时哪些选项值不应使用。With the information displayed by that command, you can tell what option values not to use when configuring an additional server.
如果你将localhost指定为主机名,mysqladmin默认使用 Unix 套接字文件而不是 TCP/IP。要显式指定传输协议,请使用--protocol={TCP|SOCKET|PIPE|MEMORY}选项。If you specify localhost as the host name,
mysqladmin defaults to using a Unix socket file
rather than TCP/IP. To explicitly specify the transport protocol,
use the
--protocol={TCP|SOCKET|PIPE|MEMORY}
option.
您不需要为了使用不同的 Unix 套接字文件和 TCP/IP 端口号而重新编译新的 MySQL 服务器。同样可以使用相同的服务器二进制文件,并在每次运行时使用不同的参数值启动它。实现这一点的一种方法是使用命令行选项: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:
$> mysqld_safe --socket=file_name --port=port_number
要启动第二个服务器,请提供不同的 --socket 和 --port 选项值,并传递 --datadir= 选项给 mysqld_safe,以便服务器使用不同的数据目录。To start a second server, provide different
dir_name--socket and
--port option values, and pass a
--datadir=
option to mysqld_safe so that the server uses a
different data directory.dir_name
或者,将每个服务器的选项放在不同的选项文件中,然后使用--defaults-file选项启动每个服务器,该选项指定相应选项文件的路径。例如,如果两个服务器实例的选项文件分别命名为/usr/local/mysql/my.cnf和/usr/local/mysql/my.cnf2,则可以这样启动服务器:命令:Alternatively, put the options for each server in a different
option file, then start each server using a
--defaults-file option that
specifies the path to the appropriate option file. For example, if
the option files for two server instances are named
/usr/local/mysql/my.cnf and
/usr/local/mysql/my.cnf2, start the servers
like this: command:
$>mysqld_safe --defaults-file=/usr/local/mysql/my.cnf$>mysqld_safe --defaults-file=/usr/local/mysql/my.cnf2
实现类似效果的另一种方法是使用环境变量来设置 Unix 套接字文件名和 TCP/IP 端口号:Another way to achieve a similar effect is to use environment variables to set the Unix socket file name and TCP/IP port number:
$>MYSQL_UNIX_PORT=/tmp/mysqld-new.sock$>MYSQL_TCP_PORT=3307$>export MYSQL_UNIX_PORT MYSQL_TCP_PORT$>bin/mysqld --initialize --user=mysql$>mysqld_safe --datadir=/path/to/datadir &
这是一种快速启动用于测试的第二个服务器的方法。这种方法的好处是,环境变量设置适用于从同一个 shell 调用的任何客户端程序。因此,这些客户端的连接会自动指向第二个服务器。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.
第 6.9 节,“环境变量”,包括了可以用来影响 MySQL 程序的其他环境变量列表。Section 6.9, “Environment Variables”, includes a list of other environment variables you can use to affect MySQL programs.
在 Unix 系统上,mysqld_multi 脚本提供了启动多个服务器的另一种方法。参考 第 6.3.4 节,“mysqld_multi — 管理多个 MySQL 服务器”。On Unix, the mysqld_multi script provides another way to start multiple servers. See Section 6.3.4, “mysqld_multi — Manage Multiple MySQL Servers”.