网络命名空间是主机系统网络栈的逻辑副本。网络命名空间对于设置容器或虚拟环境非常有用。每个命名空间都有自己的 IP 地址、网络接口、路由表等。默认或全局命名空间是主机系统物理接口所在的命名空间。A network namespace is a logical copy of the network stack from the host system. Network namespaces are useful for setting up containers or virtual environments. Each namespace has its own IP addresses, network interfaces, routing tables, and so forth. The default or global namespace is the one in which the host system physical interfaces exist.
特定于命名空间的地址空间可能会在 MySQL 连接跨命名空间时导致问题。例如,运行在容器或虚拟网络中的 MySQL 实例的网络地址空间可能与主机的地址空间不同。这可能会产生这样的现象:即使客户端和服务器运行在同一台机器上,来自某个命名空间地址的客户端连接在 MySQL 服务器看来也像是来自不同的地址。假设这两个进程都运行在 IP 地址为 203.0.113.10 的主机上,但使用不同的命名空间。一次连接可能产生如下结果:Namespace-specific address spaces can lead to problems when MySQL
connections cross namespaces. For example, the network address
space for a MySQL instance running in a container or virtual
network may differ from the address space of the host machine.
This can produce phenomena such as a client connection from an
address in one namespace appearing to the MySQL server to be
coming from a different address, even for client and server
running on the same machine. Suppose that both processes run on a
host with IP address 203.0.113.10 but use
different namespaces. A connection may produce a result like this:
$>mysql --user=admin --host=203.0.113.10 --protocol=tcpmysql>SELECT USER();+--------------------+ | USER() | +--------------------+ | admin@198.51.100.2 | +--------------------+
在这种情况下,预期的 USER() 值是 admin@203.0.113.10。如果连接来源的地址并非如其表面所示,这种行为可能会使正确分配账户权限变得困难。In this case, the expected USER()
value is admin@203.0.113.10. Such behavior can
make it difficult to assign account permissions properly if the
address from which an connection originates is not what it
appears.
为了解决此问题,MySQL 允许指定用于 TCP/IP 连接的网络命名空间,以便连接的两个端点使用约定的公共地址空间。To address this issue, MySQL enables specifying the network namespace to use for TCP/IP connections, so that both endpoints of connections use an agreed-upon common address space.
MySQL 在实现网络命名空间的平台上支持网络命名空间。MySQL 内的支持适用于:MySQL supports network namespaces on platforms that implement them. Support within MySQL applies to:
X 插件。X Plugin.
mysql 客户端和 mysqlxtest 测试套件客户端。(其他客户端不受支持。它们必须从要连接的服务器的网络命名空间内调用。)The mysql client and the mysqlxtest test suite client. (Other clients are not supported. They must be invoked from within the network namespace of the server to which they are to connect.)
常规复制。Regular replication.
组复制,仅在使用 MySQL 通信栈建立组通信连接时使用。Group Replication, only when using the MySQL communication stack to establish group communication connections.
以下各节描述了如何在 MySQL 中使用网络命名空间:The following sections describe how to use network namespaces in MySQL:
在 MySQL 中使用网络命名空间支持之前,必须满足以下主机系统前提条件:Prior to using network namespace support in MySQL, these host system prerequisites must be satisfied:
主机操作系统必须支持网络命名空间。(例如,Linux。)The host operating system must support network namespaces. (For example, Linux.)
MySQL 要使用的任何网络命名空间必须首先在主机系统上创建。Any network namespace to be used by MySQL must first be created on the host system.
主机名解析必须由系统管理员配置,以支持网络命名空间。Host name resolution must be configured by the system administrator to support network namespaces.
Note一个已知的限制是,在 MySQL 中,对于在特定网络命名空间的主机文件中指定的名称,主机名解析不起作用。例如,如果
red命名空间中的主机名地址在/etc/netns/red/hosts文件中指定,则在服务器端和客户端绑定该名称都会失败。解决方法是使用 IP 地址而不是主机名。A known limitation is that, within MySQL, host name resolution does not work for names specified in network namespace-specific host files. For example, if the address for a host name in therednamespace is specified in the/etc/netns/red/hostsfile, binding to the name fails on both the server and client sides. The workaround is to use the IP address rather than the host name.系统管理员必须为支持网络命名空间的 MySQL 二进制文件启用
CAP_SYS_ADMIN操作系统权限(mysqld,mysql,mysqlxtest)。The system administrator must enable theCAP_SYS_ADMINoperating system privilege for the MySQL binaries that support network namespaces (mysqld, mysql, mysqlxtest).Important启用
CAP_SYS_ADMIN是一个安全敏感操作,因为它不仅允许进程设置命名空间,还允许执行其他特权操作。有关其影响的描述,请参见 https://man7.org/linux/man-pages/man7/capabilities.7.html。EnablingCAP_SYS_ADMINis a security sensitive operation because it enables a process to perform other privileged actions in addition to setting namespaces. For a description of its effects, see https://man7.org/linux/man-pages/man7/capabilities.7.html.由于
CAP_SYS_ADMIN必须由系统管理员显式启用,MySQL 二进制文件默认未启用网络命名空间支持。系统管理员应在启用CAP_SYS_ADMIN之前评估以该权限运行 MySQL 进程的安全影响。BecauseCAP_SYS_ADMINmust be enabled explicitly by the system administrator, MySQL binaries by default do not have network namespace support enabled. The system administrator should evaluate the security implications of running MySQL processes withCAP_SYS_ADMINbefore enabling it.
以下示例中的指令设置了名为 red 和 blue 的网络命名空间。您选择的名称可能不同,主机系统上的网络地址和接口也可能不同。The instructions in the following example set up network
namespaces named red and
blue. The names you choose may differ, as may
the network addresses and interfaces on your host system.
以 root 操作系统用户身份执行此处显示的命令,或者在每个命令前加上 sudo。例如,如果您不是 root,要执行 ip 或 setcap 命令,请使用 sudo ip 或 sudo setcap。Invoke the commands shown here either as the
root operating system user or by prefixing
each command with sudo. For example, to
invoke the ip or setcap
command if you are not root, use
sudo ip or sudo setcap.
要配置网络命名空间,请使用 ip 命令。对于某些操作,ip 命令必须在特定的命名空间内执行(该命名空间必须已存在)。在这种情况下,命令的开头如下:To configure network namespaces, use the ip command. For some operations, the ip command must execute within a particular namespace (which must already exist). In such cases, begin the command like this:
ip netns exec namespace_name
例如,下列命令在 red 命名空间内执行以启动环回接口:For example, this command executes within the
red namespace to bring up the loopback
interface:
ip netns exec red ip link set lo up
要添加名为 red 和 blue 的命名空间,每个命名空间都有自己的虚拟以太网设备,用作命名空间之间的连接,以及自己的环回接口:To add namespaces named red and
blue, each with its own virtual Ethernet
device used as a link between namespaces and its own loopback
interface:
ip netns add red ip link add veth-red type veth peer name vpeer-red ip link set vpeer-red netns red ip addr add 192.0.2.1/24 dev veth-red ip link set veth-red up ip netns exec red ip addr add 192.0.2.2/24 dev vpeer-red ip netns exec red ip link set vpeer-red up ip netns exec red ip link set lo up ip netns add blue ip link add veth-blue type veth peer name vpeer-blue ip link set vpeer-blue netns blue ip addr add 198.51.100.1/24 dev veth-blue ip link set veth-blue up ip netns exec blue ip addr add 198.51.100.2/24 dev vpeer-blue ip netns exec blue ip link set vpeer-blue up ip netns exec blue ip link set lo up # if you want to enable inter-subnet routing... sysctl net.ipv4.ip_forward=1 ip netns exec red ip route add default via 192.0.2.1 ip netns exec blue ip route add default via 198.51.100.1
命名空间之间的链接示意图如下:A diagram of the links between namespaces looks like this:
red global blue
192.0.2.2 <=> 192.0.2.1
(vpeer-red) (veth-red)
198.51.100.1 <=> 198.51.100.2
(veth-blue) (vpeer-blue)
要检查哪些命名空间和链接存在:To check which namespaces and links exist:
ip netns list ip link list
要查看全局和命名命名空间的路由表:To see the routing tables for the global and named namespaces:
ip route show ip netns exec red ip route show ip netns exec blue ip route show
要移除 red 和 blue 链接和命名空间:To remove the red and blue
links and namespaces:
ip link del veth-red ip link del veth-blue ip netns del red ip netns del blue sysctl net.ipv4.ip_forward=0
为了让包含网络命名空间支持的 MySQL 二进制文件能够实际使用命名空间,必须授予它们 CAP_SYS_ADMIN 权限。以下 setcap 命令假设您已切换到包含 MySQL 二进制文件的目录(根据系统情况调整路径名):So that the MySQL binaries that include network namespace
support can actually use namespaces, you must grant them the
CAP_SYS_ADMIN capability. The following
setcap commands assume that you have changed
location to the directory containing your MySQL binaries (adjust
the pathname for your system as necessary):
cd /usr/local/mysql/bin
要授予适当的二进制文件 CAP_SYS_ADMIN 权限:To grant CAP_SYS_ADMIN capability to the
appropriate binaries:
setcap cap_sys_admin+ep ./mysqld setcap cap_sys_admin+ep ./mysql setcap cap_sys_admin+ep ./mysqlxtest
检查 CAP_SYS_ADMIN 权限:To check CAP_SYS_ADMIN capability:
$> getcap ./mysqld ./mysql ./mysqlxtest
./mysqld = cap_sys_admin+ep
./mysql = cap_sys_admin+ep
./mysqlxtest = cap_sys_admin+ep
移除 CAP_SYS_ADMIN 权限:To remove CAP_SYS_ADMIN capability:
setcap -r ./mysqld setcap -r ./mysql setcap -r ./mysqlxtest
如果您重新安装之前已经应用过 setcap 的二进制文件,则必须再次使用 setcap。例如,如果您执行就地升级 MySQL,如果未再次授予 CAP_SYS_ADMIN 权限,将导致与命名空间相关的失败。服务器尝试绑定到具有命名空间的地址时,会出现以下错误:If you reinstall binaries to which you have previously applied
setcap, you must use
setcap again. For example, if you perform
an in-place MySQL upgrade, failure to grant the
CAP_SYS_ADMIN capability again results in
namespace-related failures. The server fails with this error
for attempts to bind to an address with a named namespace:
[ERROR] [MY-013408] [Server] setns() failed with error 'Operation not permitted'
使用 --network-namespace 选项调用的客户端会像这样失败:A client invoked with the
--network-namespace option fails
like this:
ERROR: Network namespace error: Operation not permitted
假设前述主机系统的前提条件已经满足,MySQL 允许配置连接的监听(入站)端的服务器端命名空间以及连接的出站端的客户端命名空间。Assuming that the preceding host system prerequisites have been satisfied, MySQL enables configuring the server-side namespace for the listening (inbound) side of connections and the client-side namespace for the outbound side of connections.
在服务器端,bind_address、admin_address 和 mysqlx_bind_address 系统变量具有扩展语法,用于指定用于监听传入连接的给定 IP 地址或主机名的网络命名空间。要为地址指定命名空间,请添加斜杠和命名空间名称。例如,服务器 my.cnf 文件可能包含以下几行:On the server side, the
bind_address,
admin_address, and
mysqlx_bind_address system
variables have extended syntax for specifying the network
namespace to use for a given IP address or host name on which to
listen for incoming connections. To specify a namespace for an
address, add a slash and the namespace name. For example, a
server my.cnf file might contain these
lines:
[mysqld] bind_address = 127.0.1.1,192.0.2.2/red,198.51.100.2/blue admin_address = 102.0.2.2/red mysqlx_bind_address = 102.0.2.2/red
这些规则适用:These rules apply:
可以为 IP 地址或主机名指定网络命名空间。A network namespace can be specified for an IP address or a host name.
不能为通配符 IP 地址指定网络命名空间。A network namespace cannot be specified for a wildcard IP address.
对于给定的地址,网络命名空间是可选的。如果提供,必须作为紧跟地址的一个
/后缀来指定。For a given address, the network namespace is optional. If given, it must be specified as ans/suffix immediately following the address.ns没有
/后缀的地址使用主机系统的全局命名空间。因此,全局命名空间是默认值。An address with nons/suffix uses the host system global namespace. The global namespace is therefore the default.ns带有
/后缀的地址使用名为nsns的命名空间。An address with a/suffix uses the namespace namednsns.主机系统必须支持网络命名空间,并且每个命名的命名空间必须事先设置好。命名不存在的命名空间会产生错误。The host system must support network namespaces and each named namespace must previously have been set up. Naming a nonexistent namespace produces an error.
bind_address和mysqlx_bind_address接受多个逗号分隔的地址列表,变量值可以指定全局命名空间中的地址、命名命名空间中的地址,或两者混合。bind_addressandmysqlx_bind_addressaccept a list of multiple comma-separated addresses, the variable value can specify addresses in the global namespace, in named namespaces, or a mix.
如果在服务器启动期间尝试使用命名空间时发生错误,服务器将无法启动。如果在 X 插件初始化期间发生错误,导致无法绑定到任何地址,该插件将初始化失败,并且服务器不会加载它。If an error occurs during server startup for attempts to use a namespace, the server does not start. If errors occur for X Plugin during plugin initialization such that it is unable to bind to any address, the plugin fails its initialization sequence and the server does not load it.
在客户端,可以在以下情况下指定网络命名空间:On the client side, a network namespace can be specified in these contexts:
对于 mysql 客户端和 mysqlxtest 测试套件客户端,使用
--network-namespace选项。例如:For the mysql client and the mysqlxtest test suite client, use the--network-namespaceoption. For example:mysql --host=192.0.2.2 --network-namespace=red
如果省略
--network-namespace选项,连接将使用默认(全局)命名空间。If the--network-namespaceoption is omitted, the connection uses the default (global) namespace.对于从副本服务器到源服务器的复制连接,请使用
CHANGE REPLICATION SOURCE TO语句,并指定NETWORK_NAMESPACE选项。例如:For replication connections from replica servers to source servers, use theCHANGE REPLICATION SOURCE TOstatement and specify theNETWORK_NAMESPACEoption. For example:CHANGE REPLICATION SOURCE TO SOURCE_HOST = '192.0.2.2', NETWORK_NAMESPACE = 'red';
如果省略
NETWORK_NAMESPACE选项,复制连接将使用默认(全局)命名空间。If theNETWORK_NAMESPACEoption is omitted, replication connections use the default (global) namespace.
以下示例设置了一个 MySQL 服务器,该服务器在全局、red 和 blue 命名空间中监听连接,并展示了如何配置从 red 和 blue 命名空间连接的帐户。假设 red 和 blue 命名空间已经按照 主机系统先决条件 中的示例创建。The following example sets up a MySQL server that listens for
connections in the global, red, and
blue namespaces, and shows how to configure
accounts that connect from the red and
blue namespaces. It is assumed that the
red and blue namespaces
have already been created as shown in
Host System Prerequisites.
配置服务器以在多个命名空间的地址上监听。将这些行放入服务器的
my.cnf文件中,然后启动服务器:Configure the server to listen on addresses in multiple namespaces. Put these lines in the servermy.cnffile and start the server:[mysqld] bind_address = 127.0.1.1,192.0.2.2/red,198.51.100.2/blue
该值告诉服务器在全局命名空间中监听回环地址
127.0.0.1,在red命名空间中监听地址192.0.2.2,以及在blue命名空间中监听地址198.51.100.2。The value tells the server to listen on the loopback address127.0.0.1in the global namespace, the address192.0.2.2in therednamespace, and the address198.51.100.2in thebluenamespace.连接到全局命名空间中的服务器,并创建具有权限从每个命名命名空间的地址空间中的地址连接的帐户:Connect to the server in the global namespace and create accounts that have permission to connect from an address in the address space of each named namespace:
$>
mysql -u root -h 127.0.0.1 -pEnter password:root_passwordmysql>CREATE USER 'red_user'@'192.0.2.2'IDENTIFIED BY 'mysql>red_user_password';CREATE USER 'blue_user'@'198.51.100.2'IDENTIFIED BY 'blue_user_password';验证您是否可以连接到每个命名命名空间中的服务器:Verify that you can connect to the server in each named namespace:
$>
mysql -u red_user -h 192.0.2.2 --network-namespace=red -pEnter password:red_user_passwordmysql>SELECT USER();+--------------------+ | USER() | +--------------------+ | red_user@192.0.2.2 | +--------------------+$>
mysql -u blue_user -h 198.51.100.2 --network-namespace=blue -pEnter password:blue_user_passwordmysql>SELECT USER();+------------------------+ | USER() | +------------------------+ | blue_user@198.51.100.2 | +------------------------+Note你可能会看到来自
USER()的不同结果,如果你的 DNS 配置能够将地址解析为相应的主机名,并且服务器未启用skip_name_resolve系统变量,它可能会返回包含主机名而不是 IP 地址的值。You might see different results fromUSER(), which can return a value that includes a host name rather than an IP address if your DNS is configured to be able to resolve the address to the corresponding host name and the server is not run with theskip_name_resolvesystem variable enabled.您也可以尝试在不使用
--network-namespace选项的情况下调用 mysql,以查看连接尝试是否成功,如果成功,USER()值会受到什么影响。You might also try invoking mysql without the--network-namespaceoption to see whether the connection attempt succeeds, and, if so, how theUSER()value is affected.
为了复制监控的目的,这些信息源有一列显示连接所适用的网络命名空间:For replication monitoring purposes, these information sources have a column that displays the applicable network namespace for connections:
Performance Schema 的
replication_connection_configuration表。参见 第 29.12.11.11 节,“replication_connection_configuration 表”。The Performance Schemareplication_connection_configurationtable. See Section 29.12.11.11, “The replication_connection_configuration Table”.副本服务器连接元数据存储库。参见 第 19.2.4.2 节,“复制元数据存储库”。The replica server connection metadata repository. See Section 19.2.4.2, “Replication Metadata Repositories”.
SHOW REPLICA STATUS语句。TheSHOW REPLICA STATUSstatement.