本示例为您展示了Python runtime的mysql使用示例。 在本示例中,mysql数据库配置在函数的环境变量配置中(参考s.yaml),initializer 回调函数从环境变量中获取数据库配置,创建mysql连接并测试连通性,preStop 回调函数负责关闭mysql连接。
- 一个可用的mysql数据库,可以参考以下sql创建表并插入测试数据
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) NOT NULL,
`age` tinyint(11) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `users` (`id`, `name`, `age`) VALUES
(1, '张三', 18),
(2, '李四', 28);- [可选] 安装并配置 Serverless Devs 工具,以及 docker daemon。(https://help.aliyun.com/document_detail/195474.html)
- 根据需要修改 access 配置
- 修改 environmentVariables 配置,填入 MYSQL_USER, MYSQL_PASSWORD, MYSQL_ENDPOINT, MYSQL_PORT 和 MYSQL_DBNAME
- 使用 initialize 和 prestop 回调,需要在 s.yaml 中配置
initializationTimeout: 20
initializer: index.initialize
instanceLifecycleConfig:
preStop:
handler: index.pre_stop
timeout: 20安装依赖库 PyMySQL
# 使用s工具安装依赖,需要使用 docker
s build --use-docker注意: 使用
s build --use-docker会将依赖安装到代码包的.s/python目录下,可以在本地的.s/build/artifacts/{serviceName}/{functionName}/查看。
部署代码
# 部署代码,会自动添加环境变量 PYTHONUSERBASE=/code/.s/python,该环境变量是必须的
s deploys invoke调用函数时收到的响应如下所示:
➜ python3-mysql s invoke
========= FC invoke Logs begin =========
FunctionCompute python3 runtime inited.
FC Initialize Start RequestId: 28fa11ab-81da-4cd0-b050-xxxxxxxxxx
FC Initialize End RequestId: 28fa11ab-81da-4cd0-b050-xxxxxxxxxx
FC Invoke Start RequestId: 28fa11ab-81da-4cd0-b050-xxxxxxxxxx
2022-03-31T02:57:49.693Z 28fa11ab-81da-4cd0-b050-xxxxxxxxxx [INFO] (3, '王二', 38)
FC Invoke End RequestId: 28fa11ab-81da-4cd0-b050-xxxxxxxxxx
Duration: 18.42 ms, Billed Duration: 19 ms, Memory Size: 128 MB, Max Memory Used: 34.80 MB
========= FC invoke Logs end =========
FC Invoke Result:
user: (3, '王二', 38)
End of method: invoke# 安装依赖到 /code 目录
cd code && pip3 install -r requirements.txt -t .
# 打包文件
cd code && zip -r python3-mysql.zip *创建函数并上传代码包
当使用云数据库时,一般都会有访问控制,比如阿里云数据库RDS中的白名单设置( RDS白名单设置说明 )。
如果仅仅作为测试,可以将白名单配置成 0.0.0.0/0。(不要在生产环境使用!)
在生产环境,可以使用以下两种方式访问:
- VPC方式(推荐)
参考文档:https://help.aliyun.com/document_detail/84514.html - 代理方式
参考文档:https://help.aliyun.com/document_detail/91243.html
- 本示例不是连接池方式,因python不支持单实例多并发,可以不使用连接池,若要使用连接池,可以参考文档 DBUtils User's Guide


