Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Serverless MySQL database

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to deploy Serverless MySQL database". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to deploy Serverless MySQL database.

TDSQL-C Serverless (MySQL database), with fully automated capacity expansion, can intelligently "expand" and "shrink" with the increase and decrease of the number of user requests, and realize the automatic "huff and puff" of resources. From then on, developers can really complete the development of Serverless applications, focus on the business itself, avoid operation and maintenance, pay on demand, and enjoy the many advantages of Serverless architecture.

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

Service characteristics

Self-driving (Autopilot):

The database starts and stops automatically according to the business load, and the capacity expansion process will not be disconnected.

Pay-per-use (Utility Pricing):

It is charged according to the calculation and storage capacity actually used, no payment is required, it is measured by seconds and settled by the hour.

Applicable scenario

Development, test environment and other low-frequency database usage scenarios.

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

SaaS application scenarios such as building stations for small and medium-sized enterprises.

The following tutorial shows you how to quickly create a TDSQL-C Serverless MySQL instance and call it in a cloud function using the functions of the Node.js development language:

Operation steps

Configure environment variables

Configure VPC: create VPC and subnet through Serverless Framework VPC component, and support network connection and use of cloud functions and databases.

Configure Serverless DB: create MySQL instances through Serverless Framework Cynosdb components to provide database services for cloud function projects.

Write business code: call the database through Serverless DB SDK, and the cloud function can call Serverless DB SDK directly to connect to the PostgreSQL database for management operations.

Deploy applications: deploy the project to the cloud through Serverless Framework, and test it through the SCF console.

Remove items: items can be removed through Serverless Framework.

1. Configure environment variables

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

Mkdir test-MySQL & & cd test-MySQL

Since TDSQL-C Serverless only supports four areas: ap-beijing-3,ap-guangzhou-4,ap-shanghai-2 and ap-nanjing-1, you only need to configure here. You only need to create .env file under the project root, and then configure two environment variables, REGION and ZONE:

# .envREGION = xxx ZONE=xxx2. Configure VPC

Create the folder VPC under the test-MySQL directory.

Mkdir VPC & & cd VPC

At the same time, create a new serverless.yml file in VPC, and use VPC components to complete the creation of VPC and subnet.

The example of serverless.yml is as follows. Refer to the product documentation for full configuration.

# serverless.ymlorg: mysql-appapp: 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-mysql subnetName: serverless-mysql3. Configure Serverless DB

Create a folder DB under test-MySQL, create a new serverless.yml file under the DB folder, and enter the following to complete the configuration of the cloud development environment through the Serverless Framework component.

The example of serverless.yml is as follows. Refer to the product documentation for full configuration.

# serverless.yml org: mysql-appapp: 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} 4. Write business code and configuration files

Create a folder src under test-MySQL to hold the business logic code and related dependencies. And create the file index.js under the src folder, and enter the following sample code. Connect to the database through SDK in the function, and complete the call to the MySQL database in it.

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 dependency modules:

Npm install mysql2

After the business code is written and the dependency installation is completed, create a serverless.yml file. The sample file is as follows:

Org: mysql-appapp: mysql-appstage: devcomponent: scfname: mysql-app-scfinputs: 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

After the creation, the project directory structure is as follows:

. / test-MySQL ├── vpc │ └── serverless.yml # vpc configuration file ├── db │ └── serverless.yml # db configuration file ├── src │ ├── serverless.yml # scf component profile │ ├── node_modules # project dependency file │ └── index.js # entry function └── .env # environment variable file

Using the command line under test-MySQL, execute the following command to deploy.

Sls deploy

Code scanning authorization is required for deployment. If you do not have a Tencent Cloud account, please register a new account.

If it is a sub-account, please refer to the permission configuration of the sub-account to complete the authorization

The returned result is as follows, which means that 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 the deployment is successful, you can view and debug the function through the SCF console. The successful test is shown below:

Remove items

Under the test-MySQL directory, execute the following command to remove the project.

Sls remove

If the following result is returned, it will be removed successfully.

Serverless ⚡ framework4s > test-MySQL > Success

In addition to creating all resources with one click of the component, you can also complete the creation of the Serverless version of MySQL database through the console and call it normally using SDK in the cloud function.

At this point, I believe you have a deeper understanding of "how to deploy the Serverless MySQL database". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report