📣

TiDB Cloud Serverless 现已更名为
Starter
!此页面由 AI 自动翻译,英文原文请见
此处。

TiDB Cloud Dedicated 的 TLS 连接

在 TiDB Cloud 上,建立 TLS 连接是连接 TiDB Cloud Dedicated 集群的基本安全实践之一。你可以从客户端、应用程序和开发工具配置多种 TLS 连接到你的 TiDB Cloud Dedicated 集群,以保护数据传输的安全。出于安全考虑,TiDB Cloud Dedicated 仅支持 TLS 1.2 和 TLS 1.3,不支持 TLS 1.0 和 TLS 1.1 版本。

为确保数据安全,你的 TiDB Cloud Dedicated 集群的 CA 证书托管在 AWS Private Certificate Authority 上。CA 证书的私钥存储在 AWS 管理的硬件安全模块(HSM)中,这些模块符合 FIPS 140-2 Level 3 安全标准。

前置条件

  • 通过 密码认证SSO 认证 登录 TiDB Cloud,然后 创建 TiDB Cloud Dedicated 集群

  • 在安全设置中为你的集群设置访问密码。

    操作方法:你可以进入项目的 Clusters 页面,在你的 TiDB Cloud Dedicated 集群所在行点击 ...,然后选择 Password Settings。在密码设置中,你可以点击 Auto-generate Password,自动生成一个包含数字、大小写字母和特殊字符、长度为 16 位的 root 密码。

安全连接到 TiDB Cloud Dedicated 集群

TiDB Cloud 控制台 中,你可以获取不同连接方式的示例,并按如下步骤连接到你的 TiDB Cloud Dedicated 集群:

  1. 进入项目的 Clusters 页面,点击你的 TiDB Cloud Dedicated 集群名称,进入集群概览页面。

  2. 点击右上角的 Connect,弹出连接对话框。

  3. 在连接对话框中,从 Connection Type 下拉列表选择 Public

    如果你还未配置 IP 访问列表,请点击 Configure IP Access List 进行首次连接前的配置。详细信息参见 配置 IP 访问列表

  4. 点击 CA cert 下载用于 TLS 连接 TiDB 集群的 CA 证书。该 CA 证书默认支持 TLS 1.2 版本。

  5. 选择你喜欢的连接方式,然后参考标签页中的连接字符串和示例代码连接到你的集群。

以下示例展示了 MySQL、MyCLI、JDBC、Python、Go 和 Node.js 的连接字符串:

    MySQL CLI 客户端默认尝试建立 TLS 连接。连接 TiDB Cloud Dedicated 集群时,你需要设置 ssl-modessl-ca

    mysql --connect-timeout 15 --ssl-mode=VERIFY_IDENTITY --ssl-ca=ca.pem --tls-version="TLSv1.2" -u root -h tidb.eqlfbdgthh8.clusters.staging.tidb-cloud.com -P 4000 -D test -p

    参数说明:

    • 使用 --ssl-mode=VERIFY_IDENTITY,MySQL CLI 客户端强制启用 TLS 并校验 TiDB Cloud Dedicated 集群。
    • 使用 --ssl-ca=<CA_path> 指定你本地下载的 TiDB 集群 ca.pem 路径。
    • 使用 --tls-version=TLSv1.2 限定 TLS 协议版本。如果你想使用 TLS 1.3,可以将版本设置为 TLSv1.3

    MyCLI 在使用 TLS 相关参数时会自动启用 TLS。连接 TiDB Cloud Dedicated 集群时,你需要设置 ssl-cassl-verify-server-cert

    mycli --ssl-ca=ca.pem --ssl-verify-server-cert -u root -h tidb.eqlfbdgthh8.clusters.staging.tidb-cloud.com -P 4000 -D test

    参数说明:

    • 使用 --ssl-ca=<CA_path> 指定你本地下载的 TiDB 集群 ca.pem 路径。
    • 使用 --ssl-verify-server-cert 校验 TiDB Cloud Dedicated 集群。

    这里以 MySQL Connector/J 的 TLS 连接配置为例。

    下载 TiDB 集群 CA 后,如果你想将其导入操作系统,可以使用 keytool -importcert -alias TiDBCACert -file ca.pem -keystore <your_custom_truststore_path> -storepass <your_truststore_password> 命令。

    /* Be sure to replace the parameters in the following connection string. */ /* version >= 8.0.28 */ jdbc:mysql://tidb.srgnqxji5bc.clusters.staging.tidb-cloud.com:4000/test?user=root&password=<your_password>&sslMode=VERIFY_IDENTITY&tlsVersions=TLSv1.2&trustCertificateKeyStoreUrl=file:<your_custom_truststore_path>&trustCertificateKeyStorePassword=<your_truststore_password>

    你可以点击 show example usage 查看详细代码示例。

    import com.mysql.jdbc.Driver; import java.sql.*; class Main { public static void main(String args[]) throws SQLException, ClassNotFoundException { Class.forName("com.mysql.cj.jdbc.Driver"); try { Connection conn = DriverManager.getConnection("jdbc:mysql://tidb.srgnqxji5bc.clusters.staging.tidb-cloud.com:4000/test?user=root&password=<your_password>&sslMode=VERIFY_IDENTITY&tlsVersions=TLSv1.2&trustCertificateKeyStoreUrl=file:<your_custom_truststore_path>&trustCertificateKeyStorePassword=<your_truststore_password>"); Statement stmt = conn.createStatement(); try { ResultSet rs = stmt.executeQuery("SELECT DATABASE();"); if (rs.next()) { System.out.println("using db:" + rs.getString(1)); } } catch (Exception e) { System.out.println("exec error:" + e); } } catch (Exception e) { System.out.println("connect error:" + e); } } }

    参数说明:

    • 设置 sslMode=VERIFY_IDENTITY 启用 TLS 并校验 TiDB Cloud Dedicated 集群。
    • 设置 enabledTLSProtocols=TLSv1.2 限定 TLS 协议版本。如果你想使用 TLS 1.3,可以将版本设置为 TLSv1.3
    • 设置 trustCertificateKeyStoreUrl 为你自定义的 truststore 路径。
    • 设置 trustCertificateKeyStorePassword 为你的 truststore 密码。

    这里以 mysqlclient 的 TLS 连接配置为例。

    host="tidb.srgnqxji5bc.clusters.staging.tidb-cloud.com", user="root", password="<your_password>", port=4000, database="test", ssl_mode="VERIFY_IDENTITY", ssl={"ca": "ca.pem"}

    你可以点击 show example usage 查看详细代码示例。

    import MySQLdb connection = MySQLdb.connect(host="tidb.srgnqxji5bc.clusters.staging.tidb-cloud.com", port=4000, user="root", password="<your_password>", database="test", ssl_mode="VERIFY_IDENTITY", ssl={"ca": "ca.pem"}) with connection: with connection.cursor() as cursor: cursor.execute("SELECT DATABASE();") m = cursor.fetchone() print(m[0])

    参数说明:

    • 设置 ssl_mode="VERIFY_IDENTITY" 启用 TLS 并校验 TiDB Cloud Dedicated 集群。
    • 使用 ssl={"ca": "<CA_path>"} 指定你本地下载的 TiDB 集群 ca.pem 路径。

    这里以 Go-MySQL-Driver 的 TLS 连接配置为例。

    rootCertPool := x509.NewCertPool() pem, err := ioutil.ReadFile("ca.pem") if err != nil { log.Fatal(err) } if ok := rootCertPool.AppendCertsFromPEM(pem); !ok { log.Fatal("Failed to append PEM.") } mysql.RegisterTLSConfig("tidb", &tls.Config{ RootCAs: rootCertPool, MinVersion: tls.VersionTLS12, ServerName: "tidb.srgnqxji5bc.clusters.staging.tidb-cloud.com", }) db, err := sql.Open("mysql", "root:<your_password>@tcp(tidb.srgnqxji5bc.clusters.staging.tidb-cloud.com:4000)/test?tls=tidb")

    你可以点击 show example usage 查看详细代码示例。

    package main import ( "crypto/tls" "crypto/x509" "database/sql" "fmt" "io/ioutil" "log" "github.com/go-sql-driver/mysql" ) func main() { rootCertPool := x509.NewCertPool() pem, err := ioutil.ReadFile("ca.pem") if err != nil { log.Fatal(err) } if ok := rootCertPool.AppendCertsFromPEM(pem); !ok { log.Fatal("Failed to append PEM.") } mysql.RegisterTLSConfig("tidb", &tls.Config{ RootCAs: rootCertPool, MinVersion: tls.VersionTLS12, ServerName: "tidb.srgnqxji5bc.clusters.staging.tidb-cloud.com", }) db, err := sql.Open("mysql", "root:<your_password>@tcp(tidb.srgnqxji5bc.clusters.staging.tidb-cloud.com:4000)/test?tls=tidb") if err != nil { log.Fatal("failed to connect database", err) } defer db.Close() var dbName string err = db.QueryRow("SELECT DATABASE();").Scan(&dbName) if err != nil { log.Fatal("failed to execute query", err) } fmt.Println(dbName) }

    参数说明:

    • 在 TLS 连接配置中注册 tls.Config,以启用 TLS 并校验 TiDB Cloud Dedicated 集群。
    • 设置 MinVersion: tls.VersionTLS12 限定 TLS 协议版本。
    • 设置 ServerName: "<host>" 校验 TiDB Cloud Dedicated 的主机名。
    • 如果你不想注册新的 TLS 配置,也可以直接在连接字符串中设置 tls=true

    这里以 Mysql2 的 TLS 连接配置为例。

    var connection = mysql.createConnection({ host: 'tidb.srgnqxji5bc.clusters.staging.tidb-cloud.com', port: 4000, user: 'root', password: '<your_password>', database: 'test', ssl: { ca: fs.readFileSync('ca.pem'), minVersion: 'TLSv1.2', rejectUnauthorized: true } });

    你可以点击 show example usage 查看详细代码示例。

    var mysql = require('mysql2'); var fs = require('fs'); var connection = mysql.createConnection({ host: 'tidb.srgnqxji5bc.clusters.staging.tidb-cloud.com', port: 4000, user: 'root', password: '<your_password>', database: 'test', ssl: { ca: fs.readFileSync('ca.pem'), minVersion: 'TLSv1.2', rejectUnauthorized: true } }); connection.connect(function(err) { if (err) { throw err } connection.query('SELECT DATABASE();', function(err, rows) { if (err) { throw err } console.log(rows[0]['DATABASE()']); connection.end() }); });

    参数说明:

    • 设置 ssl: {minVersion: 'TLSv1.2'} 限定 TLS 协议版本。如果你想使用 TLS 1.3,可以将版本设置为 TLSv1.3
    • 设置 ssl: {ca: fs.readFileSync('<CA_path>')} 读取你本地下载的 TiDB 集群 ca.pem 路径。

    管理 TiDB Cloud Dedicated 的根证书

    TiDB Cloud Dedicated 使用来自 AWS Private Certificate Authority 的证书作为客户端与 TiDB Cloud Dedicated 集群之间 TLS 连接的证书颁发机构(CA)。通常,CA 证书的私钥安全地存储在 AWS 管理的硬件安全模块(HSM)中,这些模块符合 FIPS 140-2 Level 3 安全标准。

    常见问题

    连接 TiDB Cloud Dedicated 集群支持哪些 TLS 版本?

    出于安全考虑,TiDB Cloud Dedicated 仅支持 TLS 1.2 和 TLS 1.3,不支持 TLS 1.0 和 TLS 1.1 版本。详情参见 IETF Deprecating TLS 1.0 and TLS 1.1

    我的客户端与 TiDB Cloud Dedicated 支持双向 TLS 认证吗?

    不支持。

    TiDB Cloud Dedicated 目前仅支持单向 TLS 认证,不支持双向 TLS 认证。如果你需要双向 TLS 认证,请联系 TiDB Cloud Support

    文档内容是否有帮助?