Functiongraph python连接数据库demo
Functiongraph是华为云提供的serverless云服务,该服务可以让用户只需关注code的开发,然后在Functiongraph上部署代码就行了。这和AWS Lambda、Azure Funtions、aliyun Function Compute、google Cloud Functions等是一样的。其是容器技术提供的沙箱环境。
Functiongraph is a serverless cloud service provided by HUAWEI CLOUD. This service allows users to focus on code development and then deploy the code on Functiongraph. This is the same as AWS Lambda, Azure Funtions, aliyun Function Compute, google Cloud Functions, etc. It is a sandbox environment provided by container technology.
一、Python测试代码(Python test code)
由于需要将代码的依赖包和业务代码一起打包,这里选择了比较简单的pymysql数据库包。具体的代码如下:
Because the code dependency package and business code need to be packaged together, the relatively simple pymysql database package is selected here. The specific code is as follows:
1[root@ecs-f0d8 test]# cat testmysql.py
2# -*- coding:utf-8 -*-
3import pymysql.cursors
4
5def handler (event, context):
6 # Connect to the database
7 connection = pymysql.connect(host='host_ip',
8 user='root',
9 password='passwd',
10 db='testdb',
11 charset='utf8mb4',
12 cursorclass=pymysql.cursors.DictCursor)
13
14 try:
15 with connection.cursor() as cursor:
16 # Create a new record
17 sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
18 cursor.execute(sql, ('[email protected]', 'very-secret'))
19
20 # connection is not autocommit by default. So you must commit to save
21 # your changes.
22 connection.commit()
23
24 with connection.cursor() as cursor:
25 # Read a single record
26 sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
27 cursor.execute(sql, ('[email protected]',))
28 result = cursor.fetchone()
29 print(result)
30 finally:
31 connection.close()
32 output = '^_^'
33 return output
二、打包代码(Package code)
打包代码可以参考huaweicloud官方页面:https://support.huaweicloud.com/intl/zh-cn/functiongraph_faq/functiongraph_03_0280.html
For the packaging code, please refer to the official page of huaweicloud: https://support.huaweicloud.com/intl/zh-cn/functiongraph_faq/functiongraph_03_0280.html
1[root@ecs-f0d8 tmp]# pip3 install PyMySQL --root /tmp/pymysql
2[root@ecs-f0d8 site-packages]# cd /tmp/pymysql/usr/local/lib/python3.6/site-packages
3[root@ecs-f0d8 site-packages]# cp -rp pymysql/ /tmp/test/
4[root@ecs-f0d8 test]# ll
5total 108
6drwxr-xr-x 4 root root 4096 Aug 24 23:14 pymysql
7-rw-r--r-- 1 root root 1150 Aug 24 23:45 testmysql.py
8[root@ecs-f0d8 test]# zip -r * test.zip
将打包好的zip包上传到functiongraph环境,上传完成后的界面如下(Upload the packaged zip file to the functiongraph environment. The web interface after uploading is as follows):
当然也可以在该页面的下拉界面通过提前上传的依赖包进行选择。Of course, you can also select the dependency package uploaded in advance in the drop-down interface of this page.
三、数据库及配置(Database and Configuration)
数据库我使用的是云服务RDS,由于FunctionGraph 所在的VPC和RDS所在的VPC不同,所以这里需要配置FunctionGraph访问VPC、RDS的委托。
I use the cloud service RDS for the database. Because the VPC where the FunctionGraph is located is different from the VPC where the RDS is located, it is necessary to configure the agencies of the FunctionGraph to access the VPC and RDS.
1. 创建数据库和表(Create Database and table)
1MySQL [none]> create database testdb;
2MySQL [testdb]> create table users (id INT(11) NOT NULL AUTO_INCREMENT,email varchar(255), password varchar(255), PRIMARY KEY (id));
2. 创建委托(Create Agency)
3. 开启 VPC 访问(enable vpc access):
在 configuration —- Basic Settings里设置Handler名称,名称设置为testmysql.handler,其规则是文件外.函数名。
Set the Handler name in configuration —- Basic Settings,The name is set to testmysql.handler and its rule is [filename].[function name].
注意:Configuration — Advanced Settings — Initialization可以设置初始化入口函数。( Note: Configuration — Advanced Settings — Initialization can set the initialization entry function. )
四、验证(verify)
通过Configure Test Event测试 或 通过创建Trigger测试,测试效果如下(Test by Configure Test Event or by creating Trigger test, the test effect is as follows):
捐赠本站(Donate)
如您感觉文章有用,可扫码捐赠本站!(If the article useful, you can scan the QR code to donate))
- Author: shisekong
- Link: https://blog.361way.com/functiongraph-python-database/6879.html
- License: This work is under a 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议. Kindly fulfill the requirements of the aforementioned License when adapting or creating a derivative of this work.