- 在 AWS Lambda 函数中使用 mysql2 连接到 TiDB
- Example:
- Configuring SAM deploy
- ======================
- Looking for config file [samconfig.toml] : Not found
- Setting default arguments for 'sam deploy'
- =========================================
- Stack Name [sam-app]: tidb-aws-lambda-quickstart
- AWS Region [us-east-1]:
- #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
- Confirm changes before deploy [y/N]:
- #SAM needs permission to be able to create roles to connect to the resources in your template
- Allow SAM CLI IAM role creation [Y/n]:
- #Preserves the state of previously provisioned resources when an operation fails
- Disable rollback [y/N]:
- tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
- tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
- tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
- tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
- Save arguments to configuration file [Y/n]:
- SAM configuration file [samconfig.toml]:
- SAM configuration environment [default]:
- Looking for resources needed for deployment:
- Creating the required resources...
- Successfully created!
- Bundle for AWS Lambda
- =====================
- dist/index.zip
在 AWS Lambda 函数中使用 mysql2 连接到 TiDB
TiDB 是一个兼容 MySQL 的数据库。AWS Lambda 函数是一个计算服务,mysql2 是当前流行的开源 Node.js Driver 之一。
本文档将展示如何在 AWS Lambda 函数中使用 TiDB 和 mysql2 来完成以下任务:
- 配置你的环境。
- 使用 mysql2 驱动连接到 TiDB 集群。
- 构建并运行你的应用程序。你也可以参考示例代码片段,完成基本的 CRUD 操作。
- 部署你的 AWS Lambda 函数。

注意
本文档适用于 TiDB Serverless 和本地部署的 TiDB。
前置需求
为了能够顺利完成本教程,你需要提前:
- 在你的机器上安装 Node.js 18.x 或以上版本。
- 在你的机器上安装 Git。
- 准备一个 TiDB 集群。
- 准备一个具有管理员权限的 AWS IAM 用户。
- 在你的机器上安装 AWS CLI。
- 在你的机器上安装 AWS SAM CLI。
如果你还没有 TiDB 集群,可以按照以下方式创建:
- (推荐方式)参考创建 TiDB Serverless 集群,创建你自己的 TiDB Cloud 集群。
- 参考部署本地测试 TiDB 集群或部署正式 TiDB 集群,创建本地集群。
如果你还没有 AWS 账户或用户,可以按照 Lambda 入门文档中的步骤来创建它们。
运行代码并连接到 TiDB
本小节演示如何运行示例应用程序的代码,并连接到 TiDB。

注意
完整代码及其运行方式,见代码仓库 tidb-samples/tidb-aws-lambda-quickstart。
第 1 步:克隆示例代码仓库到本地
运行以下命令,将示例代码仓库克隆到本地:
1. git clone git@github.com:tidb-samples/tidb-aws-lambda-quickstart.git
2. cd tidb-aws-lambda-quickstart
第 2 步:安装依赖
运行以下命令,安装示例代码所需要的依赖(包括 mysql2):
1. npm install
第 3 步:配置连接信息
根据不同的 TiDB 部署方式,使用不同的方法连接到 TiDB 集群。
- TiDB Serverless
-
本地部署的 TiDB
-
在 TiDB Cloud 的 Clusters 页面中,选择你的 TiDB Serverless 集群,进入集群的 Overview 页面。
- 点击右上角的 Connect 按钮,将会弹出连接对话框。
-
确认对话框中的选项配置和你的运行环境一致。
-
Endpoint Type 为
Public。 - Branch 选择
main。 - Connect With 选择
General。 - Operating System 为运行示例代码所在的操作系统。

注意
在 Node.js 应用程序中,你无需提供 SSL CA 证书,因为在建立 TLS (SSL) 连接时,默认情况下 Node.js 使用内置的 Mozilla CA 证书。 4. 如果你还没有设置密码,点击 Generate Password 按钮生成一个随机的密码。

小贴士
如果你之前已经生成过密码,可以直接使用原密码,或点击 Reset Password 重新生成密码。
5. 编辑 env.json 文件,按照如下格式设置连接信息,将占位符 {} 替换为从连接对话框中复制的参数值:
1. {
2. "Parameters": {
3. "TIDB_HOST": "{gateway-region}.aws.tidbcloud.com",
4. "TIDB_PORT": "4000",
5. "TIDB_USER": "{prefix}.root",
6. "TIDB_PASSWORD": "{password}"
7. }
8. }
编辑 env.json 文件,按照如下格式设置连接信息,将占位符 {} 替换为你的 TiDB 集群的连接参数值:
1. {
2. "Parameters": {
3. "TIDB_HOST": "{tidb_server_host}",
4. "TIDB_PORT": "4000",
5. "TIDB_USER": "root",
6. "TIDB_PASSWORD": "{password}"
7. }
8. }
第 4 步:运行代码并查看结果
- (前置需求)安装 AWS SAM CLI。
- 构建应用程序包:
1. npm run build
3. 调用示例 Lambda 函数:
1. sam local invoke --env-vars env.json -e events/event.json "tidbHelloWorldFunction"
4. 检查终端中的输出。如果输出类似于以下内容,则表示连接成功:
1. {"statusCode":200,"body":"{\"results\":[{\"Hello World\":\"Hello World\"}]}"}
确认连接成功后,你可以按照部署 AWS Lambda 函数中的步骤进行部署。
部署 AWS Lambda 函数
你可以通过 SAM CLI 或 AWS Lambda 控制台部署 AWS Lambda 函数。
通过 SAM CLI 部署(推荐)
- (前置需求)安装 AWS SAM CLI。
- 构建应用程序包:
1. npm run build
3. 更新 template.yml 文件中的环境变量:
1. Environment:
2. Variables:
3. TIDB_HOST: {tidb_server_host}
4. TIDB_PORT: 4000
5. TIDB_USER: {prefix}.root
6. TIDB_PASSWORD: {password}
4. 参考使用短期凭证进行身份验证文档,设置 AWS 环境变量:
1. export AWS_ACCESS_KEY_ID={your_access_key_id}
2. export AWS_SECRET_ACCESS_KEY={your_secret_access_key}
3. export AWS_SESSION_TOKEN={your_session_token}
5. 部署 AWS Lambda 函数:
``` 1. sam deploy --guided
-
Example:
-
Configuring SAM deploy
-
======================
-
Looking for config file [samconfig.toml] : Not found
-
Setting default arguments for 'sam deploy'
-
=========================================
-
Stack Name [sam-app]: tidb-aws-lambda-quickstart
-
AWS Region [us-east-1]:
-
#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
-
Confirm changes before deploy [y/N]:
-
#SAM needs permission to be able to create roles to connect to the resources in your template
-
Allow SAM CLI IAM role creation [Y/n]:
-
#Preserves the state of previously provisioned resources when an operation fails
-
Disable rollback [y/N]:
-
tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
-
tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
-
tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
-
tidbHelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y
-
Save arguments to configuration file [Y/n]:
-
SAM configuration file [samconfig.toml]:
-
SAM configuration environment [default]:
-
Looking for resources needed for deployment:
-
Creating the required resources...
-
Successfully created!
```
通过网页控制台部署
- 构建应用程序包:
``` 1. npm run build
-
Bundle for AWS Lambda
-
=====================
-
dist/index.zip
``` - 访问 AWS Lambda 控制台。
- 按照使用 Node.js 构建 Lambda 函数中的步骤创建一个 Node.js Lambda 函数。
- 按照 Lambda 部署程序包中的步骤,上传
dist/index.zip文件。 -
在 Lambda 函数中复制并配置相应的连接字符串。
-
在 Lambda 控制台的函数页面中,选择配置 > 环境变量。
- 点击编辑。
-
按照以下步骤添加数据库访问凭证:
- 选择添加环境变量,然后在键中输入
TIDB_HOST,在值中输入主机名。 - 选择添加环境变量,然后在键中输入
TIDB_PORT,在值中输入端口号(默认4000)。 - 选择添加环境变量,然后在键中输入
TIDB_USER,在值中输入用户名。 - 选择添加环境变量,然后在键中输入
TIDB_PASSWORD,在值中输入数据库的密码。 - 点击保存。
- 选择添加环境变量,然后在键中输入
示例代码片段
你可参考以下关键代码片段,完成自己的应用开发。
完整代码及其运行方式,见代码仓库 tidb-samples/tidb-aws-lambda-quickstart。
连接到 TiDB
下面的代码使用环境变量中定义的连接选项来建立与 TiDB 集群的连接。
1. // lib/tidb.ts
2. import mysql from 'mysql2';
4. let pool: mysql.Pool | null = null;
6. function connect() {
7. return mysql.createPool({
8. host: process.env.TIDB_HOST, // TiDB host, for example: {gateway-region}.aws.tidbcloud.com
9. port: process.env.TIDB_PORT ? Number(process.env.TIDB_PORT) : 4000, // TiDB port, default: 4000
10. user: process.env.TIDB_USER, // TiDB user, for example: {prefix}.root
11. password: process.env.TIDB_PASSWORD, // TiDB password
12. database: process.env.TIDB_DATABASE || 'test', // TiDB database name, default: test
13. ssl: {
14. minVersion: 'TLSv1.2',
15. rejectUnauthorized: true,
16. },
17. connectionLimit: 1, // Setting connectionLimit to "1" in a serverless function environment optimizes resource usage, reduces costs, ensures connection stability, and enables seamless scalability.
18. maxIdle: 1, // max idle connections, the default value is the same as `connectionLimit`
19. enableKeepAlive: true,
20. });
21. }
23. export function getPool(): mysql.Pool {
24. if (!pool) {
25. pool = connect();
26. }
27. return pool;
28. }
插入数据
下面的查询会创建一条单独的 Player 记录,并返回一个 ResultSetHeader 对象:
1. const [rsh] = await pool.query('INSERT INTO players (coins, goods) VALUES (?, ?);', [100, 100]);
2. console.log(rsh.insertId);
更多信息参考插入数据。
查询数据
下面的查询会返回一条 Player 记录,其 ID 为 1:
1. const [rows] = await pool.query('SELECT id, coins, goods FROM players WHERE id = ?;', [1]);
2. console.log(rows[0]);
更多信息参考查询数据。
更新数据
下面的查询会将 ID 为 1 的 Player 记录的 coins 和 goods 字段的值分别增加 50:
1. const [rsh] = await pool.query(
2. 'UPDATE players SET coins = coins + ?, goods = goods + ? WHERE id = ?;',
3. [50, 50, 1]
4. );
5. console.log(rsh.affectedRows);
更多信息参考更新数据。
删除数据
下面的查询会删除一条 Player 记录,其 ID 为 1:
1. const [rsh] = await pool.query('DELETE FROM players WHERE id = ?;', [1]);
2. console.log(rsh.affectedRows);
更多信息参考删除数据。
注意事项
- 推荐使用连接池来管理数据库连接,以减少频繁建立和销毁连接所带来的性能开销。
- 为了避免 SQL 注入的风险,推荐使用预处理语句执行 SQL。
- 在不涉及大量复杂 SQL 语句的场景下,推荐使用 ORM 框架(例如:Sequelize、TypeORM 或 Prisma)来提升你的开发效率。
- 如需为你的应用程序构建一个 RESTful API,建议将 AWS Lambda 与 Amazon API Gateway 结合使用。
- 关于使用 TiDB Serverless 和 AWS Lambda 设计高性能应用程序的更多信息,可以参考这篇博客。
下一步
- 关于在 AWS Lambda 函数中使用 TiDB 的更多细节,可以参考 TiDB-Lambda-integration/aws-lambda-bookstore 示例程序。你也可以使用 AWS API Gateway 为你的应用程序构建 RESTful API。
- 关于 mysql2 的更多使用方法,可以参考 mysql2 的官方文档。
- 关于 AWS Lambda 的更多使用方法,可以参考 AWS Lambda 开发者指南。
- 你可以继续阅读开发者文档的其它章节来获取更多 TiDB 应用开发的最佳实践。例如:插入数据,更新数据,删除数据,单表读取,事务,SQL 性能优化等。
- 如果你更倾向于参与课程进行学习,我们也提供专业的 TiDB 开发者课程支持,并在考试后提供相应的资格认证。
