要在复制期间传输二进制日志时使用加密连接,源服务器和副本服务器都必须支持加密的网络连接。如果任一服务器不支持加密连接(因为它未被编译或配置为支持加密连接),则无法通过加密连接进行复制。To use an encrypted connection for the transfer of the binary log required during replication, both the source and the replica servers must support encrypted network connections. If either server does not support encrypted connections (because it has not been compiled or configured for them), replication through an encrypted connection is not possible.
为复制设置加密连接类似于为客户端/服务器连接设置加密连接。您必须获取(或创建)一个可用于源服务器的合适安全证书,并在每个副本服务器上使用来自同一证书颁发机构的类似证书。您还必须获取合适的密钥文件。Setting up encrypted connections for replication is similar to doing so for client/server connections. You must obtain (or create) a suitable security certificate that you can use on the source, and a similar certificate (from the same certificate authority) on each replica. You must also obtain suitable key files.
有关为服务器和客户端设置加密连接的更多信息,请参阅 第 8.3.1 节,“配置 MySQL 使用加密连接”。For more information on setting up a server and client for encrypted connections, see Section 8.3.1, “Configuring MySQL to Use Encrypted Connections”.
要在源端启用加密连接,您必须创建或获取合适的证书和密钥文件,然后将以下配置参数添加到源端 my.cnf 文件的 [mysqld] 部分,并根据需要更改文件名:To enable encrypted connections on the source, you must create or
obtain suitable certificate and key files, and then add the
following configuration parameters to the
[mysqld] section of the source
my.cnf file, changing the file names as
necessary:
[mysqld] ssl_ca=cacert.pem ssl_cert=server-cert.pem ssl_key=server-key.pem
文件路径可以是相对路径或绝对路径;我们建议您始终使用完整路径。The paths to the files may be relative or absolute; we recommend that you always use complete paths for this purpose.
配置参数如下:The configuration parameters are as follows:
ssl_ca:证书颁发机构(CA)证书文件的路径名。(ssl_capath类似,但它指定的是 CA 证书文件所在目录的路径名。)ssl_ca: The path name of the Certificate Authority (CA) certificate file. (ssl_capathis similar but specifies the path name of a directory of CA certificate files.)ssl_cert:服务器公钥证书文件的路径名。该证书可以发送给客户端,并可以根据客户端所持有的 CA 证书进行验证。ssl_cert: The path name of the server public key certificate file. This certificate can be sent to the client and authenticated against the CA certificate that it has.ssl_key:服务器私钥文件的路径名。ssl_key: The path name of the server private key file.
要在副本端启用加密连接,请使用 CHANGE REPLICATION SOURCE TO 语句。To enable encrypted connections on the replica, use the
CHANGE REPLICATION SOURCE TO
statement.
要使用
CHANGE REPLICATION SOURCE TO为副本的证书和 SSL 私钥文件命名,请添加相应的SOURCE_SSL_选项,如下所示:To name the replica's certificate and SSL private key files usingxxxCHANGE REPLICATION SOURCE TO, add the appropriateSOURCE_SSL_options, like this:xxx->
SOURCE_SSL_CA = 'ca_file_name',->SOURCE_SSL_CAPATH = 'ca_directory_name',->SOURCE_SSL_CERT = 'cert_file_name',->SOURCE_SSL_KEY = 'key_file_name',这些选项对应于具有相同名称的
--ssl-选项,如 加密连接的命令选项 中所述。要使这些选项生效,必须同时设置xxxSOURCE_SSL=1。对于复制连接,为SOURCE_SSL_CA或SOURCE_SSL_CAPATH指定值相当于设置--ssl-mode=VERIFY_CA。只有在使用指定信息找到有效匹配的证书颁发机构 (CA) 证书时,连接尝试才会成功。These options correspond to the--ssl-options with the same names, as described in Command Options for Encrypted Connections. For these options to take effect,xxxSOURCE_SSL=1must also be set. For a replication connection, specifying a value for either ofSOURCE_SSL_CAorSOURCE_SSL_CAPATHcorresponds to setting--ssl-mode=VERIFY_CA. The connection attempt succeeds only if a valid matching Certificate Authority (CA) certificate is found using the specified information.要启用主机名身份验证,请添加
SOURCE_SSL_VERIFY_SERVER_CERT选项,如下所示:To activate host name identity verification, add theSOURCE_SSL_VERIFY_SERVER_CERToption, like this:->
SOURCE_SSL_VERIFY_SERVER_CERT=1,对于复制连接,指定
SOURCE_SSL_VERIFY_SERVER_CERT=1相当于设置--ssl-mode=VERIFY_IDENTITY,如 加密连接的命令选项 中所述。要使此选项生效,还必须设置SOURCE_SSL=1。主机名身份验证不适用于自签名证书。For a replication connection, specifyingSOURCE_SSL_VERIFY_SERVER_CERT=1corresponds to setting--ssl-mode=VERIFY_IDENTITY, as described in Command Options for Encrypted Connections. For this option to take effect,SOURCE_SSL=1must also be set. Host name identity verification does not work with self-signed certificates.要启用证书撤销列表 (CRL) 检查,请添加
SOURCE_SSL_CRL或SOURCE_SSL_CRLPATH选项,如下所示:To activate certificate revocation list (CRL) checks, add theSOURCE_SSL_CRLorSOURCE_SSL_CRLPATHoption, as shown here:->
SOURCE_SSL_CRL = 'crl_file_name',->SOURCE_SSL_CRLPATH = 'crl_directory_name',这些选项对应于具有相同名称的
--ssl-选项,如 加密连接的命令选项 中所述。如果未指定这些选项,将不会进行 CRL 检查。These options correspond to thexxx--ssl-options with the same names, as described in Command Options for Encrypted Connections. If they are not specified, no CRL checking takes place.xxx要为副本指定复制连接允许的密码、密码套件和加密协议列表,请使用
SOURCE_SSL_CIPHER、SOURCE_TLS_VERSION和SOURCE_TLS_CIPHERSUITES选项,如下所示:To specify lists of ciphers, ciphersuites, and encryption protocols permitted by the replica for the replication connection, use theSOURCE_SSL_CIPHER,SOURCE_TLS_VERSION, andSOURCE_TLS_CIPHERSUITESoptions, like this:->
SOURCE_SSL_CIPHER = 'cipher_list',->SOURCE_TLS_VERSION = 'protocol_list',->SOURCE_TLS_CIPHERSUITES = 'ciphersuite_list',SOURCE_SSL_CIPHER选项指定由副本允许用于复制连接的一个或多个密码的冒号分隔列表。TheSOURCE_SSL_CIPHERoption specifies a colon-separated list of one or more ciphers permitted by the replica for the replication connection.SOURCE_TLS_VERSION选项指定副本在复制连接中允许使用的 TLS 加密协议的逗号分隔列表,格式类似于tls_version服务器系统变量。连接过程会协商使用源和副本都允许的最高 TLS 版本。要能够连接,副本必须至少与源共享一个 TLS 版本。TheSOURCE_TLS_VERSIONoption specifies a comma-separated list of the TLS encryption protocols permitted by the replica for the replication connection, in a format like that for thetls_versionserver system variable. The connection procedure negotiates the use of the highest TLS version that both the source and the replica permit. To be able to connect, the replica must have at least one TLS version in common with the source.SOURCE_TLS_CIPHERSUITES选项指定一个或多个由冒号分隔的加密套件列表,如果连接使用 TLSv1.3,则副本允许这些加密套件用于复制连接。如果在使用 TLSv1.3 时将此选项设置为NULL(如果不设置该选项,这是默认值),则允许使用默认启用的加密套件。如果将该选项设置为空字符串,则不允许任何加密套件,因此不使用 TLSv1.3。TheSOURCE_TLS_CIPHERSUITESoption specifies a colon-separated list of one or more ciphersuites that are permitted by the replica for the replication connection if TLSv1.3 is used for the connection. If this option is set toNULLwhen TLSv1.3 is used (which is the default if you do not set the option), the ciphersuites that are enabled by default are allowed. If you set the option to an empty string, no cipher suites are allowed, and TLSv1.3 is therefore not used.
您可以在这些列表中指定的协议、密码和密码套件取决于用于编译 MySQL 的 SSL 库。有关格式、允许的值以及如果未指定选项时的默认值的信息,请参见 第 8.3.2 节,“加密连接 TLS 协议和密码”。The protocols, ciphers, and ciphersuites that you can specify in these lists depend on the SSL library used to compile MySQL. For information about the formats, the permitted values, and the defaults if you do not specify the options, see Section 8.3.2, “Encrypted Connection TLS Protocols and Ciphers”.
Note您可以使用
SOURCE_TLS_CIPHERSUITES选项来指定任何选择的密码套件,如果愿意,也可以只包括非默认的密码套件。You can use theSOURCE_TLS_CIPHERSUITESoption to specify any selection of ciphersuites, including only non-default ciphersuites if you want.在源信息更新后,在从属服务器上启动复制过程,如下所示:After the source information has been updated, start the replication process on the replica, like this:
mysql>
START REPLICA;您可以使用
SHOW REPLICA STATUS语句来确认是否成功建立了加密连接。You can use theSHOW REPLICA STATUSstatement to confirm that an encrypted connection was established successfully.在副本上要求加密连接,并不确保源端要求副本使用加密连接。如果您想确保源端仅接受通过加密连接连接的副本,请在源端使用
REQUIRE SSL选项创建一个复制用户账户,然后授予该用户REPLICATION SLAVE权限。例如:Requiring encrypted connections on the replica does not ensure that the source requires encrypted connections from replicas. If you want to ensure that the source only accepts replicas that connect using encrypted connections, create a replication user account on the source using theREQUIRE SSLoption, then grant that user theREPLICATION SLAVEprivilege. For example:mysql>
CREATE USER 'repl'@'%.example.com' IDENTIFIED BY '->password'REQUIRE SSL;mysql>GRANT REPLICATION SLAVE ON *.*->TO 'repl'@'%.example.com';如果您在源端已有现有的复制用户账户,可以使用以下语句向其添加
REQUIRE SSL:If you have an existing replication user account on the source, you can addREQUIRE SSLto it with this statement:mysql>
ALTER USER 'repl'@'%.example.com' REQUIRE SSL;