操作步骤 | 操作说明 |
- | |
通过 Serverless Framework VPC 组件 创建 VPC 和 子网,支持云函数和数据库的网络打通和使用。 | |
通过 Serverless Framework Cynosdb 组件创建 MySQL 实例,为云函数项目提供数据库服务。 | |
通过 Serverless DB SDK 调用数据库,云函数支持直接调用 Serverless DB SDK,连接 PostgreSQL 数据库进行管理操作。 | |
通过 Serverless Framework 部署项目至云端,并通过云函数控制台进行测试。 | |
可通过 Serverless Framework 移除项目。 |
test-MySQL
文件夹为例。mkdir test-MySQL && cd test-MySQL
ap-beijing-3
,ap-guangzhou-4
,ap-shanghai-2
和 ap-nanjing-1
四个区域,所以这里还需要配置下,只需要在项目根目录下创建 .env
文件,然后配置 REGION
和 ZONE
两个环境变量:# .envREGION=xxxZONE=xxx
test-MySQL
目录下创建文件夹 VPC
。mkdir VPC && cd VPC
#serverless.ymlapp: mysql-appstage: devcomponent: vpc # (required) name of the component. In that case, it's vpc.name: mysql-app-vpc # (required) name of your vpc component instance.inputs:region: ${env:REGION}zone: ${env:ZONE}vpcName: serverless-mysqlsubnetName: serverless-mysql
test-MySQL
下创建文件夹 DB
。DB
文件夹下新建 serverless.yml
文件,并输入以下内容,通过 Serverless Framework 组件完成云开发环境配置。
serverless.yml
示例内容如下(全量配置参考 产品文档):# serverless.ymlapp: mysql-appstage: devcomponent: cynosdbname: mysql-app-dbinputs:region: ${env:REGION}zone: ${env:ZONE}vpcConfig:vpcId: ${output:${stage}:${app}:mysql-app-vpc.vpcId}subnetId: ${output:${stage}:${app}:mysql-app-vpc.subnetId}
test-MySQL
下创建文件夹 src
,用于存放业务逻辑代码和相关依赖项。src
文件夹下创建文件 index.js
,并输入如下示例代码。在函数中通过 SDK 连接数据库,并在其中完成 MySQL 数据库的调用。exports.main_handler = async (event, context, callback) => {var mysql = require('mysql2');var connection = mysql.createConnection({host : process.env.HOST,user : 'root',password : process.env.PASSWORD});connection.connect();connection.query('SELECT 1 + 1 AS solution', function (error, results, fields) {if (error) throw error;console.log('The solution is: ', results[0].solution);});connection.end();}
npm install mysql2
serverless.yml
文件,示例文件如下:app: mysql-appstage: devcomponent: scfname: mysql-app-scfinputs:src: ./functionName: ${name}region: ${env:REGION}runtime: Nodejs10.15timeout: 30vpcConfig:vpcId: ${output:${stage}:${app}:mysql-app-vpc.vpcId}subnetId: ${output:${stage}:${app}:mysql-app-vpc.subnetId}environment:variables:HOST: ${output:${stage}:${app}:mysql-app-db.connection.ip}PASSWORD: ${output:${stage}:${app}:mysql-app-db.adminPassword}
./test-MySQL├── vpc│ └── serverless.yml # vpc 配置文件├── db│ └── serverless.yml # db 配置文件├── src│ ├── serverless.yml # scf 组件配置文件│ ├── node_modules # 项目依赖文件│ └── index.js # 入口函数└── .env # 环境变量文件
test-MySQL
下,执行以下命令进行部署。sls deploy
mysql-app-vpc:region: xxxzone: xxxvpcId: xxxx-xxx...mysql-app-db:dbMode: xxxxregion: xxxxzone: xxxx...mysql-app-scf:functionName: xxxxdescription: xxx...59s › test-MySQL › "deploy" ran for 3 apps successfully.
test-MySQL
目录下,执行以下命令可移除项目。sls remove
serverless ⚡ framework4s › test-MySQL › Success
# -*- coding: utf8 -*-from os import getenvimport pymysqlfrom pymysql.err import OperationalErrormysql_conn = Nonedef __get_cursor():try:return mysql_conn.cursor()except OperationalError:mysql_conn.ping(reconnect=True)return mysql_conn.cursor()def main_handler(event, context):global mysql_connif not mysql_conn:mysql_conn = pymysql.connect(host = getenv('DB_HOST', '<YOUR DB HOST>'),user = getenv('DB_USER','<YOUR DB USER>'),password = getenv('DB_PASSWORD','<YOUR DB PASSWORD>'),db = getenv('DB_DATABASE','<YOUR DB DATABASE>'),port = int(getenv('DB_PORT','<YOUR DB PORT>')),charset = 'utf8mb4',autocommit = True)with __get_cursor() as cursor:cursor.execute('select * from employee')myresult = cursor.fetchall()print(myresult)for x in myresult:print(x)
'use strict';const DB_HOST = process.env[`DB_HOST`]const DB_PORT = process.env[`DB_PORT`]const DB_DATABASE = process.env[`DB_DATABASE`]const DB_USER = process.env[`DB_USER`]const DB_PASSWORD = process.env[`DB_PASSWORD`]const promisePool = require('mysql2').createPool({host : DB_HOST,user : DB_USER,port : DB_PORT,password : DB_PASSWORD,database : DB_DATABASE,connectionLimit : 1}).promise();exports.main_handler = async (event, context, callback) => {let result = await promisePool.query('select * from employee');console.log(result);}
<?phpfunction handler($event, $context) {try{$pdo = new PDO('mysql:host= getenv("DB_HOST");dbname= getenv("DB_DATABASE"),getenv("DB_USER"),getenv("DB_PASSWORD")');$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);}catch(PDOException $e){echo '数据库连接失败: '.$e->getMessage();exit;}}
<?phpfunction main_handler($event, $context) {$host = "";$username = "";$password = "";// 创建连接$conn = mysqli_connect($servername, $username, $password);// 检测连接if (!$conn) {die("Connection failed: " . mysqli_connect_error());}echo "连接成功";mysqli_close($conn);echo "断开连接";}?>
<dependencies><dependency><groupId>com.tencentcloudapi</groupId><artifactId>scf-java-events</artifactId><version>0.0.2</version></dependency><dependency><groupId>com.zaxxer</groupId><artifactId>HikariCP</artifactId><version>3.2.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.11</version></dependency></dependencies>
package example;import com.qcloud.scf.runtime.Context;import com.qcloud.services.scf.runtime.events.APIGatewayProxyRequestEvent;import com.qcloud.services.scf.runtime.events.APIGatewayProxyResponseEvent;import com.zaxxer.hikari.HikariConfig;import com.zaxxer.hikari.HikariDataSource;import javax.sql.DataSource;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.HashMap;import java.util.Map;public class Http {private DataSource dataSource;public Http() {HikariConfig config = new HikariConfig();config.setJdbcUrl("jdbc:mysql://" + System.getenv("DB_HOST") + ":"+ System.getenv("DB_PORT") + "/" + System.getenv("DB_DATABASE"));config.setUsername(System.getenv("DB_USER"));config.setPassword(System.getenv("DB_PASSWORD"));config.setDriverClassName("com.mysql.jdbc.Driver");config.setMaximumPoolSize(1);dataSource = new HikariDataSource(config);}public String mainHandler(APIGatewayProxyRequestEvent requestEvent, Context context) {System.out.println("start main handler");System.out.println("requestEvent: " + requestEvent);System.out.println("context: " + context);try (Connection conn = dataSource.getConnection(); PreparedStatement ps = conn.prepareStatement("SELECT * FROM employee")) {ResultSet rs = ps.executeQuery();while (rs.next()) {System.out.println(rs.getInt("id"));System.out.println(rs.getString("first_name"));System.out.println(rs.getString("last_name"));System.out.println(rs.getString("address"));System.out.println(rs.getString("city"));}} catch (SQLException e) {e.printStackTrace();}APIGatewayProxyResponseEvent apiGatewayProxyResponseEvent = new APIGatewayProxyResponseEvent();apiGatewayProxyResponseEvent.setBody("API GW Test Success");apiGatewayProxyResponseEvent.setIsBase64Encoded(false);apiGatewayProxyResponseEvent.setStatusCode(200);Map<String, String> headers = new HashMap<>();headers.put("Content-Type", "text");headers.put("Access-Control-Allow-Origin", "*");apiGatewayProxyResponseEvent.setHeaders(headers);return apiGatewayProxyResponseEvent.toString();}}
'use strict';const database = require('scf-nodejs-serverlessdb-sdk').database;exports.main_handler = async (event, context, callback) => {let pool = await database('TESTDB2').pool()pool.query('select * from coffee',(err,results)=>{console.log('db2 callback query result:',results)})// no need to release poolconsole.log('db2 query result:',result)}
from serverless_db_sdk import databasedef main_handler(event, context):print('Start Serverlsess DB SDK function')connection = database().connection(autocommit=False)cursor = connection.cursor()cursor.execute('SELECT * FROM name')myresult = cursor.fetchall()for x in myresult:print(x)
本页内容是否解决了您的问题?