mysql less2_The first domestic serverless MySQL database is released!

12 month 20 Japanese Techo At the conference , Tencent cloud has released its own cloud native database TDSQL-C Serverless( primary CynosDB Serverless), This is the first full-scale computing and storage product in China Serverless Architecturally Cloud native MySQL, TDSQL-C Serverless It can make enterprise users like water 、 electric 、 Coal uses cloud databases as well , Users don't have to pay for the idle time of the database , Instead, it is charged according to the actual usage of the database resource response unit , Tencent cloud native technology will benefit users .

As Serverless An important part of Ecology ,TDSQL-C Serverless And the release of Serverless In architecture MySQL The vacancy in the database . therefore ,Serverless Framework It's released synchronously TDSQL-C Serverless Components . such , Tencent cloud has a set of Serverless The core solution of the whole stack , Include Serverless API gateway 、 Tencent cloud's Serverless Function computing services 、Serverless Object storage and database . In the data storage layer, you can use TDSQL-C Serverless Connect directly to the database and SQL operation .

TDSQL-C Serverless(MySQL database ), Fully automated capacity expansion capability , With the increase and decrease of the number of user business requests , Intelligent “ inflation ” and “ narrow ”, Realize the automation of resources “ throughput ”. From then on , Developers can actually do Serverless Application development , Focus on the business itself , No maintenance , Pay as you go , enjoy Serverless Many advantages brought by architecture .

in addition ,TDSQL-C Serverless The database is completely free during the trial period .

Service features

Autopilot (Autopilot):

The database starts and stops automatically according to the business load , Noninductive expansion and contraction , The expansion and contraction process does not disconnect .

Pay by use (Utility Pricing):

According to the actual use of calculation and storage charge , You don't have to pay , Measured in seconds , By the hour .

Applicable scenario

Development 、 Test environment and other low frequency database usage scenarios .

The Internet of things (IoT)、 Uncertain load scenarios such as edge computing .

Small and medium-sized enterprises set up stations, etc SaaS Application scenarios .

The following tutorial will focus on Node.js Develop language functions , How to quickly create TDSQL-C Serverless MySQL example , And call in the cloud function :

Operation steps

Configure environment variables

Configure private networks : adopt Serverless Framework VPC Components establish VPC and subnet , Support the network connection and use of cloud function and database .

To configure Serverless DB: adopt Serverless Framework Cynosdb Components establish MySQL example , Provide database services for cloud function projects .

Write business code : adopt Serverless DB SDK Call database , Cloud functions support direct calls Serverless DB SDK, Connect PostgreSQL Database management operations .

Deploy the application : adopt Serverless Framework Deploy projects to the cloud , And test through the cloud function console .

Remove items : It can be done by Serverless Framework Remove items .

1. Configure environment variables

Create a directory locally , Used to store code and dependent modules . This article takes test-MySQL Folder as an example .

mkdir test-MySQL && cd test-MySQL

Due to the present TDSQL-C Serverless Only support ap-beijing-3,ap-guangzhou-4,ap-shanghai-2 and ap-nanjing-1 Four areas , So we need to configure , Just create it in the root of the project .env file , Then configure REGION and ZONE Two environment variables :

# .env

REGION=xxx

ZONE=xxx

2. Configure private networks

stay test-MySQL Create a folder in the directory VPC.

mkdir VPC && cd VPC

At the same time VPC New China serverless.yml file , Use VPC Components Complete the creation of private network and subnet .

serverless.yml Examples are as follows , Full configuration reference Product documentation

#serverless.yml

org: mysql-app

app: mysql-app

stage: dev

component: 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-mysql

subnetName: serverless-mysql

3. To configure Serverless DB

stay test-MySQL Create folder DB, And in DB New under folder serverless.yml file , And enter the following , adopt Serverless Framework Component completes the configuration of cloud development environment .

serverless.yml Examples are as follows , Full configuration reference Product documentation

# serverless.yml

org: mysql-app

app: mysql-app

stage: dev

component: cynosdb

name: mysql-app-db

inputs:

region: ${env:REGION}

zone: ${env:ZONE}

vpcConfig:

vpcId: ${output:${stage}:${app}:mysql-app-vpc.vpcId}

subnetId: ${output:${stage}:${app}:mysql-app-vpc.subnetId}

4. Write business code and configuration files

stay test-MySQL Create folder src, It is used to store business logic code and related dependencies . And in src Create files under folders index.js, Enter the following example code . Pass through in function SDK Connect to database , And do it in it MySQL Database call .

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();

}

Install the required dependent modules :

npm install mysql2

After completing the business code writing and dependency installation , establish serverless.yml file , The sample file is as follows :

org: mysql-app

app: mysql-app

stage: dev

component: scf

name: mysql-app-scf

inputs:

src: ./

functionName: ${name}

region: ${env:REGION}

runtime: Nodejs10.15

timeout: 30

vpcConfig:

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}

5. Rapid deployment

When you're done creating , The project directory structure is as follows :

./test-MySQL

├── vpc

│ └── serverless.yml # vpc The configuration file

├── db

│ └── serverless.yml # db The configuration file

├── src

│ ├── serverless.yml # scf Component configuration file

│ ├── node_modules # Project dependent files

│ └── index.js # Entry function

└── .env # Environment variables file

Use the command line in test-MySQL Next , Execute the following command to deploy .

sls deploy

Code scanning authorization is required for deployment , If there is no Tencent cloud account , please Sign up for a new account.

If it is a sub account , Please refer to Sub account permission configuration Complete authorization

The return result is as follows , The deployment is successful .

mysql-app-vpc:

region: xxx

zone: xxx

vpcId: xxxx-xxx

...

mysql-app-db:

dbMode: xxxx

region: xxxx

zone: xxxx

...

mysql-app-scf:

functionName: xxxx

description: xxx

...

59s › test-MySQL › "deploy" ran for 3 apps successfully.

After successful deployment , You can go through Cloud function console, View and debug functions , The test is successful, as shown in the figure below :

2baf1d8b851e0508b6b7d9476883dc98.png

Remove items

stay test-MySQL Under the table of contents , Execute the following command to remove the item .

sls remove

Returns the following , It is successfully removed .

serverless framework

4s › test-MySQL › Success

In addition to creating all resources with one click of a component , You can also do it through the console Serverless edition MySQL Database creation , And normal use in cloud functions SDK To complete the call .

Serverless Wordpress Station building , just 3 Step :

https://console.cloud.tencent... , The first deployment of a new user Wordpress application , You can get 30 element TDSQL-C ,5 element CFS File storage voucher

One More Thing

Experience Tencent cloud immediately Serverless Demo, receive Serverless New user gift pack serverless/start

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值